示例#1
0
 public bool GetAssetBundle(IgnoreCaseString key, out GameAssetBundle ab)
 {
     if (_assetbundleDictionary != null && _assetbundleDictionary.TryGetValue(key, out ab))
     {
         return(true);
     }
     ab = default(GameAssetBundle);
     return(false);
 }
示例#2
0
 public bool UpdateAssetBundle(IgnoreCaseString assetbundlename, ref GameAssetBundle ab)
 {
     if (_assetbundleDictionary != null)
     {
         _assetbundleDictionary[assetbundlename] = ab;
         return(true);
     }
     return(false);
 }
示例#3
0
        protected override void UnLoad(ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle, ref bool retcode)
        {
            Scene scene = SceneManager.GetSceneByPath(task.AssetPath);

            if (scene.IsValid() && scene.isLoaded)
            {
                if (task.IsAsync())
                {
                    if (gameAssetBundle.UnloadSceneRequest == null)
                    {
                        gameAssetBundle.UnloadSceneRequest = SceneManager.UnloadSceneAsync(task.AssetPath);
                    }
                    else if (gameAssetBundle.UnloadSceneRequest != null)
                    {
                        if (gameAssetBundle.UnloadSceneRequest.progress.SimpleEqual(0.9f))
                        {
                            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
                {
#pragma warning disable
                    if (SceneManager.UnloadScene(task.AssetPath))
                    {
                        Debug.LogFormat("UnLoadScene  :{0} Success", task.AssetPath);
                    }
#pragma warning restore
                    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
            {
                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;
            }
        }
示例#4
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);
        }
示例#5
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);
                        }
                    }
                }
            }
        }
