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);
            }
        }
Exemplo n.º 2
0
        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();
                }
            }
        }