protected override void StartOneLoadAsync(IAssetLoadTask task) { var assetId = task.AssetId; var loadState = GetLoadState(assetId); var callback = task.Callback; switch (loadState) { case LoadState.NotLoad: OnNotLoad(); break; case LoadState.Loaded: OnLoaded(); break; case LoadState.Loading: OnLoading(); break; default: throw new ArgumentOutOfRangeException(); } if (LoadingIds.Contains(assetId)) { return; } void OnNotLoad() { Callbcker.AddCallback(assetId, ConvertAction); SetLoadState(assetId, LoadState.Loading); LoadingIds.Add(assetId); StartLoadBundle(task); } void OnLoaded() { var asset = Buffer.GetValue(assetId); callback(asset); } void OnLoading() { Callbcker.AddCallback(assetId, ConvertAction); } void ConvertAction(UnityEngine.Object tCallback) { LoadingIds.Remove(assetId); var finalAsset = tCallback; callback?.Invoke(finalAsset); } }
private void LoadAssetAsyncAtType <TAsset2>(IAssetLoadTask task) where TAsset2 : UnityEngine.Object { var id = task.AssetId; _bundleLoader.LoadAsync(id, bf => { if (Buffer.HasValue(task.AssetId)) { var asset = Buffer.GetValue(task.AssetId) as TAsset2; Callbcker.Callback(task.AssetId, asset); RestoreTask(task); } else { if (bf != null) { bf.LoadAsync <TAsset2>(id, OnAssetLoaded); } else { OnAssetLoaded(null); } } } ); void OnAssetLoaded(TAsset2 asset2) { //_bundleLoader.ReleaseTarget(task.AssetId); #if UNITY_EDITOR if (asset2 != null) { RefrashShader(asset2); } else { Debug.LogError("加载ab资源失败:" + id); } #endif if (asset2 != null) { //HideLostRes(asset2); Buffer.TryCache(task.AssetId, asset2); SetLoadState(task.AssetId, LoadState.Loaded); } Callbcker.Callback(task.AssetId, asset2); RestoreTask(task); } }
protected override void StartOneLoadAsync(IBundleLoadTask task) { string path = null; path = BundlePathInfoHelepr.GetBundlePath(task.QueryId); if (!File.Exists(path)) { #if UNITY_EDITOR Debug.LogError($"目标bundle路径{path}不存在!"); #endif } var request = AssetBundle.LoadFromFileAsync(path); #if UNITY_EDITOR ////AnalyzeProfiler.SaveStartLoad(task.TargetBundleId); #endif LoadingIds.Add(task.TargetBundleId); request.completed += op => { IBundleRef bundleRef = null; try { LoadingIds.Remove(task.TargetBundleId); if (request.assetBundle == null) { bundleRef = null; if (Buffer.HasValue(task.TargetBundleId)) { bundleRef = Buffer.GetValue(task.TargetBundleId); } if (bundleRef == null) { throw new Exception($"Bundle__{task.TargetBundleId}加载失败!"); } } else { bundleRef = new BundleRef(); bundleRef.Update(request.assetBundle, OnBundleRelease); SetLoadState(task.TargetBundleId, LoadState.Loaded); Buffer.TryCache(task.TargetBundleId, bundleRef); TryIncreaseBundleRef(bundleRef); } #if UNITY_EDITOR ////AnalyzeProfiler.SaveEndLoad(task.TargetBundleId); #endif } catch (Exception e) { #if DEBUG Debug.LogError(e.Message + e.StackTrace); #endif } finally { string targetBundleId = task.TargetBundleId; RestoreTask(task); Callbcker.Callback(targetBundleId, bundleRef); } }; void TryIncreaseBundleRef(IBundleRef bundleRef) { if (task.InitiatedAssetId == null) { bundleRef.Use(); } } }
private void PushChianTask(string initiatedBundleId, BundleDependInfo dependInfo, AsyncTask asyncTask) { #if DEBUG m_countChian++; if (m_countChian > 10000) { Debug.LogError("严重错误:异步加载堆栈超过10000,请检查加载逻辑或资源依赖关系:" + initiatedBundleId); return; } #endif var dependIds = dependInfo.DirectDepends; var dependCount = dependIds.Count; var index = 0; LoadState loadState = GetLoadState(dependInfo.BundleId); if (loadState == LoadState.AddTask) { return; } SetLoadState(dependInfo.BundleId, LoadState.AddTask); while (index < dependCount) { var sonId = dependIds[index]; loadState = GetLoadState(sonId); if (loadState != LoadState.NotLoad) { index++; continue; } var sonDepndInfo = DependInfoHelper.GetDependInfo(sonId); if (sonDepndInfo.DirectDepends.Count == 0) { var sonTask = TakeTask(); asyncTask.AddCount(); Callbcker.AddCallback(sonId, asyncTask.OneLoadDown); sonTask.InitiatedBundleId = initiatedBundleId; sonTask.TargetBundleId = sonId; //LoadingIds.Add(sonId); WaitTasks.Enqueue(sonTask); SetLoadState(sonId, LoadState.Loading); index++; #if UNITY_EDITOR ////AnalyzeProfiler.AddBundleToBundleRef(sonId, dependInfo.BundleId); #endif } else { PushChianTask(initiatedBundleId, sonDepndInfo, asyncTask); index++; } } //loadState = GetLoadState(dependInfo.BundleId); //if (loadState != LoadState.NotLoad) //{ // return; //} var task = TakeTask(); asyncTask.AddCount(); Callbcker.AddCallback(dependInfo.BundleId, asyncTask.OneLoadDown); task.InitiatedBundleId = initiatedBundleId; task.TargetBundleId = dependInfo.BundleId; //LoadingIds.Add(dependInfo.BundleId); WaitTasks.Enqueue(task); SetLoadState(task.TargetBundleId, LoadState.Loading); #if UNITY_EDITOR if (task.IsMainTask) { ////AnalyzeProfiler.AddAssetToBundleRef(task.TargetBundleId, initiatedBundleId); } else { ////AnalyzeProfiler.AddBundleToBundleRef(task.TargetBundleId, initiatedBundleId); } #endif }
private int m_countChian; //链式加载堆栈计数,以免死循环 #endif public void LoadAsync(string assetId, Action <IBundleRef> callback) { var assetLowerId = assetId.ToLower(); var bundleId = BundlePathInfoHelepr.GetBundleId(assetLowerId); var loadState = GetLoadState(bundleId); switch (loadState) { case LoadState.NotLoad: OnNotLoad(); break; case LoadState.Loaded: OnLoaded(); break; case LoadState.Loading: OnLoading(); break; default: callback?.Invoke(null); throw new ArgumentOutOfRangeException(); } void OnNotLoad() { AsyncTask task = AsyncTask.Get(); task.SetCallback(callback); Callbcker.AddCallback(bundleId, task.MainLoadDown); var dependInfo = DependInfoHelper.GetDependInfo(bundleId); var dependIds = dependInfo.DirectDepends; if (dependIds.Count == 0) { Callbcker.AddCallback(bundleId, task.OneLoadDown); task.AddCount(); SetLoadState(bundleId, LoadState.Loading); WaitTasks.Enqueue(CreateMainTask(assetLowerId, bundleId)); } else { #if DEBUG m_countChian = 0; #endif PushChianTask(bundleId, dependInfo, task); } } void OnLoaded() { callback(Buffer.GetValue(bundleId)); Buffer.As <BundleBuffer>().IncreaseRef(bundleId); } void OnLoading() { Callbcker.AddCallback(bundleId, callback); } }