Preface
Based on someone's needs, we did a simple technical research: convert the Unity project into a WeChat mini-game.
The main content of this article: steps to convert small games in Unity, solutions to problems encountered, and simple performance test comparisons
Limitations of WeChat mini games
WeChat mini-games have strict restrictions on the size of the program package: the first package must be less than 2M, and the first package plus subpackages (post-loading) must not be larger than 20M. Of course, we can also package game resources for online loading. But this part of WeChat also imposes restrictions: the persistent storage area must not be larger than 200M. In other words, if your game's complete experience package is larger than 200M, you need to make appropriate choices about which parts can be cached locally and which parts will not drive users crazy even if they have to be re-downloaded every time they are run. . . . . .
We don’t have to worry about the first package being less than 2M. The WeChat mini game platform provides Unity developers with a very convenient conversion tool. Through this tool, you can not only directly package WebGL projects into WeChat mini-games, but also automatically provide optimization suggestions and perform one-click optimization of textures and other resources. Unity WeChat mini game packaging plug-in
1. WeChat Mini Game Project Conversion
1.1 Preparation
-
First, convert the existing Unity project to the WebGL platform and ensure it can run normally. Enter Build Settings, click WebGL, then click Switch Platform to convert the project
-
Download Unity packaging plug-in
Unity WeChat mini game packaging plug-in -
After the download is complete, it will look like this:
-
Double-click to enter the plug-in import interface, select the All button, and then click Import to import into the project
4 After Chen Gong imports, the "WeChat Mini Game" button will appear on the toolbar above Unity :
1.2 Set up WeChat mini-game export
- Click the conversion mini-game to bring up the configuration window
The contents in the settings window are relatively easy to understand. Only the most critical ones are selected for explanation below:
Game AppID: WeChat mini game ID. This ID needs to be obtained by creating an account and building a game on the WeChat mini game platform. Get it Fill in here after the ID. If this is empty, the program cannot be executed
Game resource CDN: This is where the game loading body is placed. It should be noted that if you are a mini-game creator, you are allowed to use the HTTP protocol. If you want others to participate in testing and development, so that others can test mini-games in WeChat, the server must support HTTPS. It cannot be empty here
Unity Heap memory: Unity memory heap size setting. Normally the default settings can be used here. But if your small game is large in size and consumes a lot of memory, you need to increase this setting, otherwise it will not pass compilation.
WebGL2.0 (bate): This setting is used to solve the failure error: Error building Player: Incompatible color space with graphics API
1.3 Export mini-game
- After making relevant settings, you can click the Generate and Convert button to start exporting the project. Yes, we will no longer use Unity’s built-in Building function. We will use this button to export projects in the future
The long wait begins
Export completed successfully! You will be surprised to find two folders in the output directory. Nanni? ?
minigame is a homepage program generated by WeChat Mini Games based on your game project. It is just a homepage and does not include the game itself. This file needs to be uploaded to the WeChat mini game platform.
webgl is the game itself and needs to be placed on your resource server. You can use Tencent Cloud (better support, more expensive), other brands, or your own server. The directory of this folder on the server is the path of "Game Resources CDN" in the previous step
After completing the above steps, you can apply for a test link on the mini game platform, search for your own WeChat mini game through WeChat on your mobile phone, and test it.
2. Problems you may encounter
1 Error: When Unity exports a mini-game, it may report: Error building Player: Incompatible color space with graphics API.
Solution: Just check "WebGL2.0(bate)" in the "Mini Game Conversion" settings window.
2 Error: In When running a mini game in WeChat or the emulator, an error is reported in the web console: The plug-in is not authorized
Solution: Check whether the "Game AppID" in the "Convert WeChat Mini Game" window is correct
3 Problem: When running the WeChat mini-game on the emulator or mobile phone, it prompts: Resource download failed. At the same time, the console prompts that the resource package download failed.
Solution: Check whether the "Game Resource CDN" in the "Convert WeChat Mini Game" window is set correctly
4 Problem: The emulator crashes when running the WeChat mini program
Solution: Direct debugging using a mobile phone can often be successful
Three performance tests
3.1. Physical block stress test
Test hardware: iphone12 pro
Test platform: Mini program
Game package size (WeChat storage): 14.5M
Packet size (own server storage): 24.1M
Rendering pipeline: URP
3.2. Block rendering stress test
Test hardware: iphone12 pro
Test platform: ios
Game package size: 55.3M
Rendering pipeline: URP
3.3 Horizontal comparison between WeChat mini games and IOS
3.4 Test conclusion:
- The WeChat mini game rendering engine claims to directly call the WebGL API, which has more powerful performance than Webwiew. But judging from the performance, there is not much improvement, and there is a huge gap compared with native applications.
- According to the table data, it can be seen that whether in pure rendering mode or physics engine mode, performance has great limitations. Especially when the number of rendering faces reaches about 20,000, the frame rate has dropped below 20, and there will be an obvious feeling of lag. When the number of rendering faces reaches 30,000, normal interactive operations are no longer possible.
- There is a crash phenomenon when the game has a large number of simultaneous renderings. Not only in testing, but also in some mature game works that have been launched online.