示例#1
0
        LoadReturnArgs LoadAssetScene(ref SceneLoadArgs args, AssetBundleTaskType type = AssetBundleTaskType.LoadAsset | AssetBundleTaskType.Sync)
        {
            if (_assetBundleFeature != null)
            {
                AssetBundleContext context = _assetBundleFeature.StartRead();

                AssetBundleInfo info;
                GameAssetBundle gameAssetBundle;
                if (CanLoad(ref context, args.AssetPath, out info, out gameAssetBundle))
                {
                    AssetBundleTask task = new AssetBundleTask();
                    task.AssetInfo     = info;
                    task.TaskLoadType  = type;
                    task.TaskId        = ++_taskid;
                    task.TaskResType   = TaskResType.Scene;
                    task.Priority      = args.Priority;
                    task.CreateTime    = Time.realtimeSinceStartup;
                    task.LoadSceneMode = args.SceneMode;

                    context.AddTask(ref task);
                    _assetBundleFeature.EndRead(ref context);

                    LoadReturnArgs returnArgs = new LoadReturnArgs(this._assetBundleFeature, task.TaskId);
                    return(returnArgs);
                }
            }
            return(default(LoadReturnArgs));
        }
示例#2
0
        LoadReturnArgs LoadCommonAsset(ref CommonLoadArgs args, AssetBundleTaskType type = AssetBundleTaskType.Sync | AssetBundleTaskType.LoadAsset)
        {
            if (_assetBundleFeature != null)
            {
                AssetBundleContext context = _assetBundleFeature.StartRead();

                AssetBundleInfo info;
                GameAssetBundle gameAssetBundle;
                if (CanLoad(ref context, args.AssetPath, out info, out gameAssetBundle))
                {
                    AssetBundleTask task = new AssetBundleTask();
                    //
                    task.AssetInfo    = info;
                    task.TaskLoadType = type;
                    task.TaskId       = ++_taskid;
                    task.TaskResType  = TaskResType.Common;
                    task.Priority     = args.Priority;
                    task.CreateTime   = Time.realtimeSinceStartup;

                    //
                    Debug.LogFormat("Task ---- {0} Created", task);

                    context.AddTask(ref task);
                    _assetBundleFeature.EndRead(ref context);

                    LoadReturnArgs returnArgs = new LoadReturnArgs(this._assetBundleFeature, task.TaskId);
                    return(returnArgs);
                }
            }

            return(default(LoadReturnArgs));
        }
        public HashSet <KeyValuePair <IgnoreCaseString, GameAssetBundle> > Record(ref AssetBundleContext data, ref bool force, Dictionary <IgnoreCaseString, GameAssetBundle> targets)
        {
            HashSet <KeyValuePair <IgnoreCaseString, GameAssetBundle> > result = null;

            foreach (var pair in targets)
            {
                var  gameAssetBundle = pair.Value;
                bool inmemory        = gameAssetBundle.AssetStatus.HasEnum(AssetBundleStatus.InMemory);
                bool canUnload       = gameAssetBundle.CanUnLoad;
                bool nointask        = !InTask(ref data, ref gameAssetBundle);
                bool noinscene       = !InScene(ref data, ref gameAssetBundle);
                bool hasactivescene  = HasShowScene();

                if (force ||
                    (inmemory && canUnload && nointask && noinscene && hasactivescene))
                {
                    if (result == null)
                    {
                        result = HashSetPool <KeyValuePair <IgnoreCaseString, GameAssetBundle> > .Get();
                    }

                    KeyValuePair <IgnoreCaseString, GameAssetBundle> tuple = new KeyValuePair <IgnoreCaseString, GameAssetBundle>(pair.Key, gameAssetBundle);

                    result.Add(tuple);
                }
            }
            return(result);
        }
示例#4
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;
            }
        }
