public void Reset()
 {
     m_ObjDealFinish = null;
     m_ResObj        = null;
     m_ResDealFinish = null;
     m_Param1        = m_Param2 = m_Param3 = null;
 }
        /// <summary>
        /// 针对ObjectPoolMgr异步加载接口
        /// </summary>
        public void AsyncLoadResource(string path, ResourceObj resObj, OnAsyncFinish dealFinish, LoadResPriority priority)
        {
            ResourceItem item = GetCacheResourceItem(resObj.m_Crc);

            if (item != null)
            {
                resObj.m_ResItem = item;
                if (dealFinish != null)
                {
                    dealFinish(path, resObj);
                }
                return;
            }

            //判断是否在加载中
            AsynvLoadResParam para = null;

            if (!m_LodingAssetDic.TryGetValue(resObj.m_Crc, out para) || para == null)
            {
                para            = m_AsyncLoadResParamPool.Spawn(true);
                para.m_Crc      = resObj.m_Crc;
                para.m_Path     = path;
                para.m_Priority = priority;
                m_LodingAssetDic.Add(resObj.m_Crc, para);
                m_LoadingAssetList[(int)priority].Add(para);
            }

            //往回调列表里加回调
            //这里加的是针对的回调
            ResMgrAsyncCallBack callBack = m_AsyncCallBackPool.Spawn(true);

            callBack.m_ObjDealFinish = dealFinish;
            callBack.m_ResObj        = resObj;
            para.m_CallBackList.Add(callBack);
        }
        /// <summary>
        /// 取消异步加载资源
        /// </summary>
        public bool CancleLoad(ResourceObj resObj)
        {
            AsynvLoadResParam para = null;

            if (m_LodingAssetDic.TryGetValue(resObj.m_Crc, out para) && m_LoadingAssetList[(int)para.m_Priority].Contains(para))
            {
                //清除异步加载中间类中记录的异步回调list,将回调类先重置,后回收,然后移除
                for (int i = para.m_CallBackList.Count; i >= 0; i--)
                {
                    ResMgrAsyncCallBack tempCallBack = para.m_CallBackList[i];
                    if (tempCallBack != null && resObj == tempCallBack.m_ResObj)
                    {
                        tempCallBack.Reset();
                        m_AsyncCallBackPool.Recycle(tempCallBack);
                        para.m_CallBackList.Remove(tempCallBack);
                    }
                }
                //进行完上一步后,继续重置异步加载中间类,先重置,然后将分级列表中,dic中移除,回收异步加载中间类
                if (para.m_CallBackList.Count <= 0)
                {
                    para.Reset();
                    m_LoadingAssetList[(int)para.m_Priority].Remove(para);
                    m_AsyncLoadResParamPool.Recycle(para);
                    m_LodingAssetDic.Remove(resObj.m_Crc);
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// 同步加载
        /// </summary>
        /// <param name="path"></param>
        /// <param name="clearScene"></param>
        /// <returns></returns>
        public GameObject InstantiateObject(string path, bool setSceneObj = false, bool clearScene = true)
        {
            uint        crc         = CRC32.GetCRC32(path);
            ResourceObj resourceObj = GetObjectFromPool(crc);

            if (resourceObj == null)
            {
                resourceObj              = m_ResourceObjClassPool.Spawn(true);
                resourceObj.m_Crc        = crc;
                resourceObj.m_ClearScene = clearScene;
                //ResourceMgr提供加载方法
                resourceObj = ResourcesMgr.Instance.LoadResource(path, resourceObj);

                if (resourceObj.m_ResItem.m_Obj != null)
                {
                    resourceObj.m_CloneObj = GameObject.Instantiate(resourceObj.m_ResItem.m_Obj) as GameObject;
                }
            }

            if (setSceneObj)
            {
                resourceObj.m_CloneObj.transform.SetParent(SceneTrs, false);
            }

            int tempGuid = resourceObj.m_CloneObj.GetInstanceID();

            if (!m_ResourceObjDic.ContainsKey(tempGuid))
            {
                m_ResourceObjDic.Add(tempGuid, resourceObj);
            }

            return(resourceObj.m_CloneObj);
        }
        /// <summary>
        /// 清空对象池
        /// </summary>
        public void ClearCache()
        {
            List <uint> tempList = new List <uint>();

            foreach (uint key in m_ObjectPoolDic.Keys)
            {
                List <ResourceObj> st = m_ObjectPoolDic[key];
                for (int i = st.Count; i >= 0; i--)
                {
                    ResourceObj resObj = st[i];
                    if (!System.Object.ReferenceEquals(resObj.m_CloneObj, null) && resObj.m_ClearScene)
                    {
                        GameObject.Destroy(resObj.m_CloneObj);
                        m_ResourceObjDic.Remove(resObj.m_CloneObj.GetInstanceID());
                        resObj.Reset();
                        m_ResourceObjClassPool.Recycle(resObj);
                    }
                }
                if (st.Count <= 0)
                {
                    tempList.Add(key);
                }
            }

            for (int i = 0; i < tempList.Count; i++)
            {
                uint temp = tempList[i];
                if (m_ObjectPoolDic.ContainsKey(temp))
                {
                    m_ObjectPoolDic.Remove(temp);
                }
            }
            tempList.Clear();
        }
        /// <summary>
        /// 资源加载完成回调
        /// </summary>
        /// <param name="path"></param>
        /// <param name="resObj"></param>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <param name="param3"></param>
        void OnLoadResourceObjFinish(string path, ResourceObj resObj, object param1 = null, object param2 = null, object param3 = null)
        {
            if (resObj == null)
            {
                return;
            }
            if (resObj.m_ResItem.m_Obj == null)
            {
                ZLogger.Error("异步加载的资源为空:{0}", path);
            }
            else
            {
                resObj.m_CloneObj = GameObject.Instantiate(resObj.m_ResItem.m_Obj) as GameObject;
            }
            //加载完成就从正在加载的异步中移除
            if (m_AsyncResObjDic.ContainsKey(resObj.m_Guid))
            {
                m_AsyncResObjDic.Remove(resObj.m_Guid);
            }

            if (resObj.m_CloneObj != null && resObj.m_SetSceneParent)
            {
                resObj.m_CloneObj.transform.SetParent(SceneTrs, false);
            }

            if (resObj.m_DealFinish != null)
            {
                int tempGuid = resObj.m_CloneObj.GetInstanceID();
                if (!m_ResourceObjDic.ContainsKey(tempGuid))
                {
                    m_ResourceObjDic.Add(tempGuid, resObj);
                }
                resObj.m_DealFinish(path, resObj.m_CloneObj, resObj.m_Param1, resObj.m_Param2, resObj.m_Param3);
            }
        }
        /// <summary>
        /// 从对象池取对象
        /// </summary>
        /// <param name="crc"></param>
        /// <returns></returns>
        protected ResourceObj GetObjectFromPool(uint crc)
        {
            List <ResourceObj> st = null;

            if (m_ObjectPoolDic.TryGetValue(crc, out st) && st != null && st.Count > 0)
            {
                //ResourcesMgr的引用计数
                ResourcesMgr.Instance.IncreaseResourceRef(crc);
                ResourceObj resObj = st[0];
                st.RemoveAt(0);
                GameObject obj = resObj.m_CloneObj;
                if (!System.Object.ReferenceEquals(obj, null))
                {
                    resObj.m_Already = false;
#if UNITY_EDITOR
                    if (obj.name.EndsWith("(Recycle)"))
                    {
                        obj.name = obj.name.Replace("(Recycle)", "");
                    }
#endif
                }
                return(resObj);
            }
            return(null);
        }
        /// <summary>
        /// 清除某个资源在对象池中所有的对象
        /// </summary>
        /// <param name="crc"></param>
        public void ClearPoolObject(uint crc)
        {
            List <ResourceObj> st = null;

            if (!m_ObjectPoolDic.TryGetValue(crc, out st) || st == null)
            {
                return;
            }
            for (int i = st.Count - 1; i >= 0; i--)
            {
                ResourceObj resObj = st[i];
                if (resObj.m_ClearScene)
                {
                    st.Remove(resObj);
                    int tempID = resObj.m_CloneObj.GetInstanceID();
                    GameObject.Destroy(resObj.m_CloneObj);
                    resObj.Reset();
                    m_ResourceObjDic.Remove(tempID);
                    m_ResourceObjClassPool.Recycle(resObj);
                    st.Remove(resObj);
                }
            }
            if (st.Count <= 0)
            {
                m_ObjectPoolDic.Remove(crc);
            }
        }
        /// <summary>
        /// 同步加载资源,针对给ObjectPoolMgr接口
        /// </summary>
        public ResourceObj LoadResource(string path, ResourceObj resObj)
        {
            if (resObj == null)
            {
                return(null);
            }
            uint crc = resObj.m_Crc == 0 ? CRC32.GetCRC32(path) : resObj.m_Crc;

            ResourceItem item = GetCacheResourceItem(crc);

            if (item != null)
            {
                resObj.m_ResItem = item;
                return(resObj);
            }

            Object obj = null;

#if UNITY_EDITOR
            if (AppConst.DebugMode)
            {
                item = AssetBundleMgr.Instance.FindResourceItem(crc);
                if (item != null && item.m_Obj != null)
                {
                    obj = item.m_Obj as Object;
                }
                else
                {
                    if (item == null)
                    {
                        item       = new ResourceItem();
                        item.m_Crc = crc;
                    }
                    obj = LoadAssetByEditor <Object>(path);
                }
            }
#endif
            if (obj == null)
            {
                item = AssetBundleMgr.Instance.LoadResourceAssetBundle(crc);
                if (item != null && item.m_AssetBundel != null)
                {
                    if (item.m_Obj != null)
                    {
                        obj = item.m_Obj as Object;
                    }
                    else
                    {
                        obj = item.m_AssetBundel.LoadAsset <Object>(item.m_AssetName);
                    }
                }
            }

            CacheResource(path, ref item, crc, obj);
            resObj.m_ResItem  = item;
            item.m_ClearScene = resObj.m_ClearScene;

            return(resObj);
        }
        /// <summary>
        /// 取消异步加载
        /// </summary>
        /// <param name="guid"></param>
        public void CancleLoad(long guid)
        {
            ResourceObj resObj = null;

            if (m_AsyncResObjDic.TryGetValue(guid, out resObj) && ResourcesMgr.Instance.CancleLoad(resObj))
            {
                m_AsyncResObjDic.Remove(guid);
                resObj.Reset();
                m_ResourceObjClassPool.Recycle(resObj);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// 根据ResourceObj卸载资源,针对给ObjectPoolMgr接口
        /// </summary>
        public bool ReleaseResource(ResourceObj resObj, bool destoryObj = false)
        {
            if (resObj == null)
            {
                return(false);
            }
            ResourceItem item = null;

            if (!AssetDic.TryGetValue(resObj.m_Crc, out item) || item == null)
            {
                ZLogger.Error("AssetDic里不存在该资源:{0} ,可能释放了多次", resObj.m_CloneObj.name);
            }
            GameObject.Destroy(resObj.m_CloneObj);
            item.RefCount--;
            DestoryResourceItem(item, destoryObj);
            return(true);
        }
        /// <summary>
        /// 异步对象加载
        /// </summary>
        /// <param name="path"></param>
        /// <param name="dealFinish"></param>
        /// <param name="priority"></param>
        /// <param name="setSceneObject"></param>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <param name="param3"></param>
        /// <param name="bClear"></param>
        public long InstantiateObjectAsync(string path, OnAsyncObjFinish dealFinish, LoadResPriority priority, bool setSceneObject = false,
                                           object param1 = null, object param2 = null, object param3 = null, bool clearScene = true)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(0);
            }

            uint        crc    = CRC32.GetCRC32(path);
            ResourceObj resObj = GetObjectFromPool(crc);

            if (resObj != null)
            {
                if (setSceneObject)
                {
                    resObj.m_CloneObj.transform.SetParent(SceneTrs, false);
                }

                if (dealFinish != null)
                {
                    dealFinish(path, resObj.m_CloneObj, param1, param2, param3);
                }
                return(resObj.m_Guid);
            }
            long guid = ResourcesMgr.Instance.CreatGuid();

            resObj                  = m_ResourceObjClassPool.Spawn(true);
            resObj.m_Crc            = crc;
            resObj.m_SetSceneParent = setSceneObject;
            resObj.m_ClearScene     = clearScene;
            resObj.m_DealFinish     = dealFinish;
            resObj.m_Param1         = param1;
            resObj.m_Param2         = param2;
            resObj.m_Param3         = param3;
            //调用ResourcesMgr的异步加载接口
            ResourcesMgr.Instance.AsyncLoadResource(path, resObj, OnLoadResourceObjFinish, priority);
            return(guid);
        }
Esempio n. 13
0
        /// <summary>
        /// 异步加载
        /// </summary>
        IEnumerator AsyncLoadCor()
        {
            List <ResMgrAsyncCallBack> callBackList = null;
            //上一次yield的时间
            long lastYieldTime = System.DateTime.Now.Ticks;

            while (true)
            {
                bool haveYieldBoo = false;
                for (int i = 0; i < (int)LoadResPriority.RES_NUM; i++)
                {
                    if (m_LoadingAssetList[(int)LoadResPriority.RES_HIGHT].Count > 0)
                    {
                        i = (int)LoadResPriority.RES_HIGHT;
                    }
                    else if (m_LoadingAssetList[(int)LoadResPriority.RES_MIDDLE].Count > 0)
                    {
                        i = (int)LoadResPriority.RES_MIDDLE;
                    }

                    List <AsynvLoadResParam> loadingList = m_LoadingAssetList[i];
                    if (loadingList.Count <= 0)
                    {
                        continue;
                    }
                    AsynvLoadResParam loadingItem = loadingList[0];
                    loadingList.RemoveAt(0);
                    callBackList = loadingItem.m_CallBackList;

                    Object       obj  = null;
                    ResourceItem item = null;
#if UNITY_EDITOR
                    if (AppConst.DebugMode)
                    {
                        if (loadingItem.m_SpriteBoo)
                        {
                            obj = LoadAssetByEditor <Sprite>(loadingItem.m_Path);
                        }
                        else
                        {
                            obj = LoadAssetByEditor <Object>(loadingItem.m_Path);
                        }
                        //模拟异步加载
                        yield return(new WaitForSeconds(0.5f));

                        item = AssetBundleMgr.Instance.FindResourceItem(loadingItem.m_Crc);
                        if (item == null)
                        {
                            item       = new ResourceItem();
                            item.m_Crc = loadingItem.m_Crc;
                        }
                    }
#endif
                    if (obj == null)
                    {
                        item = AssetBundleMgr.Instance.LoadResourceAssetBundle(loadingItem.m_Crc);
                        if (item != null && item.m_AssetBundel != null)
                        {
                            AssetBundleRequest abRequest = null;
                            if (loadingItem.m_SpriteBoo)
                            {
                                abRequest = item.m_AssetBundel.LoadAssetAsync <Sprite>(item.m_AssetName);
                            }
                            else
                            {
                                abRequest = item.m_AssetBundel.LoadAssetAsync(item.m_AssetName);
                            }
                            yield return(abRequest);

                            if (abRequest.isDone)
                            {
                                obj = abRequest.asset;
                            }
                            lastYieldTime = System.DateTime.Now.Ticks;
                        }
                    }
                    CacheResource(loadingItem.m_Path, ref item, loadingItem.m_Crc, obj, callBackList.Count);

                    for (int j = 0; j < callBackList.Count; j++)
                    {
                        ResMgrAsyncCallBack callBack = callBackList[j];

                        if (callBack != null && callBack.m_ObjDealFinish != null && callBack.m_ResObj != null)
                        {
                            ResourceObj tempResObj = callBack.m_ResObj;
                            tempResObj.m_ResItem = item;
                            callBack.m_ObjDealFinish(loadingItem.m_Path, tempResObj, tempResObj.m_Param1, tempResObj.m_Param2, tempResObj.m_Param3);
                            callBack.m_ObjDealFinish = null;
                            tempResObj = null;
                        }

                        if (callBack != null && callBack.m_ResDealFinish != null)
                        {
                            callBack.m_ResDealFinish(loadingItem.m_Path, obj, callBack.m_Param1, callBack.m_Param2, callBack.m_Param3);
                            callBack.m_ResDealFinish = null;
                        }
                        callBack.Reset();
                        m_AsyncCallBackPool.Recycle(callBack);
                    }

                    obj = null;
                    callBackList.Clear();
                    m_LodingAssetDic.Remove(loadingItem.m_Crc);

                    loadingItem.Reset();
                    m_AsyncLoadResParamPool.Recycle(loadingItem);

                    if (System.DateTime.Now.Ticks - lastYieldTime > MAXLOADRESTIME)
                    {
                        yield return(null);

                        lastYieldTime = System.DateTime.Now.Ticks;
                        haveYieldBoo  = true;
                    }
                }

                if (!haveYieldBoo || System.DateTime.Now.Ticks - lastYieldTime > MAXLOADRESTIME)
                {
                    lastYieldTime = System.DateTime.Now.Ticks;
                    yield return(null);
                }
            }
        }
Esempio n. 14
0
 /// <summary>
 /// 根据ResourceObj减少引用计数
 /// </summary>
 public int DecreaseResourceRef(ResourceObj resObj, int count = 1)
 {
     return(resObj != null?DecreaseResourceRef(resObj.m_Crc, count) : 0);
 }
        /// <summary>
        /// 回收资源
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="maxCacheCount"></param>
        /// <param name="destoryCache"></param>
        /// <param name="recycleParent"></param>
        public void ReleaseObject(GameObject obj, int maxCacheCount = -1, bool destoryCache = false, bool recycleParent = true)
        {
            if (obj == null)
            {
                return;
            }

            ResourceObj resObj   = null;
            int         tempGuid = obj.GetInstanceID();

            if (!m_ResourceObjDic.TryGetValue(tempGuid, out resObj))
            {
                ZLogger.Error("{0} 对象不是ObjectManager创建的!", obj.name);
                return;
            }

            if (resObj == null)
            {
                ZLogger.Error("缓存的ResourObj为空");
                return;
            }

            if (resObj.m_Already)
            {
                ZLogger.Error("该对象已经返回对象池了,检查自己是否清空引用");
                return;
            }

#if UNITY_EDITOR
            obj.name += "(Recycle)";
#endif
            List <ResourceObj> st = null;
            if (maxCacheCount == 0)
            {
                m_ResourceObjDic.Remove(tempGuid);
                ResourcesMgr.Instance.ReleaseResource(resObj, destoryCache);
                resObj.Reset();
                m_ResourceObjClassPool.Recycle(resObj);
            }
            else//回到到对象池
            {
                if (!m_ObjectPoolDic.TryGetValue(resObj.m_Crc, out st) || st == null)
                {
                    st = new List <ResourceObj>();
                    m_ObjectPoolDic.Add(resObj.m_Crc, st);
                }

                if (resObj.m_CloneObj)
                {
                    if (recycleParent)
                    {
                        resObj.m_CloneObj.transform.SetParent(RecyclePoolTrs);
                    }
                    else
                    {
                        resObj.m_CloneObj.SetActive(false);
                    }
                }

                if (maxCacheCount < 0 || st.Count < maxCacheCount)
                {
                    st.Add(resObj);
                    resObj.m_Already = true;
                    //ResourcesMgr做一个引用计数
                    ResourcesMgr.Instance.DecreaseResourceRef(resObj);
                }
                else
                {
                    m_ResourceObjDic.Remove(tempGuid);
                    ResourcesMgr.Instance.ReleaseResource(resObj, destoryCache);
                    resObj.Reset();
                    m_ResourceObjClassPool.Recycle(resObj);
                }
            }
        }
        /// <summary>
        /// 该对象是否是对象池创建的
        /// </summary>
        /// <returns></returns>
        public bool IsObjectManagerCreat(GameObject obj)
        {
            ResourceObj tempObj = m_ResourceObjDic[obj.GetInstanceID()];

            return(tempObj != null);
        }