Пример #1
0
        void ExitScene(Scene scene)
        {
            if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
            {
                Debug.LogFormat("ExitScene {0} at {1}", scene.name, Time.frameCount);
            }

            AssetBundleFeature assetfeature = Parent as AssetBundleFeature;

            if (assetfeature != null)
            {
                var             context = assetfeature.StartRead();
                AssetBundleInfo info;
                if (context.Cache.TryGetInfo(scene.path, out info))
                {
                    GameAssetBundle gameAssetBundle;
                    if (context.Cache.GetAssetBundle(info.AssetBundleName, out gameAssetBundle))
                    {
                        AssetBundleFunction.RemoveRef(ref context, ref gameAssetBundle);
                        assetfeature.EndRead(ref context);
                    }
                }
            }
            else
            {
                Debug.LogError("assetfeature is Null");
            }
        }
Пример #2
0
        protected override void LoadMainAsset(ref AssetBundleContext context, ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle, ref bool retcode)
        {
            if (task.IsAsync())
            {
                if (gameAssetBundle.SceneRequest == null)//准备加载场景
                {
                    gameAssetBundle.SceneRequest = SceneManager.LoadSceneAsync(task.AssetPath, task.LoadSceneMode);
                    if (gameAssetBundle.SceneRequest != null)
                    {
                        gameAssetBundle.SceneRequest.allowSceneActivation = false;
                    }

                    if (task.Result.ProgresCallback != null)
                    {
                        ProgressArgs progressArgs = new ProgressArgs(0, 0, task.AssetPath, true);
                        task.Result.ProgresCallback(ref progressArgs);
                    }
                }
                else if (gameAssetBundle.SceneRequest != null)//检查加载场景的状态
                {
                    if (Mathf.Abs(gameAssetBundle.SceneRequest.progress - 0.9f) < 0.001f)
                    {
                        gameAssetBundle.AssetStatus |= AssetBundleStatus.InMemory;
                        //暂时不释放
                        AssetBundleFunction.AddRef(ref context, ref gameAssetBundle, task.AssetPath);

                        task.Result.scene = gameAssetBundle.SceneRequest;

                        gameAssetBundle.SceneRequest = null;

                        if (task.Result.ProgresCallback != null)
                        {
                            ProgressArgs progressArgs = new ProgressArgs(1, 0, task.AssetPath, true);
                            task.Result.ProgresCallback(ref progressArgs);
                        }
                        task.FinishTime = Time.realtimeSinceStartup;
                        retcode         = true;
                    }
                }
            }
            else
            {
                SceneManager.LoadScene(task.AssetPath, task.LoadSceneMode);
                gameAssetBundle.AssetStatus |= AssetBundleStatus.InMemory;
                AssetBundleFunction.AddRef(ref context, ref gameAssetBundle, task.AssetPath);

                if (task.Result.ProgresCallback != null)
                {
                    ProgressArgs progressArgs = new ProgressArgs(1, 0, task.AssetPath, true);
                    task.Result.ProgresCallback(ref progressArgs);
                }
                task.FinishTime = Time.realtimeSinceStartup;
                retcode         = true;
            }
        }
