Unity uses Addressables to preload all resources, withdraw and load resources, and publish webgl slow loading problems

I am also new to Addressables, and I don’t know a lot, but I still know some basic usage

1. Find Addressables in Window–>Package Manager to install

 

2. Select a resource, click a resource in Assets, and a check Assressable will appear on the Inspector panel, that is, whether to join the resource packaging group, which is the same property as the AssetBundle group. After being selected, it will appear in the grouping of the Addressable panel.

There is a default group, and you can also create a new group yourself. The resources in the group can use addressable to load resources.

This is my group Click window->AssetsManagement->Addressables->Groups to bring up this interface

Drag the resource into the group or add it directly

The red box is the key of the resource, and the following default is the label, and the resource can also be loaded according to the label

 

(1) path is the name of the resource in the group. This name can be customized, or it can be the label tag behind

 Addressables.LoadAssetAsync<GameObject>(path).Completed += (opt) =>
                    {
                        if (opt.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
                        {
                            GameObject panel1 = Instantiate(opt.Result, m_LayerDic[data.layer]);
                            panel1.transform.localPosition = Vector3.zero;
                            panel1.transform.localScale = Vector3.one;
                        }
                        else
                        {
                            Debug.LogError("load error");
                        }
                        m_IsLoading = false;
                    };

(2) Load resources through AssetReference, as shown in the figure below, hang the checked resources under the AssetReference type of the script, and then instantiate them in this way

(3) Loading and unloading scenes

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.ResourceManagement.ResourceProviders;

public class SceneLoadManager : TSingleton<SceneLoadManager>
{
    public void UnloadScene()
    {
        Addressables.UnloadSceneAsync(handle);

    }

    public void LoadScene(string sceneName, UnityEngine.SceneManagement.LoadSceneMode mode= UnityEngine.SceneManagement.LoadSceneMode.Additive)
    {
        CoroutineMgr.Instance.StartCorountine(DoLoadScene(sceneName, mode));
    }

    AsyncOperationHandle<SceneInstance> handle;
    IEnumerator DoLoadScene(string sceneName, UnityEngine.SceneManagement.LoadSceneMode mode)
    {
        handle = Addressables.LoadSceneAsync(sceneName, mode);
        while (!handle.IsDone)
        {
            Debug.Log("LoadSceneAsync" + handle.PercentComplete);
            yield return null;
        }
        Debug.Log("LoadSceneAsync complete");
    }
}

 

3. Profile: Set the address of your own resource packaging, and select the location you set, enter Manage Profile

• Local Build Path - Local Build Path
• Local Load Path - Local Load Path
• Remote Build Path - Remote Build Path
• Remote Load Path - Remote Load Path

4. Play Mode Script has three options, the first is fast loading (faster), which can be tested in the Unity editor, the second is simulated loading (advanced), Unity simulates a remote loading process, and the third Build Loading (requires), when the program is packaged, you must first select it to package the resources

5. Package the resources before packaging, select Default Build Script,  

Update a Previous Build is  to select the bin file generated during the first build again, and build the resource package in an updated manner. Hit the update patch pack

Finally, let’s talk about resource preloading, which is to load all resources through the label tag of Addressables

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

public class Main : MonoBehaviour
{
    public GameObject root;
    private AsyncOperationHandle dn;
    // Start is called before the first frame update
    private void Awake()
    {
         if (UIManager.Instance == null)
        {// 切换场景只实例化一次
            GameObject obj = Instantiate<GameObject>(root);
            obj.name = "UIRoot";
            DontDestroyOnLoad(obj);
        }
    }
    void Start()
    {
        PreLoadAllAssets();
    }
    // 预加载所有资源
    void PreLoadAllAssets()
    {
        dn = Addressables.DownloadDependenciesAsync("default", true);
        while (dn.PercentComplete != 1)
        {
            if (UnityCallJs.Instance)
            {
                UnityCallJs.Instance.LoadAssetsPro_Update(dn.PercentComplete.ToString());
            }
            Debug.Log("下载进度" + dn.PercentComplete);
        }
        dn.Completed += (opt) =>
        {// 加载完成
            if (UnityCallJs.Instance)
            {
                UnityCallJs.Instance.LoadAssetsPro_Update("1");
            }
        };
    }
    // Update is called once per frame
    void Update()
    {
    }
}

 In this way, as long as the default tag is selected behind all resources, they will all be loaded, and then can be quickly loaded and displayed when the interface is loaded.

 Here I added a notification to js loading progress, the communication between unity and js, you can see the official website description WebGL: Interacting with browser scripts - Unity Manual

Guess you like

Origin blog.csdn.net/github_38633141/article/details/123851193