Exemplo n.º 1
0
        public static ProjectBundleDependInfo Create(string manifestPath)
        {
            var bundle        = AssetBundle.LoadFromFile(manifestPath);
            var manifest      = bundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest");
            var appDependInfo = new ProjectBundleDependInfo();
            var allBundleIds  = manifest.GetAllAssetBundles();

            foreach (var bundleId in allBundleIds)
            {
                var directDepends = manifest.GetDirectDependencies(bundleId);
                var dependInfo    = new BundleDependInfo(bundleId, directDepends);
                appDependInfo.AllDependInfos.Add(bundleId, dependInfo);
            }

            bundle.Unload(true);
            return(appDependInfo);
        }
Exemplo n.º 2
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
        }
Exemplo n.º 3
0
        public IBundleRef Load(string assetId)
        {
            var assetLowerId = assetId.ToLower();
            var bundleId     = BundlePathInfoHelepr.GetBundleId(assetLowerId);
            var loadState    = GetLoadState(bundleId);

            switch (loadState)
            {
            case LoadState.NotLoad:
                return(OnNotLoad());

            case LoadState.Loaded:
                return(OnLoaded());

            case LoadState.Loading:
                //Todo 待完成
                //Debug.LogError("错误,在异步加载中,调用了词资源的同步加载");
                return(OnNotLoad());

            default:
                throw new ArgumentOutOfRangeException();
            }

            IBundleRef OnNotLoad()
            {
                var dependInfo = DependInfoHelper.GetDependInfo(bundleId);

                if (dependInfo == null)
                {
                    dependInfo = new BundleDependInfo(bundleId, new string[0]);
                }
        #if DEBUG
                m_countChian = 0;
        #endif

                return(LoadBundleChian(bundleId, dependInfo));
            }

            IBundleRef OnLoaded()
            {
                var bundleRef = Buffer.GetValue(bundleId);

                return(bundleRef);
            }

            IBundleRef LoadBundleChian(string initiatedBundleId, BundleDependInfo dependInfo)
            {
        #if DEBUG
                m_countChian++;
                if (m_countChian > 100)
                {
                    Debug.LogError("严重错误:同步加载堆栈超过100,请检查加载逻辑或资源依赖关系:" + initiatedBundleId);
                    return(null);
                }
        #endif

                var dependIds   = dependInfo.DirectDepends;
                var dependCount = dependIds.Count;

                var        loadState2 = GetLoadState(dependInfo.BundleId);
                IBundleRef bundleRef2;
                if (loadState2 == LoadState.Loaded)
                {
                    bundleRef2 = Buffer.GetValue(dependInfo.BundleId);
                    bundleRef2.Use();
                    return(bundleRef2);
                }
                else if (loadState2 == LoadState.AddTask)
                {
                    return(null);
                }
                SetLoadState(dependInfo.BundleId, LoadState.AddTask);

                for (int index = 0; index < dependCount; index++)
                {
                    var sonId = dependIds[index];

                    var sonDepndInfo = DependInfoHelper.GetDependInfo(sonId);
                    if (sonDepndInfo.DirectDepends.Count > 0)
                    {
                        LoadBundleChian(initiatedBundleId, sonDepndInfo);
                    }
                    else
                    {
                        loadState2 = GetLoadState(sonId);
                        IBundleRef bundleRef;
                        if (loadState2 != LoadState.NotLoad)
                        {
                            if (loadState2 == LoadState.Loaded)
                            {
                                bundleRef = Buffer.GetValue(sonId);
                                bundleRef.Use();
                                continue;
                            }
                            else if (loadState2 == LoadState.AddTask)
                            {
                                continue;
                            }
                        }

                        var bundlePath = BundlePathInfoHelepr.GetBundlePath(sonId);

        #if UNITY_EDITOR
                        ////AnalyzeProfiler.SaveStartLoad(sonId);
        #endif

                        var assetBundle = AssetBundle.LoadFromFile(bundlePath);
                        bundleRef = new BundleRef();
                        if (assetBundle != null)
                        {
                            bundleRef.Update(assetBundle, OnBundleRelease);
                            bundleRef.Use();
                            Buffer.TryCache(sonId, bundleRef);
                            SetLoadState(sonId, LoadState.Loaded);
                        }
                        else
                        {
                            SetLoadState(sonId, LoadState.NotLoad);
                        }

        #if UNITY_EDITOR
                        ////AnalyzeProfiler.AddBundleToBundleRef(sonId, dependInfo.BundleId);
        #endif
                    }
                }

                loadState2 = GetLoadState(dependInfo.BundleId);
                if (loadState2 == LoadState.Loaded)          //Todo 如果在异步加载中应该怎么设计?
                {
                    bundleRef2 = Buffer.GetValue(dependInfo.BundleId);
                    bundleRef2.Use();
                    return(bundleRef2);
                }
                var bundlePath2 = BundlePathInfoHelepr.GetBundlePath(dependInfo.BundleId);
        #if UNITY_EDITOR
                ////AnalyzeProfiler.SaveStartLoad(dependInfo.BundleId);
        #endif
                var assetBundle2 = AssetBundle.LoadFromFile(bundlePath2);
                bundleRef2 = new BundleRef();
                if (assetBundle2 != null)
                {
                    bundleRef2.Update(assetBundle2, OnBundleRelease);
                    bundleRef2.Use();
                    Buffer.TryCache(dependInfo.BundleId, bundleRef2);
                    SetLoadState(dependInfo.BundleId, LoadState.Loaded);
                }
                else
                {
                    SetLoadState(dependInfo.BundleId, LoadState.NotLoad);
                    return(null);
                }

        #if UNITY_EDITOR
                ////AnalyzeProfiler.AddBundleToBundleRef(initiatedBundleId, dependInfo.BundleId);
        #endif
                return(bundleRef2);
            }
        }
 public BundleDependInfoVisual(BundleDependInfo dependInfo)
 {
     DirectDepends = dependInfo.DirectDepends;
 }