Пример #3
0
        public bool IsDone(ref AssetBundleContext context, out GameAssetBundle assetBundle)
        {
            if (context.Cache.GetAssetBundle(AssetBundleName, out assetBundle))
            {
                //异步请求还未完成
                if (assetBundle.LoadRequest != null || assetBundle.LoadAssetRequest != null ||
                    assetBundle.SceneRequest != null || assetBundle.UnloadSceneRequest != null)
                {
                    return(false);
                }

                if (AssetBundleConfig.IsSimulator())
                {
                    return(true);
                }

                ///被预加载pin住的
                if (IsPreLoad() && PinTime > 0)
                {
                    if (FinishTime > CreateTime)
                    {
                        return(Time.realtimeSinceStartup - FinishTime > PinTime);
                    }
                    return(false);
                }
                //正在下载的
                if (AssetBundleFunction.InDowloading(ref context, ref this))
                {
                    return(false);
                }
                //加载异常
                if (AssetBundleConfig.SafeMode && assetBundle.IsException())
                {
                    return(true);
                }
                else if (assetBundle.AssetStatus.HasEnum(AssetBundleStatus.InMemory))  //加载正常
                {
                    int finish;
                    int total;
                    return(IsSubAllDone(ref context, out finish, out total));
                }
                //else if(assetBundle.AssetStatus == AssetBundleStatus.None)//啥事也没干的task
                //{
                //    return true;
                //}
            }
            else
            {
                return(true);
            }
            return(false);
        }
        protected virtual void TryDoTask(ref AssetBundleContext context)
        {
            int loadcnt = Mathf.Max(1, AssetBundleConfig.TaskStep);
            int loaded  = 0;

            for (int i = 0; i < context.Tasks.Count; i++)
            {
                var task = context.Tasks[i];
                if (IsEnableTask(ref context, ref task))
                {
                    bool changed       = true;
                    bool indownloading = AssetBundleFunction.InDowloading(ref context, ref task);
                    if (indownloading)
                    {
                        changed = false;
                    }
                    else
                    {
                        try
                        {
                            changed = LoadAssetBundle(ref context, ref task);
                        }
                        catch (Exception e)
                        {
                            Debug.LogError(e);

                            changed = true;
                            AddException(ref context, task.AssetPath);
                        }
                    }


                    if (changed)
                    {
                        context.Tasks[i] = task;
                    }

                    if (task.IsPreLoad() || indownloading)//ignore
                    {
                        continue;
                    }
                    else//真实加载的才被记录
                    {
                        loaded++;
                        if (loaded >= loadcnt)
                        {
                            break;
                        }
                    }
                }
            }
        }
        bool DispatcherTask(ref AssetBundleContext context, ref AssetBundleTask task)
        {
            GameAssetBundle gameAssetBundle;

            if (task.IsDone(ref context, out gameAssetBundle))
            {
                //set or not ?
                //task.FinishTime = Time.realtimeSinceStartup;
                task.Result.Result      = !gameAssetBundle.IsException();
                task.Result.LoadObject  = gameAssetBundle.LoadAsset(task.AssetPath);
                task.Result.RuntimeInfo = task.AssetInfo;
                //因为这里只改边list 中的task存储内容,不涉及context 非reference的修改,所以没有关系
                task.Result.UpdateReferenceAction = AddRefrence;

                //notify
                if (task.Result.ResultCallback != null)
                {
                    task.Result.ResultCallback(ref task.Result);
                }
                else if (task.IsPreLoad())
                {
                    if (task.PinTime < 0)
                    {
                        AssetBundleFunction.AddRef(ref context, ref gameAssetBundle, task.AssetPath);
#if UNITY_EDITOR
                        context.Cache.UpdateAssetBundle(task.AssetBundleName, ref gameAssetBundle);
#endif
                    }
                }
                else if (task.TaskResType == TaskResType.GameObject)
                {
                    task.Result.Instantiate();
                }
                else if (task.TaskResType == TaskResType.Scene)
                {
                    if (!task.Result.Hide)
                    {
                        task.Result.LoadScene();
                    }
                }


                Debug.LogFormat("DisposeTask : {0} dispose", task);
                DisposeTask(ref task);
                return(true);
            }

            return(false);
        }
        void AddRefrence(ref AssetBundleInfo assetbundleinfo, Object target)
        {
            AssetBundleFeature assetfeature = Parent as AssetBundleFeature;

            if (assetfeature != null)
            {
                var context = assetfeature.StartRead();
                AssetBundleFunction.AddAssetBundleRef(ref context, ref assetbundleinfo, assetbundleinfo.AssetBundleName, target);

                assetfeature.EndRead(ref context);
            }
            else
            {
                Debug.LogError("assetfeature is Null");
            }
        }
        public FrameProfilerData GetLastestData()
        {
            if (Datas == null)
            {
                FrameProfilerData data = new FrameProfilerData();
                data.Frame = -1;
                return(data);
            }
            else if (DataCount >= Datas.Length)
            {
                var realoffset = AssetBundleFunction.GetRingOffset(Offset - 1, 0, Datas.Length);
                return(Datas[realoffset]);
            }

            if (Offset == 0)
            {
                FrameProfilerData data = new FrameProfilerData();
                data.Frame = -1;
                return(data);
            }
            return(Datas[Offset - 1]);
        }