本文主要是介绍【Unity】Addressables资源管理笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【Unity】Addressables资源管理笔记
Addressables是一种用于管理资源的系统。允许以一种灵活的方式加载、卸载和管理资源,无论是场景、预制件、材质、纹理、音频剪辑等。
一、快速实现
-
安装Addressables插件
-
把对象添加到组
1)打开AddressablesGroups面板
window→AssetManagement→Addressables→Grounps
2)建立环境预制体到组
打包预制体后直接拖拽到默认的组(Default Local Group)下
这里可以看见预制体自动添加了一个寻址路径,拿到这个路径就可以动态加载该资源
-
加载对象
简单的资源加载
void Update(){if (Input.GetKeyDown(KeyCode.C)){AddAsset();}}void AddAsset(){Addressables.LoadAssetAsync<GameObject>("Assets/Prefab/Environment_Prefab.prefab").Completed +=(asyncOperationHandle) =>{if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded){Instantiate(asyncOperationHandle.Result);}else{Debug.Log("加载失败");}};}
二、加载方法
-
根据路径加载
void AddAsset(){asyncOperationHandle = Addressables.LoadAssetAsync<GameObject>("Assets/Prefab/Environment_Prefab.prefab");asyncOperationHandle.Completed += AsyncOperationHandle_Completed;}void AsyncOperationHandle_Completed(AsyncOperationHandle<GameObject> asyncOperationHandle){if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded){Instantiate(asyncOperationHandle.Result);}else{Debug.Log("加载失败");}}
-
根据标签加载
[SerializeField] AssetLabelReference labelReference;void AddAssetForAssetLabelReference(){Addressables.LoadAssetAsync<GameObject>(labelReference).Completed +=(asyncOperationHandle) =>{if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded){Instantiate(asyncOperationHandle.Result);}else{Debug.Log("加载失败");}};}
-
指定预制体加载
[SerializeField] AssetReference assetReference;void AddAssetForAssetReference(){assetReference.LoadAssetAsync<GameObject>().Completed+=(asyncOperationHandle) =>{if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded){game= asyncOperationHandle.Result;Instantiate(game);}else{Debug.Log("加载失败");}};}
-
加载多个资源
void AddAssetForAssetLabelReferences(){Addressables.LoadAssetsAsync<GameObject>(labelReference, (addAesst) => { Instantiate(addAesst); }).Completed +=(asyncOperationHandle) =>{if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded){Debug.Log("加载成功");}else{Debug.Log("加载失败");}};}
-
加载创建与卸载
GameObject game;void AddAssetForAssetInstantiateAsync(){assetReference.InstantiateAsync().Completed += (asyncOperation) => game = asyncOperation.Result;}void ClearInstantiateAsset(){//该方法只适用于InstantiateAsync创建的资源assetReference.ReleaseInstance(game);GC.Collect();}
-
远程加载
1)开启远程
window→AssetManagement→Addressables→Setting
勾选BuildRemoteCatalog
Build&Load Paths 选择Remote
选择资源组Addressables Asset Grounp
Build&Load Paths 选择Remote
2)修改配置文件
window→AssetManagement→Addressables→Profiles
Remote.BuildPath是创建路径
Remote.LoadPath是远程地址
[BuildTarget]是发布是类型文件夹,比如webgl和StandaloneWindows64
这里可以创建多个配置文件
3)发布
发布后可以在对应的文件夹下找到资源文件
网发布文件的时候,需要将整个文件夹一起拷贝到地址下
这里可以从配置文件中看到
配置文件地址通常在StreamingAssets→aa下
包括:settings.json和catalog.json
最后建议还是使用AssetBundle,自己写配置和依赖
这篇关于【Unity】Addressables资源管理笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!