指定资源加载 1 public AssetReferenceT gameObject;
异步加载资源 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 gameObject.LoadAssetAsync<GameObject>().Completed += (op) => { if (op.Status == AsyncOperationStatus.Succeeded) { Debug.Log("gameObject loaded successfully" ); GameObject obj = Instantiate(op.Result); material.LoadAssetAsync<Material>().Completed += (op1) => { obj.GetComponent<MeshRenderer>().material = op1.Result; }; } else { Debug.Log("AudioClip loading failed" ); } };
异步加载场景 1 2 3 4 5 6 7 8 9 Addressables.LoadSceneAsync("Lesson1" , UnityEngine.SceneManagement.LoadSceneMode.Single, false ).Completed += (op) => { if (op.Status == AsyncOperationStatus.Succeeded) { Debug.Log("Scene loaded successfully" ); op.Result.ActivateAsync(); } };
UnityEngine.SceneManagement.LoadSceneMode
:
Single
:加载新的场景时会卸载当前正在运行的所有场景
Additive
:允许同时加载多个场景。当使用这种模式加载场景时,新的场景会被添加到当前正在运行的场景上,而不是卸载它们。这对于需要在一个场景中展示另一个场景内容的应用(如UI叠加、多人游戏中的不同玩家场景)非常有用。
Label标签的作用 运用场景:
一个帽子的建模,不同材质(用标签区分)
通过设备的性能高低来选择不同材质的图片、模型
通过特性限制来查找对象 1 [AssetReferenceUILabelRestriction("gameobject" , "prefab" ) ]
异步加载多个资源 1 2 3 4 5 6 7 8 9 10 11 12 Addressables.LoadAssetsAsync<GameObject>(new string [] { "Cube" , "Sphere" }, (op) => { }, Addressables.MergeMode.Intersection).Completed += (op) => { var i = 0 ; foreach (var handle in op.Result) { GameObject obj = Instantiate((GameObject)handle); obj.transform.position = new Vector3(0 , 5 + i * 5 , 0 ); } };
如果键 (Cube,Red)
映射的结果是 ([1,2,3],[1,3,4])
:
None
:不发生合并,将使用第一个结果 [1,2,3]
Union
:将两个结果合并为一个集合,去除重复元素 [1,2,3,4]
Intersect
:将两个结果合并为一个集合,保留共同元素 [1,3]
UseFirst
:只使用第一个结果 [1,2,3]
AddressableMgr 脚本(封装管理类)
点击展开代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 using System;using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.AddressableAssets;using UnityEngine.ResourceManagement.AsyncOperations;namespace Script { public class AddressableMgr { private static AddressableMgr _instance; public static AddressableMgr Instance { get { if (_instance == null ) { _instance = new AddressableMgr(); } return _instance; } } private Dictionary<string , IEnumerator> Resdict = new Dictionary<string , IEnumerator>(); public void LoadAssetAsync <T >(string name, Action<AsyncOperationHandle<T>> callBack ) { string KeyName = name + "_" + typeof (T).Name; AsyncOperationHandle<T> handle; if (Resdict.TryGetValue(KeyName, out var value )) { handle = (AsyncOperationHandle<T>)value ; if (handle.IsDone) { callBack(handle); } else { handle.Completed += callBack; } return ; } handle = Addressables.LoadAssetAsync<T>(name); handle.Completed += (op) => { if (op.Status == AsyncOperationStatus.Succeeded) { callBack(op); } else { Debug.LogError("异步加载资源失败" ); if (Resdict.ContainsKey(KeyName)) Resdict.Remove(KeyName); } }; Resdict.Add(KeyName, handle); } public void Release <T >(string name ) { string KeyName = name + "_" + typeof (T).Name; if (Resdict.TryGetValue(KeyName, out var value )) { Addressables.Release((AsyncOperationHandle<T>)value ); Resdict.Remove(KeyName); } } public void Clear () { Resdict.Clear(); AssetBundle.UnloadAllAssetBundles(true ); Resources.UnloadUnusedAssets(); GC.Collect(); } } }