using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.AddressableAssets.ResourceLocators; using UnityEngine.ResourceManagement.AsyncOperations; using UnityEngine.ResourceManagement.ResourceLocations; using UnityEngine.U2D; namespace CreatGame.AssetBundle { public class AssetBundleManager : Singleton { private class AssetBundleData { public string assetBundleName; public GameObject assetBundle; } /// /// /// private Dictionary m_AssetBundles; /// /// /// private Dictionary m_SpriteAtlasCache; /// /// 是否初始化完成 /// public bool IsInitializeAsync; /// /// 需要初始化Addressble系统 /// public AssetBundleManager() { IsInitializeAsync = false; m_AssetBundles = new Dictionary(); m_SpriteAtlasCache = new Dictionary(); Addressables.InitializeAsync(); } public void Initialize() { Addressables.InitializeAsync().Completed += OnInitializeCompleted; } /// /// 初始化信息回调 /// private void OnInitializeCompleted(AsyncOperationHandle operationHandle) { if (operationHandle.Status == AsyncOperationStatus.Succeeded) { Debug.Log("Addressables initialization succeeded"); #region MyRegion // IResourceLocator locator = operationHandle.Result; // foreach (object locatorKey in locator.Keys) // { // locator.Locate(locatorKey,typeof(UnityEngine.Object),out IList locations); // if (locations == null) // { // continue; // } // // foreach (var location in locations) // { // string key = location.PrimaryKey; // Addressables.LoadAssetAsync(key).Completed += (handle) => // { // if (handle.Status == AsyncOperationStatus.Succeeded) // { // Debug.Log("Addressables load asset succeeded"); // var asset = handle.Result; // GameObject.Instantiate(asset); // } // }; // } // } #endregion IsInitializeAsync = true; } } /// /// 异步加载资源 /// /// /// public void LoadGameObjectAsync(string assetBundleName, Action callback) { if (m_AssetBundles.TryGetValue(assetBundleName, out var bundle)) { callback.Invoke(GameObject.Instantiate(bundle.assetBundle)); return; } Addressables.LoadAssetAsync(assetBundleName).Completed += (handle) => { if (handle.Status == AsyncOperationStatus.Succeeded) { var assetData = CacheAssetBundles(assetBundleName, handle.Result); Addressables.Release(handle); callback?.Invoke(GameObject.Instantiate(assetData.assetBundle)); } else { Debug.Log($"assetBundleName = {assetBundleName} 加载失败 Status = {handle.Status}"); callback?.Invoke(null); } }; } /// /// 同步等待加载资源 /// /// public GameObject LoadGameObject(string assetBundleName) { if (m_AssetBundles.TryGetValue(assetBundleName, out var bundle)) { return GameObject.Instantiate(bundle.assetBundle); } var handle = Addressables.LoadAssetAsync(assetBundleName); handle.WaitForCompletion(); if (handle.Status == AsyncOperationStatus.Succeeded) { bundle = CacheAssetBundles(assetBundleName, handle.Result); Addressables.Release(handle); return GameObject.Instantiate(bundle.assetBundle); } Debug.LogError("资源加载失败"); return null; } /// /// 缓存加载出来的资源 /// /// /// /// private AssetBundleData CacheAssetBundles(string bundleName, GameObject bundle = null) { var data = new AssetBundleData(){assetBundleName = bundleName, assetBundle = bundle}; m_AssetBundles.Add(bundleName, data); return data; } /// /// 同步加载图集 /// /// /// public SpriteAtlas LoadSpriteAtlas(string atlasName) { if (m_SpriteAtlasCache.TryGetValue(atlasName, out var atlas)) { return atlas; } var handle = Addressables.LoadAssetAsync(atlasName); handle.WaitForCompletion(); if (handle.Status == AsyncOperationStatus.Succeeded) { atlas = handle.Result; Addressables.Release(handle); m_SpriteAtlasCache.Add(atlasName, atlas); return atlas; } Debug.LogError($"图集加载失败 atlasName = {atlasName} Status = {handle.Status}"); return null; } /// /// 异步加载图集 /// /// /// public void LoadSpriteAsync(string atlasName, Action callback) { if (m_SpriteAtlasCache.TryGetValue(atlasName, out var atlas)) { callback.Invoke(atlas); return; } Addressables.LoadAssetAsync(atlasName).Completed += (handle) => { if (handle.Status == AsyncOperationStatus.Succeeded) { atlas = handle.Result; Addressables.Release(handle); m_SpriteAtlasCache.Add(atlasName, atlas); } else { Debug.LogError($"图集加载失败 atlasName = {atlasName} Status = {handle.Status}"); callback?.Invoke(null); } }; } /// /// 释放所有的资源 /// public void ReleaseAll() { foreach (var mAssetBundle in m_AssetBundles) { GameObject.Destroy(mAssetBundle.Value.assetBundle); } m_AssetBundles.Clear(); } } }