using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.ResourceManagement.AsyncOperations; using ZXK.Framework; /******************************************************************************* *Create By CG *Function Addressables加载释放管理 *******************************************************************************/ namespace ZXK.UTility { public class AddressablesManager : MonoSingleton { /// /// 通过Addressables加载资源 /// /// /// /// public void LoadAsset(string address,System.Action onLoaded) where T : Object { StartCoroutine(LoadAssetCoroutine(address, onLoaded)); } /// /// 通过标签加载所有相关资源 /// /// /// /// public void LoadAssets(string lable, System.Action> onLoaded) where T : Object { StartCoroutine(LoadAssetsCoroutine(lable, onLoaded)); } /// /// 通过资源路径加载资源 /// /// /// /// /// private IEnumerator LoadAssetCoroutine(string address, System.Action onLoaded) where T : Object { AsyncOperationHandle handle = Addressables.LoadAssetAsync(address); yield return handle; if (handle.Status == AsyncOperationStatus.Succeeded) { onLoaded?.Invoke(handle.Result); } else { Debug.LogError($"资源加载失败:{address}"); } } /// /// 通过标签加载所有相关资源 /// /// /// /// /// private IEnumerator LoadAssetsCoroutine(string lable, System.Action> onLoaded) where T : Object { AsyncOperationHandle> handle = Addressables.LoadAssetsAsync(lable,null); yield return handle; if (handle.Status == AsyncOperationStatus.Succeeded) { onLoaded?.Invoke(handle.Result as List); } else { Debug.LogError($"资源加载失败:{lable}"); } } /// /// 卸载资源 /// /// /// public void ReleaseAsset(T asset) where T : Object { Addressables.Release(asset); } } }