Unity3d C# develops WeChat mini-game screenshot sharing function (including source code)

foreword

Sharing is one of the important cornerstones of game promotion, and it plays an important role in game communication. The meaning of sharing includes many aspects, the most important of which is to promote communication and exchange between users, and at the same time, it can increase the user scale of the game and increase user viscosity for the game side.
Here I will first share a small game I developed myself (if it is convenient, click in to experience it, please):
insert image description here

Before, the author also organized "Unity3d C# to develop WeChat mini-games to share pictures, circle of friends and other functions" . If you need it, you can check it first.
The function of sharing screenshots with friends can intuitively share and display the screen in the game. Object object) or Canvas.toTempFilePathSync(Object object)), the specific implementation is described below, and there are certain restrictions.

accomplish

Here we take screenshot sharing of the share button in the lobby as an example to explain, that is, place a share button UI in the lobby, click to take a screenshot of the scene in the lobby, and then share it. The lobby scene is as follows:
insert image description here

Here we want to capture the location near the vehicle in the scene to share:

insert image description here

The position here needs to control the following parameters to achieve the scope:

x = 0,
y = 0,
width = 375,
height = 300,
destWidth = 375,
destHeight = 300,

x is the abscissa of the upper left corner of the intercepted canvas (that is, the starting x coordinate of the screenshot);
y is the ordinate of the upper left corner of the intercepted canvas (that is, the starting y coordinate of the screenshot);
width is the width of the intercepted canvas;
height is the height of the intercepted canvas;
destWidth is the width of the target file, and the intercepted part will be stretched or compressed to this value;
destHeight is the height of the target file, and the intercepted part will be stretched or compressed to this value.

There is a pitfall here. I used WX.GetSystemInfoSync(); to obtain the screen width and height before, and the information obtained by conversion is not correct on the mobile phone and development tools. You can also test it yourself:

var sys= WX.GetSystemInfoSync();
Debug.Log("screenWidth:" + sys.screenWidth);
Debug.Log("screenHeight:" + sys.screenHeight);
Debug.Log("windowWidth:" + sys.windowWidth);
Debug.Log("windowHeight:" + sys.windowHeight);

And I use Unity's screen information:

 Debug.Log("UnityEngine.Screen.width;" + UnityEngine.Screen.width);
 Debug.Log("UnityEngine.Screen.height;" + UnityEngine.Screen.height);

Since the previews of our shared pictures are basically equal in length and width, those that are too wide may not be fully displayed:
insert image description here

Therefore, the length and width of the screenshots should be basically the same. Here, I take screenshots according to the ratio of 1/3 of the upper and lower middle:

          int ShareHeight = UnityEngine.Screen.height / 3;
            WXCanvas.ToTempFilePath(new WXToTempFilePathParam()
            {
    
    
                x = (Screen.width-ShareHeight) / 2,
                y = ShareHeight,
                width = ShareHeight,
                height = ShareHeight,
                destWidth = ShareHeight,
                destHeight = ShareHeight,
                success = (result) =>
                {
    
    
                    Debug.Log("ToTempFilePath success" + JsonUtility.ToJson(result));
                    WX.ShareAppMessage(new ShareAppMessageOption()
                    {
    
    
                        title = "这是你的标题",
                        imageUrl = result.tempFilePath,
                    });
                },
                fail = (result) =>
                {
    
    
                    Debug.Log("ToTempFilePath fail" + JsonUtility.ToJson(result));
                },
                complete = (result) =>
                {
    
    
                    Debug.Log("ToTempFilePath complete" + JsonUtility.ToJson(result));
                },
            });

The effect of sending is as follows:
insert image description here

According to the official document, you can also use the following synchronization method (the author has not verified it):

           int ShareHeight = UnityEngine.Screen.height / 3;
            var tf = WXCanvas.ToTempFilePathSync(new WXToTempFilePathParam()
            {
    
    
                x = (Screen.width - ShareHeight) / 2,
                y = ShareHeight,
                width = ShareHeight,
                height = ShareHeight,
                destWidth = ShareHeight,
                destHeight = ShareHeight,
            });
            WX.ShareAppMessage(new ShareAppMessageOption()
            {
    
    
                title = "这是你的标题",
                imageUrl = tf,
            });

Other parameters:
fileType There are two types of target files: jpg and png, and the default is png.
quality The quality of the jpg image, only valid when the fileType is jpg. The value range is 0.0 (lowest) - 1.0 (highest), excluding 0. Treated as 1.0 when not in range.

Special attention is that the captured image is only a temporary file, that is, the canvas (Canvas) set for the current screenshot is saved as a temporary file.
If an open data domain is used, the generated file can only be used for the following interfaces:

wx.saveImageToPhotosAlbum、
wx.shareAppMessage、
wx.onShareAppMessage、
wx.previewImage、
wx.previewMedia、
wx.onShareTimeline、
wx.showShareImageMenu

Guess you like

Origin blog.csdn.net/qq_33789001/article/details/132109469