Example #1
0
        /// <summary>
        /// 异步加载资源统一回调
        /// </summary>
        /// <param name="LoadResult"></param>
        /// <param name="resInfo"></param>
        public void AsyncResCallBack(bool LoadResult, ResInfo resInfo)
        {
            AsyncCount -= 1;
            if (!LoadResult)
            {
                ResManager.Instance.RemoveLoadFailRes(resInfo.mCRC);
                resInfo.Recycle2Cache();
                mAsyncLoadingResDic[resInfo.ResPath].ForEach((resLoadInfo) =>
                {
                    resLoadInfo.mRealListener.InvokeGracefully(LoadResult, null);
                    resLoadInfo.Recycle2Cache();
                });
                mAsyncLoadingResDic.Remove(resInfo.ResPath);
                return;
            }
            bool DestroyCache = true;

            //异步加载回调
            mAsyncLoadingResDic[resInfo.ResPath].ForEach((resLoadInfo) =>
            {
                //但凡有一个调用此资源的说不销毁,就不销毁
                if (!resLoadInfo.DestroyCache)
                {
                    DestroyCache = false;
                }
                resInfo.Retain();
                resLoadInfo.mRealListener.InvokeGracefully(LoadResult, resInfo);
                resLoadInfo.Recycle2Cache();
            });
            mAsyncLoadingResDic.Remove(resInfo.ResPath);
            resInfo.DestroyCache = DestroyCache;
            resInfo.mGUID        = resInfo.ResObject.GetInstanceID();
            mResList.AddValue(resInfo.ResPath);
        }
Example #2
0
        public void LoadAsync(ResLoadInfo resLoadInfo)
        {
            if (resLoadInfo.mResPath == "")
            {
                AFLogger.e("资源路径为空,无法进行加载");
                return;
            }
            ResInfo resInfo = ResManager.Instance.GetRes(resLoadInfo);

            if (resInfo != null && resInfo.State == ResState.Ready && (resInfo.ResObject.IsNotNull() || resInfo.ResStr.IsNotNullAndEmpty()))
            {
                resLoadInfo.mListener.InvokeGracefully(true, resInfo);
            }
            else if (resInfo == null)
            {
                resInfo = ResFactory.Create(resLoadInfo);
                //加入队列
                if (resInfo.LoadAsync())
                {
                    ResManager.Instance.CacheResource(resLoadInfo.mResPath, resInfo);
                }
            }
            else
            {
                AFLogger.e("当前请求的资源正在加载,增加回调");
                //当前请求的资源正在加载,增加回调
                resInfo.RegisterListen(resLoadInfo.mListener);
            }
        }
Example #3
0
 public void CacheResource(uint CRC, ResInfo resInfo)
 {
     //缓存太多,清除最早没有使用的资源
     WashOut();
     resInfo.mGUID = resInfo.ResObject.GetInstanceID();
     mResDictionary.AddValue <uint, ResInfo>(CRC, resInfo);
 }
Example #4
0
        public ResInfo GetRes(uint crc)
        {
            ResInfo resInfo = null;

            mResDictionary.TryGetValue(crc, out resInfo);
            return(resInfo);
        }
Example #5
0
        /// <summary>
        /// 获取已经加载的资源
        /// </summary>
        /// <param name="resLoadInfo"></param>
        /// <returns></returns>
        public ResInfo GetRes(ResLoadInfo resLoadInfo)
        {
            ResInfo resInfo = null;

            mResDictionary.TryGetValue(resLoadInfo.mCRC, out resInfo);
            return(resInfo);
        }
Example #6
0
        /// <summary>
        /// 不需要实例化的资源的卸载,根据对象(比如Sprite,Texture)
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="destoryObj"></param>
        /// <returns></returns>
        public bool ReleaseResouce(Object obj, bool destoryObj = false)
        {
            if (obj == null)
            {
                return(false);
            }
            //根据GUID查找资源
            ResInfo item = null;

            foreach (ResInfo res in mResDictionary.Values)
            {
                if (res.mGUID == obj.GetInstanceID())
                {
                    item = res;
                }
            }

            if (item == null)
            {
                AFLogger.e("mResDictionary里不存在改资源:" + obj.name + "  可能释放了多次");
                return(false);
            }
            //destoryObj为false : 不释放destoryObj,只是将item放在前面去
            DestoryResouceItme(item, destoryObj);
            return(true);
        }
 //异步加载Sprite回调
 public void LoadSprite(bool result, ResInfo resInfo)
 {
     if (result)
     {
         spriteRender.sprite = resInfo.ResObject as Sprite;
     }
 }