示例#6
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);
 }
 protected virtual bool IsEnableLoadAssetBunlde(ref AssetBundleContext context, ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle)
 {
     return(true);
 }
 protected virtual void PreLoad(ref AssetBundleContext context, ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle, ref bool retcode, bool mainloadrequest = false)
 {
     this.Load(ref context, ref task, ref gameAssetBundle, ref retcode, mainloadrequest);
 }
 protected virtual void AfterLoadAssetBundle(ref AssetBundleContext context, ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle, ref bool retcode, ref bool mainloadrequest)
 {
     if (gameAssetBundle.AssetBundle == null)
     {
         gameAssetBundle.AssetStatus |= AssetBundleStatus.NotInMemory;
     }
     else if (!mainloadrequest)//作为依赖
     {
         if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
         {
             Debug.LogFormat("dependency :{0} is Done", task);
         }
         gameAssetBundle.AssetStatus |= AssetBundleStatus.InMemory;
     }
     else
     {
         LoadMainAsset(ref context, ref task, ref gameAssetBundle, ref retcode);
     }
 }
 protected virtual void UnLoad(ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle, ref bool retcode)
 {
 }
        protected virtual void Load(ref AssetBundleContext context, ref AssetBundleTask task,
                                    ref GameAssetBundle gameAssetBundle, ref bool retcode, bool mainloadrequest = false)
        {
            if (gameAssetBundle.IsException())
            {
                return;
            }

            if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
            {
                Debug.LogFormat("Load --- {0}", task);
            }

            gameAssetBundle.AssetStatus |= AssetBundleStatus.Loading;
            //not loaded
            if (gameAssetBundle.AssetBundle == null && gameAssetBundle.LoadRequest == null && gameAssetBundle.LoadAssetRequest == null)
            {
                if (IsEnableLoadAssetBunlde(ref context, ref task, ref gameAssetBundle))
                {
                    string assetfilename = AssetBundleConfig.Convert(task.AssetBundleName);
                    string path          = AssetBundleHelper.GetBundlePersistentPath(assetfilename);
                    bool   fileExist     = File.Exists(path);

                    string url;
                    bool   indownloadcache = context.Cache.TryGetDownloadUrl(new IgnoreCaseString(task.AssetBundleName), out url);
                    bool   canload         = false;
                    if (indownloadcache && !fileExist)
                    {
                        AssetDownloadInfo downloadInfo = new AssetDownloadInfo();
                        downloadInfo.AssetPath       = task.AssetPath;
                        downloadInfo.AssetBundleName = task.AssetBundleName;
                        if (!context.DownLoadQueue.Contains(downloadInfo))
                        {
                            downloadInfo.DstPath = path;
                            downloadInfo.Url     = url;
                            downloadInfo.TaskId  = task.TaskId;
                            context.DownLoadQueue.Add(downloadInfo);
                        }
                    }
                    else if (fileExist)
                    {
                        canload = true;
                    }
                    else if (!fileExist)
                    {
                        string streampath = AssetBundleHelper.GetBundleStreamPath(assetfilename);
                        if (File.Exists(streampath))
                        {
                            canload = true;
                            path    = streampath;
                        }
                    }

                    if (canload)
                    {
                        if (task.IsAsync())
                        {
                            gameAssetBundle.LoadRequest = AssetBundle.LoadFromFileAsync(path);
                            if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                            {
                                Debug.LogFormat("LoadFromFileAsync AssetBundle :{0} ", task.AssetBundleName);
                            }
                        }
                        else
                        {
                            gameAssetBundle.AssetBundle = AssetBundle.LoadFromFile(path);
                            if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                            {
                                Debug.LogFormat("LoadFromFile AssetBundle :{0} ", task.AssetBundleName);
                            }

                            AfterLoadAssetBundle(ref context, ref task, ref gameAssetBundle, ref retcode, ref mainloadrequest);
                        }
                    }
                    else if (!indownloadcache)
                    {
                        gameAssetBundle.AssetStatus |= AssetBundleStatus.FileNotExist;
                        AddException(ref context, task.AssetPath);
                        Debug.LogError(string.Format("cant load :{0}", task));
                    }
                }
            }
            else if (gameAssetBundle.LoadRequest != null)// in asyncing Load AssetBundle
            {
                if (gameAssetBundle.LoadRequest.isDone)
                {
                    if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                    {
                        Debug.LogFormat("Async Task for Load AssetBundle is Done :{0} ", task);
                    }
                    gameAssetBundle.AssetBundle = gameAssetBundle.LoadRequest.assetBundle;

                    AfterLoadAssetBundle(ref context, ref task, ref gameAssetBundle, ref retcode, ref mainloadrequest);
                    gameAssetBundle.LoadRequest = null;
                }
            }
            else if (gameAssetBundle.LoadAssetRequest != null && mainloadrequest)// Load Asset
            {
                if (gameAssetBundle.LoadAssetRequest.isDone)
                {
                    var loadfinishasset = gameAssetBundle.LoadAssetRequest.asset;

                    if (loadfinishasset != null)
                    {
                        if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                        {
                            Debug.LogFormat("Async Task Add Asset :{0} ", task);
                        }
                        gameAssetBundle.AddAsset(task.AssetPath, gameAssetBundle.LoadAssetRequest.asset);
                        gameAssetBundle.AssetStatus |= AssetBundleStatus.InMemory;
                        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
                    {
                        Debug.LogErrorFormat("{0} has 0 Assets", gameAssetBundle.AssetBundle);
                        gameAssetBundle.AssetStatus |= AssetBundleStatus.NotInMemory;
                    }

                    gameAssetBundle.LoadAssetRequest = null;
                }
            }
            else
            {
                if (mainloadrequest)
                {
                    LoadMainAsset(ref context, ref task, ref gameAssetBundle, ref retcode);
                }
            }
        }
        //Load Main Asset
        protected virtual void LoadMainAsset(ref AssetBundleContext context, ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle, ref bool retcode)
        {
            if (!gameAssetBundle.ContainsAsset(task.AssetPath))
            {
                if (task.IsAsync())
                {
                    //Load Async Asset

                    if (task.AssetInfo.AssetResType == AssetBundleResType.Image)
                    {
                        gameAssetBundle.LoadAssetRequest = gameAssetBundle.AssetBundle.LoadAssetAsync <Sprite>(task.AssetInfo.AssetName);
                        if (gameAssetBundle.LoadAssetRequest == null || gameAssetBundle.LoadAssetRequest.asset == null)
                        {
                            gameAssetBundle.LoadAssetRequest = gameAssetBundle.AssetBundle.LoadAssetAsync(task.AssetInfo.AssetName);
                        }
                    }
                    else
                    {
                        gameAssetBundle.LoadAssetRequest = gameAssetBundle.AssetBundle.LoadAssetAsync(task.AssetInfo.AssetName);
                    }

                    if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                    {
                        Debug.LogFormat("Load Async Asset :{0}", task);
                    }
                }
                else
                {
                    //Load Asset
                    Object asset = null;
                    if (task.AssetInfo.AssetResType == AssetBundleResType.Image)
                    {
                        asset = gameAssetBundle.AssetBundle.LoadAsset <Sprite>(task.AssetInfo.AssetName);
                        if (asset == null)
                        {
                            asset = gameAssetBundle.AssetBundle.LoadAsset(task.AssetInfo.AssetName);
                        }
                    }
                    else
                    {
                        asset = gameAssetBundle.AssetBundle.LoadAsset(task.AssetInfo.AssetName);
                    }
                    // gameAssetBundle.AssetBundle.LoadAsset(task.AssetInfo.AssetName);

                    if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                    {
                        Debug.LogFormat("Load Sync Asset :{0}", task);
                    }

                    if (asset != null)
                    {
                        gameAssetBundle.AddAsset(task.AssetPath, asset);
                        if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                        {
                            Debug.LogFormat("Add Asset :{0} ", task);
                        }
                        gameAssetBundle.AssetStatus |= AssetBundleStatus.InMemory;

                        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
                    {
                        Debug.LogErrorFormat("{0} cant load Asset", gameAssetBundle.AssetBundle);
                        gameAssetBundle.AssetStatus |= AssetBundleStatus.NotInMemory;
                    }
                }
            }
            else
            {
                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;
            }
        }
示例#13
0
 public bool GetAssetBundle(string key, out GameAssetBundle ab)
 {
     return(GetAssetBundle(new IgnoreCaseString(key), out ab));
 }
示例#14
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);
        }
 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 IsEnableLoadAssetBunlde(ref AssetBundleContext context, ref AssetBundleTask task, ref GameAssetBundle gameAssetBundle)
 {
     return(task.FreeSize || _size < Mathf.Max(1, AssetBundleConfig.TaskLoadAssetLimit));
 }
