Addressables 我也是刚接触,知道的不是很多,基本的用法还是知道一些的
1.在Window–>Package Manager里找到Addressables进行安装
2.选择资源,点击Assets中的一个资源,在Inspector面板上就会出现一个勾选Assressable,也就是是否加入资源打包的分组,和AssetBundle分组是一个性质。选上以后就出现在Addressable面板的分组里,
有默认分组,也可以自己新建分组,在分组中的资源就可以使用addressable加载资源了。
这是我的分组 点击window->AssetsManagement->Addressables->Groups 调出来这个界面
将资源拖进分组下面也可以直接加进去
红框中就是资源的key,后面的default是标签,也可以根据标签来加载资源
(1)path就是资源在分组中的名字,这个名字可以自定义,也可以是后面的label标签
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)通过AssetReference加载资源,如下图,把勾选的资源挂在脚本的 AssetReference类型下,就可以通过这种方式实例化
(3)加载和卸载场景
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:设置自己资源打包的地址,并选择你设置的位置,进入到Manage Profile
• Local Build Path - 本地端打包路路径
• Local Load Path - 本地端加载路路径
• Remote Build Path - 远端打包路路径
• Remote Load Path - 远端加载路路径
4. play Mode Script有三个选项,第一个是快速加载(faster),可以在Unity编辑器中测试,第二个是模拟加载(advanced),Unity模拟了一个远程加载的过程,第三个Build加载(requires),程序打包的时候,先要选择他,对资源进行打包
5.打包前先把资源打包了,选择Default Build Script,
Update a Previous Build是 再次选择首次构建时生成的bin文件,以更新方式构建资源包。打的是更新补丁包
最后说一下 资源预加载,就是通过Addressables的label标签加载所有的资源
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()
{
}
}
这样只要所有的资源后面 都选择的是default标签,就都会被加载下来,然后再界面加载的时候可以快速加载显示出来。
我这里加了一个通知给js加载进度,unity和js的通信,可以看看官网说明WebGL:与浏览器脚本交互 - Unity 手册