/// <summary> /// 自定义LoadTask。当非实例化资源(非GameObject,如Material,Texture,TextAsset等)使用完毕后,需要主动调用Destroy去释放资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="task"></param> public static void LoadAsset <T>(IAssetLoadTask <T> task) where T : UnityEngine.Object { if (task == null) { return; } Instance.StartCoroutine(Instance.LoadAssetAsync(task)); }
private IEnumerator LoadAssetAsync <T>(IAssetLoadTask <T> task) where T : UnityEngine.Object { if (mStatus != LoadStatus.Done) { yield return(BeginInitialize()); } BundleAsset bundle = GetOrCreateBundle <BundleAsset>(task.bundleName); yield return(bundle.LoadAssetAsync(task)); }
/// <summary> /// 自定义LoadTask。当非实例化资源(非GameObject,如Material,Texture,TextAsset等)使用完毕后,需要主动调用Destroy去释放资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="task"></param> public static void LoadAsset <T>(IAssetLoadTask <T> task) where T : UnityEngine.Object { if (task == null) { Debug.LogError("The param task can't be null!"); return; } if (string.IsNullOrEmpty(task.bundleName)) { Debug.LogError("BundlName can't be null!"); return; } if (string.IsNullOrEmpty(task.assetName)) { Debug.LogError("AssetName can't be null!"); return; } Instance.StartCoroutine(Instance.LoadAssetAsync(task)); }
public IEnumerator LoadAssetAsync <T>(IAssetLoadTask <T> task) where T : UnityEngine.Object { if (task == null || task.isCancel) { yield break; } Asset <T> assetT = null; Object asset = null; string assetName = task.assetName; if (mCacheAssetDic.TryGetValue(assetName, out List <IAsset> list)) { for (int i = list.Count - 1; i >= 0; --i) { if (list[i] == null || list[i].destroyed) { list.RemoveAt(i); continue; } var t = list[i] as Asset <T>; if (t != null) { list.RemoveAt(i); assetT = t; break; } } } if (assetT != null) { task.OnCompleted(assetT); } else { #if UNITY_EDITOR if (AssetPath.mode == AssetMode.Editor) { mAssetDic.TryGetValue(assetName, out asset); if (asset == null) { asset = UnityEditor.AssetDatabase.LoadAssetAtPath <Object>(assetName); if (asset) { mAssetDic.Add(assetName, asset); } } } else #endif { mLoadTasks.Add(task); //不是Resources资源 if (bundle == null) { if (status == LoadStatus.None && task.isCancel == false) { yield return(LoadAsync()); } else { yield return(new WaitUntil(() => status == LoadStatus.Done || task.isCancel)); } } if (mAssetDic.ContainsKey(assetName) == false) { if (bundle != null && task.isCancel == false) { var request = bundle.LoadAssetAsync(assetName); yield return(request); if (request.asset != null && mAssetDic.ContainsKey(assetName) == false) { asset = request.asset; mAssetDic.Add(assetName, asset); } } } } if (mAssetDic.ContainsKey(assetName) == false && task.isCancel == false) { ///尝试从Resources加载 string path = AssetPath.GetResourcePath(assetName); ResourceRequest request = Resources.LoadAsync(path); yield return(request); if (request.asset != null && mAssetDic.ContainsKey(assetName) == false) { asset = request.asset; mAssetDic.Add(assetName, asset); mResourceAssets.Add(assetName); } } if (asset == null) { mAssetDic.TryGetValue(assetName, out asset); } if (asset && task.isCancel == false) { if (typeof(T) == typeof(GameObject)) { var go = Object.Instantiate(asset) as GameObject; assetT = new Asset <T>(assetName, this, asset, go as T); } else { assetT = new Asset <T>(assetName, this, asset, asset as T); } } mLoadTasks.Remove(task); if (task.isCancel == false) { task.OnCompleted(assetT); } else { RemoveReference(); } } }