Example #8
0
        public static ResObject Allocate(ResInfo resInfo)
        {
            ResObject resObject = SafeObjectPool <ResObject> .Instance.Allocate();

            resObject.mClear        = resInfo.DestroyCache;
            resObject.mResPath      = resInfo.ResPath;
            resObject.IsRecyclePool = false;
            return(resObject);
        }
Example #9
0
        public void PushIEnumeratorTask(ResInfo task)
        {
            if (task == null)
            {
                return;
            }

            mResInfoTask.AddLast(task);
            TryStartNextIEnumeratorTask();
        }
Example #10
0
        public bool ReleaseResouce(string ResPath)
        {
            ResInfo item = null;

            if (!mResDictionary.TryGetValue(Crc32.GetCrc32(ResPath), out item) || null == item)
            {
                AFLogger.e("mResDictionary里不存在改资源:" + ResPath + "  可能释放了多次");
            }
            DestoryResouceItme(item, item.DestroyCache);
            return(true);
        }
Example #11
0
 public void CancelLoadRes(ResInfo resInfo)
 {
     if (mAsyncLoadingObjDic.ContainsKey(resInfo.ResPath))
     {
         mAsyncLoadingObjDic[resInfo.ResPath].ForEach((objLoadInfo) =>
         {
             objLoadInfo.loadObjCall.InvokeGracefully(false, null);
         });
         mAsyncLoadingObjDic.Remove(resInfo.ResPath);
     }
 }
 public void LoadSpriteCallBack(bool result, ResInfo resInfo)
 {
     if (result)
     {
         spriteRender.sprite = resInfo.ResObject as Sprite;
     }
     else
     {
         AFLogger.e("加载" + resInfo.ResPath + "失败!");
     }
 }
 public void LoadResPrefab(bool result, ResInfo resInfo)
 {
     if (result)
     {
         Res.Add(Instantiate(resInfo.ResObject as GameObject));
         AFLogger.d("从Resources加载并实例化了" + Res.Count + "个");
     }
     else
     {
         AFLogger.e("加载" + resInfo.ResPath + "失败!");
     }
 }
Example #14
0
 /// <summary>
 /// 缓存太多,清除最早没有使用的资源
 /// </summary>
 protected void WashOut()
 {
     //当大于缓存个数时,进行一半释放
     while (m_NoRefrenceAssetMapList.Size() >= MAXCACHECOUNT)
     {
         for (int i = 0; i < MAXCACHECOUNT / 2; i++)
         {
             ResInfo item = m_NoRefrenceAssetMapList.Back();
             DestoryResouceItme(item, true);
         }
     }
 }
Example #15
0
        /// <summary>
        /// 取消异步加载资源
        /// </summary>
        /// <returns></returns>
        public bool CancelResLoad(uint crc)
        {
            ResInfo resInfo = GetRes(crc);

            if (resInfo != null)
            {
                if (resInfo.State != ResState.Ready)
                {
                    ResourceLoader.Instance.CancelLoad(resInfo);
                    resInfo.CancelLoad();
                }
            }
            return(true);
        }
