Unity develops WeChat games and uses Addressables to manage game resources

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)
  1. Download for later useInsert image description here

Install the Addressables package

  1. Let’s do the basic operations first and add more details later.
  2. 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)
  1. Switch Build Setting to WebGl
    Insert image description here

  2. Get some resources (xml, png, prefab, scene, etc...)
    Insert image description here

Using Addressables to handle resources

Click Window - Asset Management - Groups to open the Addressables Group panel
Insert image description here

Click Create to create a new Addressables Settings
Insert image description here
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
Insert image description here
Insert image description here

**Assets目录下会新增AddressableAssetsData文件夹**
![在这里插入图片描述](https://img-blog.csdnimg.cn/85e9b3d38e4e44968e517ceeb2e7122a.png)

其中Default Local Group是默认的Group组设置文件,自己新建的分组(MyAssets)设置文件也在相同的位置
Add resources
1. 直接拖

Insert image description here
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.
Insert image description here
3. Group settings.
Click the Default Local Group file in the AddressableAssetsData directory to open the Default Local Group settings panel.
Insert image description here

	Build Path: 资源包创建的位置
	Load Path: 资源包加载的位置
	**LocalBuildPath**资源打包的位置在Library\com.unity.addressables\aa\WebGL下
	**RemoteBuildPath** 资源打包位置在ServerData\WebGL下
  1. Labels settings
    : Click the red arrow position to open the label options, click Manage Labels to open the label settings, create your own labels,
    Insert image description here
    and then subdivide labels for the resources.
    Insert image description here

    1. To package resources
      , click the AddressableAssetSettings file in the AddressableAssetsData directory.
      Insert image description here
      Click Manage Groups to open the Groups management interface, and click Build-New Build-Default Build Script to start creation.
      Insert image description here
      Creation is completed.
      Insert image description here

Addressable loads resources

code
Load images based on address

Insert image description here

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;
    }
运行结果

Insert image description here

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);
    }

Insert image description here

Bulk load by tag

Insert image description here

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
Insert image description here

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
Insert image description here

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

  1. Unload individual resources from memory
  2. 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.
Insert image description here

Modify Player Settings

Insert image description here
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

Insert image description here

Basic Information
  1. For the game APPID, go to the WeChat Mini Program Center to apply for it. Or get a test account in WeChat developer tools
    Insert image description here
  2. 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.
  3. Pour out the path, the location where the WeChat mini-game is exported
Start Loading settings
  1. Start the background image
    and you can modify the cover to your own logo
    Insert image description here

  2. Loading phase video URL
    changes loading phase video

  3. First package loading method
    Change the first package loading method

  4. 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 )
Insert image description here

Modify CDN address

Open the game.js file, find "DATA_CDN" and modify the following address.
Insert image description here

Open the project with WeChat developer tools

Insert image description here

other problems
  1. Chinese is not displayed.
    Change all Unity default fonts to new fonts.
  2. 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/",

Guess you like

Origin blog.csdn.net/the_vnas/article/details/132882266