示例#5
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);
        }
 bool InTask(ref AssetBundleContext context, ref GameAssetBundle gameAssetBundle)
 {
     if (context.Tasks != null)
     {
         for (int i = 0; i < context.Tasks.Count; i++)
         {
             var task = context.Tasks[i];
             if (InTask(ref gameAssetBundle, ref task))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        public void Run(ref AssetBundleContext context, AssetEvent eEvent)
        {
            bool force = eEvent == AssetEvent.Destroy;
            //简单点,暂时不准备做更精细的task 依赖比较,可能会cost 更多的时间,但是很多时候并不需要这么精确
            bool inLoading = context.Tasks == null ? false : context.Tasks.Count > 0;

            if (!inLoading || force)
            {
                var result = Record(ref context, ref force, context.Cache.GetAllAssetBundle());

                if (result != null && result.Count > 0)
                {
                    UnLoad(result, ref context);
                }
            }
        }
示例#8
0
 public bool IsSubAllDone(ref AssetBundleContext context, out int finishcount, out int total)
 {
     finishcount = 0;
     total       = 0;
     if (SubTasks != null)
     {
         for (int i = 0; i < SubTasks.Count; i++)
         {
             var subtask = SubTasks[i];
             if (!SubAllDone(ref context, ref subtask, ref finishcount, ref total))
             {
                 return(false);
             }
         }
     }
     return(true);
 }
示例#9
0
        //public void UnLoadAsset(string assetpath)
        //{
        //    if(_assetBundleFeature != null)
        //    {
        //        var context = _assetBundleFeature.StartRead();

        //        AssetBundleInfo info;
        //        GameAssetBundle gameAssetBundle;
        //        if(context.Cache.TryGetInfo(assetpath,out info) && context.Cache.GetAssetBundle(info.AssetName,out gameAssetBundle))
        //        {

        //            //gameAssetBundle.RemoveUserRef();
        //            context.Cache.UpdateAssetBundle(info.AssetBundleName, ref gameAssetBundle);
        //        }

        //        _assetBundleFeature.EndRead(ref context);
        //    }
        //}

        //public void UnLoadAsset(Object obj)
        //{
        //    if (_assetBundleFeature != null)
        //    {
        //        var context = _assetBundleFeature.StartRead();

        //        bool contains = false;
        //        GameAssetBundle tempbundle = default(GameAssetBundle);
        //        foreach(var gameassetbundle in context.Cache.GetAllAssetBundle())
        //        {
        //            if(gameassetbundle.Value.Contains(obj))
        //            {
        //                tempbundle = gameassetbundle.Value;
        //                contains = true;
        //                break;
        //            }
        //        }

        //        if(contains)
        //        {
        //            //tempbundle.RemoveUserRef();
        //            context.Cache.UpdateAssetBundle(tempbundle.AssetBundleInfo.AssetBundleName,ref tempbundle);
        //        }

        //        _assetBundleFeature.EndRead(ref context);
        //    }
        //}

        /// <summary>
        /// 这里会检查是否包含assetbundle info ,是否已经加载,是否是异常资源
        /// </summary>
        internal bool CanLoad(ref AssetBundleContext context, string assetpath, out AssetBundleInfo info, out GameAssetBundle assetbundle)
        {
            if (context.Cache.CanLoad(assetpath, out info))
            {
                if (!context.Cache.GetAssetBundle(info.AssetBundleName, out assetbundle))
                {
                    assetbundle.AssetBundleInfo = info;
                    context.Cache.UpdateAssetBundle(info.AssetBundleName, ref assetbundle);
                }
                return(true);
            }
            else
            {
                Debug.LogErrorFormat("{0} can't Load ", assetpath);
            }

            assetbundle = default(GameAssetBundle);
            return(false);
        }
示例#10
0
 public bool IsStatus(ref AssetBundleContext context, out GameAssetBundle assetBundle, AssetBundleStatus status)
 {
     if (context.Cache.GetAssetBundle(AssetBundleName, out assetBundle))
     {
         if (assetBundle.AssetStatus.ContainsEnum(status))
         {
             return(true);
         }
         else if (SubTasks != null && SubTasks.Count > 0)
         {
             for (int i = 0; i < SubTasks.Count; i++)
             {
                 if (SubTasks[i].IsStatus(ref context, status))
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
示例#11
0
        internal static void AddRef(ref AssetBundleContext context, ref GameAssetBundle gameAssetBundle, string rootpath)
        {
            gameAssetBundle.AddUserRef(rootpath);

            context.Cache.UpdateAssetBundle(gameAssetBundle.AssetBundleInfo.AssetBundleName, ref gameAssetBundle);

            if (gameAssetBundle.AssetBundleInfo.DepAssetBundleNames != null)
            {
                foreach (var depname in gameAssetBundle.AssetBundleInfo.DepAssetBundleNames)
                {
                    if (!string.IsNullOrEmpty(depname))
                    {
                        GameAssetBundle depassetbundle;
                        if (context.Cache.GetAssetBundle(depname, out depassetbundle))
                        {
                            AddRef(ref context, ref depassetbundle, rootpath);
                        }
                    }
                }
            }
        }
        bool InScene(ref AssetBundleContext context, ref GameAssetBundle gameAssetBundle)
        {
            if (gameAssetBundle.AssetBundleInfo.AssetResType == AssetBundleResType.Scene)
            {
                var allassets = context.Cache.GetPathsInSceneAsset(gameAssetBundle.AssetBundleInfo.AssetBundleName);

                if (allassets != null && allassets.Count > 0)
                {
#pragma warning disable
                    var allscenes = SceneManager.GetAllScenes();
#pragma warning restore
                    foreach (var scene in allscenes)
                    {
                        if (allassets.Contains(scene.path))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
示例#13
0
        private static bool SubAllDone(ref AssetBundleContext context, ref AssetBundleTask task, ref int finishcount, ref int total)
        {
            total++;
            if (task.SubTasks != null)
            {
                for (int i = 0; i < task.SubTasks.Count; i++)
                {
                    var subtask = task.SubTasks[i];
                    if (!SubAllDone(ref context, ref subtask, ref finishcount, ref total))
                    {
                        return(false);
                    }
                }
            }

            GameAssetBundle gameAssetBundle;

            if (context.Cache.GetAssetBundle(task.AssetBundleName, out gameAssetBundle))
            {
                if (AssetBundleConfig.SafeMode && gameAssetBundle.IsException())
                {
                    return(false);
                }
                else if (gameAssetBundle.AssetStatus.HasEnum(AssetBundleStatus.InMemory))
                {
                    finishcount++;
                    return(true);
                }
            }
#pragma warning disable
            else if (!AssetBundleConfig.SafeMode)
            {
                return(true);
            }
#pragma warning restore
            return(false);
        }
示例#14
0
        internal static bool InDowloading(ref AssetBundleContext context, ref AssetBundleTask task)
        {
            AssetDownloadInfo downloadInfo = new AssetDownloadInfo();

            downloadInfo.AssetBundleName = task.AssetBundleName;
            if (context.DownLoadQueue.Contains(downloadInfo))
            {
                return(true);
            }

            if (task.SubTasks != null)
            {
                for (int i = 0; i < task.SubTasks.Count; i++)
                {
                    var subtask = task.SubTasks[i];
                    if (InDowloading(ref context, ref subtask))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
示例#15
0
        internal static void AddAssetBundleRef(ref AssetBundleContext context, ref AssetBundleInfo assetbundleinfo, string name, Object target)
        {
            GameAssetBundle gameAssetBundle;

            if (context.Cache.GetAssetBundle(name, out gameAssetBundle))
            {
                gameAssetBundle.AddReference(target);
                context.Cache.UpdateAssetBundle(name, ref gameAssetBundle);
            }

            if (assetbundleinfo.DepAssetBundleNames != null)
            {
                for (int i = 0; i < assetbundleinfo.DepAssetBundleNames.Length; i++)
                {
                    var             assetbundlename = assetbundleinfo.DepAssetBundleNames[i];
                    string          resname         = assetbundleinfo.DependNames[i];
                    AssetBundleInfo runtimedata;
                    if (!string.IsNullOrEmpty(assetbundlename) && context.Cache.TryGetInfo(resname, out runtimedata))
                    {
                        AddAssetBundleRef(ref context, ref runtimedata, assetbundlename, target);
                    }
                }
            }
        }
        public void UnLoad(HashSet <KeyValuePair <IgnoreCaseString, GameAssetBundle> > removeList, ref AssetBundleContext context)
        {
            foreach (var pair in removeList)
            {
                GameAssetBundle assetBundle = pair.Value;
                var             key         = pair.Key;
                if (assetBundle.AssetBundle != null)
                {
                    Debug.LogFormat("UnLoad {0}", assetBundle.AssetBundle);
                    //UnLoadAsset
                    var allassets = assetBundle.GetAllAssets();
                    if (allassets != null)
                    {
                        foreach (var kvAllasset in allassets)
                        {
                            var asset = kvAllasset.Value;
                            if (asset != null)
                            {
                                UnityEngine.Object.DestroyImmediate(asset, true);
                            }
                        }
                    }
                    //Unload Reference
                    if (assetBundle.References != null)
                    {
                        for (int i = 0; i < assetBundle.References.Count; i++)
                        {
                            var target = assetBundle.References[i];
                            if (target != null && target.IsAlive && target.Target != null && target.Target is UnityEngine.Object)
                            {
                                UnityEngine.Object.Destroy(target.Target as UnityEngine.Object);
                            }
                        }
                    }

                    assetBundle.AssetBundle.Unload(true);
                    assetBundle.Dispose();
                }

                context.Cache.RemoveAssetBundle(key);
            }


            if (removeList != null && removeList.Count > 0)
            {
                HashSetPool <KeyValuePair <IgnoreCaseString, GameAssetBundle> > .Release(removeList);

#if UNITY_EDITOR
                Debug.LogFormat("<color=#ff0a22ff>left AssetBundle Count:{0} at :{1}</color>", context.Cache.AssetBundleCount(), Time.frameCount);
#else
                Debug.LogFormat("left AssetBundle Count:{0} at :{1}", context.Cache.AssetBundleCount(), Time.frameCount);
#endif
                context.IsDestroying = false;
            }
        }
 protected override bool IsEnableLoadAssetBunlde(ref AssetBundleContext context, ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle)
 {
     return(task.FreeSize || _size < Mathf.Max(1, AssetBundleConfig.TaskLoadAssetLimit));
 }
 protected override void AfterLoadAssetBundle(ref AssetBundleContext context, ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle, ref bool retcode, ref bool mainloadrequest)
 {
     _size += gameAssetBundle.AssetBundleInfo.AssetSize;
     base.AfterLoadAssetBundle(ref context, ref task, ref gameAssetBundle, ref retcode, ref mainloadrequest);
 }
 protected override bool IsEnableTask(ref AssetBundleContext context, ref AssetBundleTask task)
 {
     return(task.TaskResType <= TaskResType.GameObject && base.IsEnableTask(ref context, ref task));
 }
示例#20
0
        public bool IsDone(ref AssetBundleContext context)
        {
            GameAssetBundle assetBundle;

            return(IsDone(ref context, out assetBundle));
        }
 protected override void TryDoTask(ref AssetBundleContext context)
 {
     _size = 0;
     base.TryDoTask(ref context);
 }
示例#22
0
        public bool IsStatus(ref AssetBundleContext context, AssetBundleStatus status)
        {
            GameAssetBundle assetBundle;

            return(IsStatus(ref context, out assetBundle, status));
        }