Example #16
0
        /// <summary>
        ///  回收一个资源
        /// </summary>
        /// <param name="item"></param>
        /// <param name="destroyCache"></param>
        protected void DestoryResouceItme(ResInfo item, bool destroyCache = false)
        {
            if (item == null)
            {
                AFLogger.e("DestoryResouceItme:要释放的资源为空");
                return;
            }
            if (item.IsRecycled)
            {
                AFLogger.e("资源已经被回收,请检查代码是否回收了多次:" + item.ResPath);
                mResDictionary.Remove(item.mCRC);
                return;
            }
            if (item.RefCount <= 0)
            {
                AFLogger.e("资源引用计数<=0:" + item.ResPath);
                return;
            }
            //释放减少引用计数
            item.Release();
            //如果缓存下来,移到表头
            if (!destroyCache)
            {
                m_NoRefrenceAssetMapList.InsertToHead(item);
                return;
            }
            //不缓存,要根据引用计数判断是否释放内存
            if (item.ReleaseRes())
            {
                AFLogger.i("释放资源成功:" + item.ResPath);
                //资源缓存
                if (!mResDictionary.Remove(item.mCRC))
                {
                    return;
                }

                m_NoRefrenceAssetMapList.Remove(item);
                //释放assetbundle引用
                ReleaseAssetBundle(item);
                item.Recycle2Cache();
                //在编辑器中加载,需要如此才能清除内存,因此在程序退出时要调用此方法清除内存
#if UNITY_EDITOR
                Resources.UnloadUnusedAssets();
#endif
            }
            //else
            //{
            //    AF_Logger.Info("释放资源失败(引用计数不为0或者并没有加载完成):" + item.ResPath);
            //}
        }
Example #17
0
        /// <summary>
        /// 根据ResInfo卸载资源
        /// </summary>
        /// <param name="resInfo"></param>
        /// <param name="destoryObj"></param>
        /// <returns></returns>
        public bool ReleaseResouce(ResInfo resInfo, bool destoryObj = false)
        {
            if (resInfo == null)
            {
                return(false);
            }

            ResInfo item = null;

            if (!mResDictionary.TryGetValue(resInfo.mCRC, out item) || null == item)
            {
                AFLogger.e("mResDictionary里不存在改资源:" + resInfo.ResObject.name + "  可能释放了多次");
                return(false);
            }
            DestoryResouceItme(item, destoryObj);
            return(true);
        }
Example #18
0
        /// <summary>
        /// 不需要实例化的资源卸载,根据路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="destoryObj"></param>
        /// <returns></returns>
        public bool ReleaseResouce(string path, bool destoryObj)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            ResInfo item = null;

            if (!mResDictionary.TryGetValue(Crc32.GetCrc32(path), out item) || null == item)
            {
                AFLogger.e("mResDictionary里不存在改资源:" + path + "  可能释放了多次");
                return(false);
            }
            DestoryResouceItme(item, destoryObj);
            return(true);
        }
Example #19
0
 /// <summary>
 /// 释放资源
 /// </summary>
 /// <param name="item"></param>
 public void ReleaseAssetBundle(ResInfo item)
 {
     if (item == null)
     {
         return;
     }
     if (item.resItem == null)
     {
         return;
     }
     //先卸载依赖项再卸载自己
     if (item.resItem.DependAssetBundle != null && item.resItem.DependAssetBundle.Count > 0)
     {
         for (int i = 0; i < item.resItem.DependAssetBundle.Count; i++)
         {
             UnLoadAssetBundle(item.resItem.DependAssetBundle[i]);
         }
     }
     UnLoadAssetBundle(item.ABName);
 }
Example #20
0
        public void LoadResAsyncForObj(ObjLoadInfo objLoadInfo)
        {
            ResInfo resInfo = ResManager.Instance.GetRes(objLoadInfo.mCRC);

            if (resInfo == null)
            {
                ResLoadInfo resLoadInfo = ResLoadInfo.Allocate(objLoadInfo);
                //资源没有加载
                LoadAsync(resLoadInfo);
                resLoadInfo.Recycle2Cache();
            }
            else if (resInfo.State != ResState.Ready)
            {
                //资源正在加载,是其他loader的异步加载
                resInfo.RegisterListen(objLoadInfo.loadResCall);
            }
            else
            {
                //资源已经加载完成,开始回调
                objLoadInfo.loadResCall(true, resInfo);
            }
        }
