2021-07-14【unity】Addressble bundle
1.同步加载一个:public T LoadAsset<T>(string key, Action<T> callback = null) { var op = Addressables.LoadAssetAsync<T>(key); var t = op.WaitForCompletion(); callback?.Invoke(t); Addressables.Release(op); return t; }
2.同步加载多个:
public IList<T> LoadAssets<T>(string key, Action<T> callback = null) { var op = Addressables.LoadAssetsAsync<T>(key, ta => callback?.Invoke(ta)); var t = op.WaitForCompletion(); Addressables.Release(op); return t; }
3.异步加载一个:
public async Task<T> LoadAssetAsync<T>(string key, Action<T> callback = null) { var t = await Addressables.LoadAssetAsync<T>(key).Task; callback?.Invoke(t); return t; }
4.异步加载:
public async Task LoadAllEffectObj(){ var loadedEffects = new List<GameObject>(); int count = EFX.rows.Length; for (int i = 0; i < EFX.rows.Length; ++i) { var path = StringUtility.GetBuilder().Append(EFX.rows.respath).Append(EFX.rows.m枚举).Append(".prefab").ToString(); LoadMgr.Instance.LoadAssetAsync<GameObject>(path, (x) => { count--; if (x != null) { Debug.Log("加载特效:" + path); loadedEffects.Add(x); } else { Debug.LogError("加载特效失败:" + path); } }); } await Task.Run(() => { while (count > 0) { //wait } return; }); ObjectPool.PreloadedPrefab[] allPrefabs = new ObjectPool.PreloadedPrefab; for (int i = 0; i < loadedEffects.Count; i++) { ObjectPool.PreloadedPrefab preloadedPrefab = new ObjectPool.PreloadedPrefab(loadedEffects, 2) { }; allPrefabs = preloadedPrefab; } ObjectPool.Instance.AddPreloadPrefabs(allPrefabs);}
页:
[1]