internal void Detach()
 {
     _detached = true;
     while (WaitTasks.Count > 0)
     {
         WaitTasks.First().Terminate(new Exception("waitForFunction failed: frame got detached."));
     }
 }
 protected virtual void LoadUpdate()
 {
     while (WaitTasks.Count > 0)
     {
         var task = WaitTasks.Dequeue();
         StartOneLoadAsync(task);
     }
 }
        public void LoadAsync <TAsset>(string assetId, Action <TAsset> callback) where TAsset : Object
        {
            if (_isLoadBundle)
            {
                if (Buffer.HasValue(assetId))
                {
                    callback(Buffer.GetValue(assetId) as TAsset);
                    return;
                }

                var task = TakeTask();
                task.AssetId   = assetId;
                task.AssetType = typeof(TAsset);
                task.Callback  = (asset) => { callback?.Invoke(asset as TAsset); };
                WaitTasks.Enqueue(task);
            }
            else
            {
                string path  = GetAssetPath(assetId);
                var    asset = AssetDatabaseUtility.LoadAssetAtPath <TAsset>(path);
                callback(asset);
            }
        }
Beispiel #4
0
        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
        }
Beispiel #5
0
        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);
            }
        }