Example #21
0
        public void AsyncObjCallBack(bool LoadResult, ResInfo resInfo)
        {
            if (!LoadResult)
            {
                mAsyncLoadingObjDic.Remove(resInfo.ResPath);
                ResManager.Instance.RemoveLoadFailRes(resInfo.mCRC);
                resInfo.Recycle2Cache();
                mAsyncLoadingObjDic[resInfo.ResPath].ForEach((objLoadInfo) =>
                {
                    objLoadInfo.loadObjCall.InvokeGracefully(LoadResult, null);
                    objLoadInfo.Recycle2Cache();
                });
                mAsyncLoadingObjDic.Remove(resInfo.ResPath);
                return;
            }
            bool DestroyCache = false;

            mAsyncLoadingObjDic[resInfo.ResPath].ForEach((objLoadInfo) =>
            {
                ResObject resObject = ResObject.Allocate(objLoadInfo);
                resObject.mObj      = resInfo.ResObject;
                resObject.ABName    = resInfo.ABName;
                resObject.mCloneObj = GameObject.Instantiate(resObject.mObj, objLoadInfo.ObjParentTrans) as GameObject;
                mObjList.AddValue(resObject.mCloneObj.GetInstanceID());
                objLoadInfo.loadObjCall.InvokeGracefully(LoadResult, resObject);
                ResManager.Instance.CacheResObj(resObject);
                if (objLoadInfo.mClear)
                {
                    DestroyCache = true;
                }
                resObject.Retain();
                resInfo.Retain();
                objLoadInfo.Recycle2Cache();
            });
            resInfo.DestroyCache = DestroyCache;
            mAsyncLoadingObjDic.Remove(resInfo.ResPath);
        }
Example #22
0
        /// <summary>
        /// 同步加载资源
        /// </summary>
        /// <param name="resLoadInfo"></param>
        /// <returns></returns>
        public Object LoadSync(ResLoadInfo resLoadInfo)
        {
            if (resLoadInfo.mResPath == "")
            {
                AFLogger.e("资源路径为空,无法进行加载");
                return(null);
            }
            ResInfo resInfo = ResManager.Instance.GetRes(resLoadInfo);

            if (resInfo != null && resInfo.State == ResState.Ready && (resInfo.ResObject.IsNotNull() || resInfo.ResStr.IsNotNullAndEmpty()))
            {
                resInfo.Retain();
                return(resInfo.ResObject);
            }
            else if (resInfo == null)
            {
                resInfo = ResFactory.Create(resLoadInfo);
                if (resInfo.LoadSync())
                {
                    resInfo.Retain();
                    Debug.Log("加载成功:" + resInfo.RefCount);
                    ResManager.Instance.CacheResource(resLoadInfo.mResPath, resInfo);
                    return(resInfo.ResObject);
                }
                else
                {
                    //加载失败,释放
                    resInfo.Recycle2Cache();
                }
            }
            else
            {
                AFLogger.e("同步遇到异步加载的资源 : " + resInfo.ResPath + ",请检查调用代码!");
            }
            return(null);
        }
Example #23
0
        public ResObject LoadResSyncForObj(ObjLoadInfo objLoadInfo)
        {
            ResObject resObject = ResObject.Allocate(objLoadInfo);
            ResInfo   resInfo   = ResManager.Instance.GetRes(objLoadInfo.mCRC);

            //之前就未加载
            if (resInfo == null)
            {
                ResLoadInfo resLoadInfo = ResLoadInfo.Allocate(objLoadInfo);
                resObject.mObj = LoadSync(resLoadInfo);
                resLoadInfo.Recycle2Cache();
            }
            //之前就加载完成
            else if (resInfo.State == ResState.Ready)
            {
                resInfo.Retain();
                resObject.mObj = resInfo.ResObject;
            }
            else
            {
                AFLogger.e("同步遇到异步正在或者等待加载的资源 : " + resInfo.ResPath + ",请检查调用代码!");
            }
            return(resObject);
        }
 public void LoadABSpriteCallBack(bool result, ResInfo resInfo)
 {
     spriteRender1.sprite = resInfo.ResObject as Sprite;
 }
Example #25
0
 /// <summary>
 /// 缓存加载好的资源
 /// </summary>
 /// <param name="ResPath"></param>
 /// <param name="resInfo"></param>
 public void CacheResource(string ResPath, ResInfo resInfo)
 {
     //缓存太多,清除最早没有使用的资源
     WashOut();
     mResDictionary.AddValue <uint, ResInfo>(Crc32.GetCrc32(ResPath), resInfo);
 }
Example #26
0
 public void CancelLoad(ResInfo task)
 {
     OnIEnumeratorTaskFinish(task.mCRC);
     ResManager.Instance.RemoveLoadFailRes(task.mCRC);
     ResManager.Instance.StopCoroutine(task.DoLoadAsync(OnIEnumeratorTaskFinish));
 }