今天来简单介绍一下Addressables,并介绍一下如何通过AssetName加载单个资源、如何通过Label加载多个资源、以及如何通过List<string>加载多个资源。由于Addressables的资源加载均为异步加载,所以今天给大家介绍如何使用StartCoroutine、如何使用Async/Await、如何使用Completed回调的三种方式加载。
准备工作
首先我们需要在Unity菜单栏Window中找到Package Manager,在Package Manager中下载并安装Addressables组件。
我们在项目中准备了一些游戏资源,并且可以在Inspector窗口下将资源选择为加载资源,对于目录而言也可以整体选择为加载资源。
选择完加载资源之后,我们通过Window->Asset Management->Addressables->Groups路径页签打开Addressables Groups窗口,Addressables Groups中列举的则是所有选中的加载资源。
我们可以对每个资源进行Label分类,当然不分类默认default也没关系。点击Manage Labels可以打开Labels窗口自定义自己所需要的标签。关于Label有什么用往后看。
使用StartCoroutine加载资源
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;public class LoadCoroutine : MonoBehaviour
{public void Awake(){StartCoroutine(LoadAssetAsync("LanguageConfig"));StartCoroutine(LoadMultipleAssetAsync("Config"));StartCoroutine(LoadMultipleAssetAsync(new List<string>() { "LanguageConfig", "RoleConfig", "Prefab" }));}/// <summary>单资源加载</summary>/// <param name="pAssetName"></param>public IEnumerator LoadAssetAsync(string pAssetName){AsyncOperationHandle asyncOperationHandle = Addressables.LoadAssetAsync<UnityEngine.Object>(pAssetName);yield return asyncOperationHandle;LoadAssetAsyncCompleted(asyncOperationHandle);}/// <summary>多资源加载(通过Label)</summary>/// <param name="pLabelsName"></param>public IEnumerator LoadMultipleAssetAsync(string pLabelsName){AsyncOperationHandle<IList<UnityEngine.Object>> asyncOperationHandle = Addressables.LoadAssetsAsync<UnityEngine.Object>(pLabelsName, null);yield return asyncOperationHandle;LoadAssetAsyncCompleted(asyncOperationHandle);}/// <summary>多资源加载(通过List)</summary>/// <param name="pNameList"></param>public IEnumerator LoadMultipleAssetAsync(List<string> pNameList){AsyncOperationHandle<IList<UnityEngine.Object>> asyncOperationHandle = Addressables.LoadAssetsAsync<UnityEngine.Object>(pNameList, null, Addressables.MergeMode.Union, false);yield return asyncOperationHandle;LoadMultipleAssetAsyncCompleted(asyncOperationHandle);}/// <summary>加载完成回调</summary>public void LoadAssetAsyncCompleted(AsyncOperationHandle pHandle){if (pHandle.Status == AsyncOperationStatus.Succeeded){if (pHandle.Result is List<UnityEngine.Object>){List<UnityEngine.Object> assetList = pHandle.Result as List<UnityEngine.Object>;for (int index = 0; index < assetList.Count; index++){Debug.LogError(assetList[index]);}}else{Debug.LogError(pHandle.Result);}}}/// <summary>加载完成回调</summary>public void LoadMultipleAssetAsyncCompleted(AsyncOperationHandle<IList<UnityEngine.Object>> pHandle){if (pHandle.Status == AsyncOperationStatus.Succeeded){if (pHandle.Result is List<UnityEngine.Object>){List<UnityEngine.Object> assetList = pHandle.Result as List<UnityEngine.Object>;for (int index = 0; index < assetList.Count; index++){Debug.LogError(assetList[index]);}}else{Debug.LogError(pHandle.Result);}}}
}
我们这里先来介绍使用StartCoroutine加载资源,这里我提供了三种加载资源的方法。
public IEnumerator LoadAssetAsync(string pAssetName) 是通过资源名称加载资源,这个方法只能加载单个资源,这里的参数pAssetName指代的则是Addressables Groups窗口中的Addressable Name。这里的Addressable Name是可以自行修改的,资源选取的时候默认是全路径名称,就像上面截图中MainPanel一样显示的是全路径。我们在开发中可以根据自己的需求进行修改。
public IEnumerator LoadMultipleAssetAsync(string pLabelsName)是通过Label名称加载资源的,一次可以加载一个或多个资源。这个方法会将所有标记为pLabelsName的资源加载进来,并且返回一个IList<TObject>类型的结果。
public IEnumerator LoadMultipleAssetAsync(List<string> pNameList)是通过一个名称列表加载资源的,一次可以加载一个或多个资源。这里的pNameList参数既可以包含单个pAssetName,也可以包含pLabelsName,或者同时包含pAssetName和pLabelsName。加载完成后同样会返回一个IList<TObject>类型的结果,这里需要说明的是一个资源只会被加载一次,例如pNameList中包含了资源名MainPanel,又包含了Label名Prefab,同时MainPanel又被标记为了Prefab,此时加载返回的资源结果中只会有一个MainPanel。
以上三种方法均是调用了Addressables类中提供的资源加载接口,Addressables中则是包含了所有的资源加载接口,感兴趣的小伙伴可以自己研究。
Addressables的加载接口返回的均为一个AsyncOperationHandle对象,由于AsyncOperationHandle对象继承自IEnumerator,所以我们可以将加载方法写成一个协程函数通过StartCoroutine来进行调用。加载完成后我们可以通过AsyncOperationHandle.Result获取加载的资源,Result为object类型在实际使用时需要根据实际类型进行转换后使用。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;public class LoadAsyncAwait : MonoBehaviour
{public void Awake(){LoadAssetAsync("LanguageConfig");LoadMultipleAssetAsync("Config");LoadMultipleAssetAsync(new List<string>() { "LanguageConfig", "MainPanel", "SettingPanel" });}/// <summary>单资源加载</summary>/// <param name="pAssetName"></param>public async void LoadAssetAsync(string pAssetName){AsyncOperationHandle asyncOperationHandle = Addressables.LoadAssetAsync<UnityEngine.Object>(pAssetName);await asyncOperationHandle.Task;LoadAssetAsyncCompleted(asyncOperationHandle);}/// <summary>多资源加载(通过Label)</summary>/// <param name="pLabelsName"></param>public async void LoadMultipleAssetAsync(string pLabelsName){AsyncOperationHandle asyncOperationHandle = Addressables.LoadAssetsAsync<UnityEngine.Object>(pLabelsName, null);await asyncOperationHandle.Task;LoadAssetAsyncCompleted(asyncOperationHandle);}/// <summary>多资源加载(通过List)</summary>/// <param name="pNameList"></param>public async void LoadMultipleAssetAsync(List<string> pNameList){AsyncOperationHandle<IList<UnityEngine.Object>> asyncOperationHandle = Addressables.LoadAssetsAsync<UnityEngine.Object>(pNameList, null, Addressables.MergeMode.Union, false);await asyncOperationHandle.Task;LoadMultipleAssetAsyncCompleted(asyncOperationHandle);}/// <summary>加载完成回调</summary>public void LoadAssetAsyncCompleted(AsyncOperationHandle pHandle){if (pHandle.Status == AsyncOperationStatus.Succeeded){if (pHandle.Result is List<UnityEngine.Object>){List<UnityEngine.Object> assetList = pHandle.Result as List<UnityEngine.Object>;for (int index = 0; index < assetList.Count; index++){Debug.LogError(assetList[index]);}}else{Debug.LogError(pHandle.Result);}}}/// <summary>加载完成回调</summary>public void LoadMultipleAssetAsyncCompleted(AsyncOperationHandle<IList<UnityEngine.Object>> pHandle){if (pHandle.Status == AsyncOperationStatus.Succeeded){if (pHandle.Result is List<UnityEngine.Object>){List<UnityEngine.Object> assetList = pHandle.Result as List<UnityEngine.Object>;for (int index = 0; index < assetList.Count; index++){Debug.LogError(assetList[index]);}}else{Debug.LogError(pHandle.Result);}}}
}
使用Async/Await加载资源
接下来介绍的是使用Async/Await方式加载资源,同样示例中展示了通过AssetName加载资源、通过Label加载资源、以及通过List<string>加载资源,同样是调用Addressables类的加载接口加载资源。不同的是Async/Await方式使用的是AsyncOperationHandle中提供的Task异步类,在函数写法上略有不同。相比StartCoroutine而言Async/Await的调用方式与普通函数无异。在加载完成之后,同样从AsyncOperationHandle.Result获取加载的资源资源对象。
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;public class LoadCompleted : MonoBehaviour
{public void Awake(){LoadAssetAsync("LanguageConfig", LoadAssetAsyncCompleted);LoadMultipleAssetAsync("Config", LoadMultipleAssetAsyncCompleted);LoadMultipleAssetAsync(new List<string>() { "LanguageConfig", "MainPanel", "SettingPanel" }, LoadMultipleAssetAsyncCompleted);}/// <summary>单资源加载</summary>/// <param name="pAssetName"></param>/// <param name="LoadComplete"></param>public void LoadAssetAsync(string pAssetName, Action<AsyncOperationHandle> LoadComplete){AsyncOperationHandle asyncOperationHandle = Addressables.LoadAssetAsync<UnityEngine.Object>(pAssetName);asyncOperationHandle.Completed += LoadComplete;}/// <summary>多资源加载(通过Label)</summary>/// <param name="pLabelsName"></param>/// <param name="LoadComplete"></param>public void LoadMultipleAssetAsync(string pLabelsName, Action<AsyncOperationHandle<IList<UnityEngine.Object>>> LoadComplete){AsyncOperationHandle<IList<UnityEngine.Object>> asyncOperationHandle = Addressables.LoadAssetsAsync<UnityEngine.Object>(pLabelsName, null);asyncOperationHandle.Completed += LoadComplete;}/// <summary>多资源加载(通过List)</summary>/// <param name="pNameList"></param>/// <param name="LoadComplete"></param>public void LoadMultipleAssetAsync(List<string> pNameList, Action<AsyncOperationHandle<IList<UnityEngine.Object>>> LoadComplete){AsyncOperationHandle<IList<UnityEngine.Object>> asyncOperationHandle = Addressables.LoadAssetsAsync<UnityEngine.Object>(pNameList, null, Addressables.MergeMode.Union, false);asyncOperationHandle.Completed += LoadComplete;}/// <summary>加载完成回调</summary>public void LoadAssetAsyncCompleted(AsyncOperationHandle pHandle){if (pHandle.Status == AsyncOperationStatus.Succeeded){if (pHandle.Result is List<UnityEngine.Object>){List<UnityEngine.Object> assetList = pHandle.Result as List<UnityEngine.Object>;for (int index = 0; index < assetList.Count; index++){Debug.LogError(assetList[index]);}}else{Debug.LogError(pHandle.Result);}}}/// <summary>加载完成回调</summary>public void LoadMultipleAssetAsyncCompleted(AsyncOperationHandle<IList<UnityEngine.Object>> pHandle){if (pHandle.Status == AsyncOperationStatus.Succeeded){if (pHandle.Result is List<UnityEngine.Object>){List<UnityEngine.Object> assetList = pHandle.Result as List<UnityEngine.Object>;for (int index = 0; index < assetList.Count; index++){Debug.LogError(assetList[index]);}}else{Debug.LogError(pHandle.Result);}}}
}
使用Completed回调加载资源
最后一种方式是使用Completed回调来加载资源。与前两种方式最大的不同就是在调用方法时需要传入回调函数,当资源加载完成时则会调用回调函数。回调函数则会返回一个AsyncOperationHandle类型的参数,使用方法与前两种方式相同。
官方文档链接
Loading Addressable assets:https://docs.unity3d.com/Packages/com.unity.addressables@1.18/manual/LoadingAddressableAssets.html
Addressables类:https://docs.unity3d.com/Packages/com.unity.addressables@1.18/api/UnityEngine.AddressableAssets.Addressables.html