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(); } } }