示例#17
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;
            }
        }
示例#18
0
        public void AddObjectRef(ref GameAssetBundle gameAssetBundle)
        {
            if (ObjectReferences == null)
            {
                ObjectReferences = ListPool <string> .Get();
            }

            if (RefHistories == null)
            {
                RefHistories = ListPool <UserRefHistory> .Get();
            }

            if (AssetsList == null)
            {
                AssetsList = ListPool <AssetInfo> .Get();
            }

            AssetBundlename = gameAssetBundle.AssetBundleInfo.AssetBundleName;
            AssetBundleSize = gameAssetBundle.AssetBundleInfo.AssetSize;
            Status          = gameAssetBundle.AssetStatus;
            UserRefCount    = gameAssetBundle.UserReference;

            if (gameAssetBundle.References != null)
            {
                foreach (var weakobj in gameAssetBundle.References)
                {
                    if (weakobj != null && weakobj.IsAlive)
                    {
                        ObjectReferences.Add(GetPath(weakobj.Target as Object));
                    }
                }
            }

            if (gameAssetBundle.loadedAssets != null)
            {
                foreach (var item in gameAssetBundle.loadedAssets)
                {
                    var asset = item.Value;
                    if (asset != null)
                    {
                        long size = Profiler.GetRuntimeMemorySizeLong(asset);
                        LoadedSize += size;

                        AssetInfo info = new AssetInfo();
                        info.Size = size;
                        info.Path = GetPath(asset);

                        AssetsList.Add(info);
                    }
                }
            }

#if UNITY_EDITOR
            if (gameAssetBundle.history != null)
            {
                foreach (var historydata  in gameAssetBundle.history)
                {
                    RefHistories.Add(historydata);
                }
            }
#endif
        }
示例#19
0
 public bool UpdateAssetBundle(string assetbundlename, ref GameAssetBundle ab)
 {
     return(UpdateAssetBundle(new IgnoreCaseString(assetbundlename), ref ab));
 }