Tips
document
You can usually read the documentation for Unity’s adaptation of mini-games by yourself (bushi). WeChat Mini Game Adaptation Solution
Download plugin
往下翻找到下载Unity插件,点击下载![在这里插入图片描述](https://img-blog.csdnimg.cn/ee5bbc32675c4c2cbcc1736ff7eb1ca9.png)
- Download for later use
Install the Addressables package
- Let’s do the basic operations first and add more details later.
- New mini game project unity version 2020.3.46
Load the Addressable package
工具栏打开Window-Packages Manager
Packages 选择 Unity Registry找到Addressables
install安装
![在这里插入图片描述](https://img-blog.csdnimg.cn/14f2ad031b334de58fc42da556ebd1fd.png)
-
Switch Build Setting to WebGl
-
Get some resources (xml, png, prefab, scene, etc...)
Using Addressables to handle resources
Click Window - Asset Management - Groups to open the Addressables Group panel
Click Create to create a new Addressables Settings
3. After the creation, the Addressable Groups panel will add the Default Local Group (Default) group. Addressables defaults to AssetBundle packaging according to the Group group.Put resources in a Group group, then these will be placed in the same Group group
Right-click the panel or click the Create button in the upper left corner to create a new group
**Assets目录下会新增AddressableAssetsData文件夹**
![在这里插入图片描述](https://img-blog.csdnimg.cn/85e9b3d38e4e44968e517ceeb2e7122a.png)
其中Default Local Group是默认的Group组设置文件,自己新建的分组(MyAssets)设置文件也在相同的位置
Add resources
1. 直接拖
2. Click on the resource and check Addressable under the Inspector panel. The address after Addressable is the resource address. When loading, you can directly search for resources based on this address.
3. Group settings.
Click the Default Local Group file in the AddressableAssetsData directory to open the Default Local Group settings panel.
Build Path: 资源包创建的位置
Load Path: 资源包加载的位置
**LocalBuildPath**资源打包的位置在Library\com.unity.addressables\aa\WebGL下
**RemoteBuildPath** 资源打包位置在ServerData\WebGL下
-
Labels settings
: Click the red arrow position to open the label options, click Manage Labels to open the label settings, create your own labels,
and then subdivide labels for the resources.
- To package resources
, click the AddressableAssetSettings file in the AddressableAssetsData directory.
Click Manage Groups to open the Groups management interface, and click Build-New Build-Default Build Script to start creation.
Creation is completed.
- To package resources
Addressable loads resources
code
Load images based on address
static AsyncOperationHandle<Sprite> spHandle;
// 地址
string keys = "Assets/Resouces/cpy.png";
// Start is called before the first frame update
void Start()
{
StartCoroutine(loadSprite(keys));
}
IEnumerator loadSprite(string kety)
{
spHandle = Addressables.LoadAssetAsync<Sprite>(kety);
while (!spHandle.IsDone)
{
Debug.Log($"加载中{
spHandle.PercentComplete.ToString()}");
yield return null;
}
onSPLoaded(spHandle.Result);
}
void onSPLoaded(Sprite sp)
{
transform.GetComponent<Image>().sprite = sp;
}
运行结果
Load preset
static AsyncOperationHandle<GameObject> goHandle;
// 地址
string keys = "Assets/Prefabs/Cube.prefab";
// Start is called before the first frame update
void Start()
{
StartCoroutine(loadSprite(keys));
}
IEnumerator loadSprite(string kety)
{
goHandle = Addressables.LoadAssetAsync<GameObject>(kety);
while (!goHandle.IsDone)
{
Debug.Log($"加载中{
goHandle.PercentComplete.ToString()}");
yield return null;
}
onOBLoaded(goHandle.Result);
}
void onOBLoaded(GameObject sp)
{
GameObject ob = Instantiate(sp);
}
Bulk load by tag
static AsyncOperationHandle<IList<Sprite>> spHandle;
static string iconLabels = "icon";
// Start is called before the first frame update
void Start()
{
StartCoroutine(loadAllSprite(iconLabels));
}
IEnumerator loadAllSprite(string key)
{
spHandle = Addressables.LoadAssetsAsync<Sprite>(key, onSpLoaded);
yield return null;
}
void onSpLoaded(Sprite sp)
{
Debug.Log($"icon name : {
sp.name.ToString()}");
}
The running results are as follows
Load scene
static AsyncOperationHandle<SceneInstance> sceneHandle;
static string sceneKey = "Assets/Scenes/TestScene.unity";
SceneInstance scene;
// Start is called before the first frame update
void Start()
{
StartCoroutine(loadScene(sceneKey));
}
IEnumerator loadScene(string key)
{
// 保留原场景,不保留Addressables.LoadSceneAsync(key, LoadSceneMode.Single, activateOnLoad: true)
// 加载模式LoadSceneMode 激活activateOnLoad
sceneHandle = Addressables.LoadSceneAsync(key, LoadSceneMode.Additive, activateOnLoad: false);
while (!sceneHandle.IsDone)
{
Debug.Log($"load pcs : {
sceneHandle.PercentComplete.ToString()}");
yield return null;
}
scene = sceneHandle.Result;
onSceneLoaded();
}
void onSceneLoaded()
{
scene.ActivateAsync();
}
operation result
API warrior
LoadAssetsAsync的三个重载:
// 用于加载单个标签或地址
static AsyncOperationHandle<IList<T>> LoadAssetsAsync<T>(object key, Action<T> callback);
// 用于加载地址+标签, MergeMode 为找到的资源合并模式
static AsyncOperationHandle<IList<T>> LoadAssetsAsync<T>(IList<object> keys, Action<T> callback, MergeMode mode);
static AsyncOperationHandle<IList<T>> LoadAssetsAsync<T>(IList<IResourceLocation> locations, Action<T> callback);
Unload
resourcesAddressables.Release
- Unload individual resources from memory
- Release handle
File preloading
Transfer to WeChat mini game
Import the initially downloaded plug-in
After the import is completed, there will be a WeChat mini-game button on the menu bar. Click Convert mini-game to open the tool panel.
Modify Player Settings
Color Space Select Gamma, otherwise
the Auto Graphics API cannot be packaged. Uncheck
the Graphics API to only retain WebGL 1.0 or WebGL 2.0, otherwise the package will be too large.
Lightmap Encoding Select Normal Quality, otherwise the resources will be large.
Tool panel settings
Basic Information
- For the game APPID, go to the WeChat Mini Program Center to apply for it. Or get a test account in WeChat developer tools
- Game resource CDN, the CDN address where game resources are stored. You don’t have to write it down and change it later in the project.
- Pour out the path, the location where the WeChat mini-game is exported
Start Loading settings
-
Start the background image
and you can modify the cover to your own logo
-
Loading phase video URL
changes loading phase video -
First package loading method
Change the first package loading method -
Others can be tested by yourself
Debug compile options
Check Clear Streaming Assets
WeChat debugging
resource
Open the converted folder, open the webgl folder
StreamingAssets (resource folder)
.webgl.data.unityweb.bin.txt (resource information) and put the three
index.html
files on the remote CDN (the rest of the mini-games are not needed )
Modify CDN address
Open the game.js file, find "DATA_CDN" and modify the following address.
Open the project with WeChat developer tools
other problems
- Chinese is not displayed.
Change all Unity default fonts to new fonts. - When opening , there is a compilation error and the game.json file cannot be found.
This is generally done by deleting the source file, resetting the APPID, and then importing it. Or
find the project.config.json file and add
"miniprogramRoot" to the line above the APPID: "minigame/",