/// <summary>
        /// objectManager 提供一个清除缓存的接口
        /// </summary>
        public void ClearCatch()
        {
            List <uint> tempList = new List <uint>();

            foreach (var key in m_resourcesItemPoolDic.Keys)
            {
                List <MResourceObjectItem> mResourceObjectLsit = m_resourcesItemPoolDic[key];
                for (int i = mResourceObjectLsit.Count - 1; i >= 0; i--)
                {
                    MResourceObjectItem mResourceObjectItem = mResourceObjectLsit[i];
                    m_resourceObjectDic.Remove(mResourceObjectItem.m_cloneObeject.GetInstanceID());
                    mResourceObjectItem.Reset();
                    m_resourceObjectClssPool.Recycle(mResourceObjectItem);
                }

                if (mResourceObjectLsit.Count <= 0)
                {
                    tempList.Add(key);
                }
            }

            for (int i = 0; i < tempList.Count; i++)
            {
                uint tempKey = tempList[i];
                if (m_resourcesItemPoolDic.ContainsKey(tempKey))
                {
                    m_resourcesItemPoolDic.Remove(tempKey);
                }
            }

            tempList.Clear();
        }
Example #2
0
        /// <summary>
        /// 取消异步加载资源
        /// </summary>
        /// <returns></returns>
        public bool CancleAsyncLoad(MResourceObjectItem mResourceObjectItem)
        {
            AsyncLoadResParam asyncLoadResParam = null;

            if (m_asyncLoadingAssetDic.TryGetValue(mResourceObjectItem.m_crc, out asyncLoadResParam)
                //异步进行中有取的操作 所以有这个判断
                && m_asyncAssetLoadingList[(int)asyncLoadResParam.m_loadResPriority].Contains(asyncLoadResParam))
            {
                for (int i = asyncLoadResParam.m_asyncCallBacks.Count - 1; i >= 0; i--)
                {
                    AsyncCallBack asyncCallBack = asyncLoadResParam.m_asyncCallBacks[i];
                    if (asyncCallBack != null &&
                        mResourceObjectItem == asyncCallBack.m_resourceObjectItem)
                    {
                        asyncCallBack.Reset();
                        m_asyncCallBackPool.Recycle(asyncCallBack);
                        asyncLoadResParam.m_asyncCallBacks.Remove(asyncCallBack);
                    }
                }
                //如果中间类中已经没有回调类了 那么回收中间类
                if (asyncLoadResParam.m_asyncCallBacks.Count < 0)
                {
                    asyncLoadResParam.Reset();
                    m_asyncAssetLoadingList[(int)asyncLoadResParam.m_loadResPriority].Remove(asyncLoadResParam);
                    m_asyncLoadResParamPool.Recycle(asyncLoadResParam);
                    return(true);
                }
            }
            return(false);
        }
        /// <summary>
        /// 同步加载GameObject的方法
        /// </summary>
        /// <returns>The game obeject.</returns>
        /// <param name="resPath">资源路径</param>
        /// <param name="isSetToDefault">是否实例化到默认节点</param>
        /// <param name="isChangeSceneClear">在切换场景的时候是否清楚资源的缓存</param>
        public GameObject InstantiateGameObeject(string resPath, bool isSetToDefault = false, bool isChangeSceneClear = true)
        {
            uint crc = MCrcHelper.GetCRC32(resPath);
            MResourceObjectItem mResourceObjectItem = GetObjectFromPool(crc);

            if (mResourceObjectItem == null)
            {
                mResourceObjectItem           = m_resourceObjectClssPool.Spawn(true);
                mResourceObjectItem.m_crc     = crc;
                mResourceObjectItem.m_isClear = isChangeSceneClear;
                mResourceObjectItem           = MResourceManager.singleton.LoadToResourceObject(resPath, mResourceObjectItem);

                if (mResourceObjectItem.m_resItem.m_object != null)
                {
                    mResourceObjectItem.m_cloneObeject   = (GameObject)Object.Instantiate(mResourceObjectItem.m_resItem.m_object);
                    mResourceObjectItem.m_instanceId     = mResourceObjectItem.m_cloneObeject.GetInstanceID();
                    mResourceObjectItem.m_resOffLineData = mResourceObjectItem.m_cloneObeject.GetComponent <MResOffLineDataBase>();
                }
            }

            if (isSetToDefault)
            {
                mResourceObjectItem.m_cloneObeject.transform.SetParent(DefaultObjectTrans, false);
            }

            if (!m_resourceObjectDic.ContainsKey(mResourceObjectItem.m_instanceId))
            {
                m_resourceObjectDic.Add(mResourceObjectItem.m_instanceId, mResourceObjectItem);
            }
            return(mResourceObjectItem.m_cloneObeject);
        }
        /// <summary>
        /// 存ResobjecyItem对象池中取出对象
        /// </summary>
        /// <returns>返回 MResourceObjectItem 对象</returns>
        /// <param name="crc">资源的ResPath的Crc</param>
        public MResourceObjectItem GetObjectFromPool(uint crc)
        {
            List <MResourceObjectItem> mResourceObjectItemList = null;

            if (m_resourcesItemPoolDic.TryGetValue(crc, out mResourceObjectItemList) && mResourceObjectItemList.Count > 0)
            {
                MResourceManager.singleton.InCreaseResourceRef(crc);
                MResourceObjectItem resObj = mResourceObjectItemList[0];
                mResourceObjectItemList.RemoveAt(0);
                GameObject gameObject = resObj.m_cloneObeject;
                if (!System.Object.ReferenceEquals(gameObject, null))
                {
                    if (!System.Object.ReferenceEquals(resObj.m_resOffLineData, null))
                    {
                        resObj.m_resOffLineData.ResetBasicData();
                    }
                    resObj.m_isAlreadyRelease = false;
#if UNITY_EDITOR
                    if (gameObject.name.EndsWith("(Recycle)"))
                    {
                        gameObject.name = gameObject.name.Replace("(Recycle)", "");
                    }
#endif
                }
                return(resObj);
            }
            return(null);
        }
Example #5
0
 //数据重置
 public void Reset()
 {
     m_onAsyncLoadFinished       = null;
     m_onAsyncLoadObjectFinished = null;
     m_resourceObjectItem        = null;
     m_parms = null;
 }
        /// <summary>
        /// 清除某个对象在对象池中的所有对象
        /// </summary>
        /// <param name="resCrc">资源路径Crc</param>
        public void ClearPoolObject(uint resCrc)
        {
            List <MResourceObjectItem> mResourceObjectLsit = null;

            if (!m_resourcesItemPoolDic.TryGetValue(resCrc, out mResourceObjectLsit) || mResourceObjectLsit == null)
            {
                return;
            }
            for (int i = mResourceObjectLsit.Count - 1; i >= 0; i--)
            {
                MResourceObjectItem mResourceObjectItem = mResourceObjectLsit[i];
                if (mResourceObjectItem.m_isClear)
                {
                    mResourceObjectLsit.Remove(mResourceObjectItem);
                    mResourceObjectItem.Reset();
                    m_resourceObjectDic.Remove(mResourceObjectItem.m_cloneObeject.GetInstanceID());
                    m_resourceObjectClssPool.Recycle(mResourceObjectItem);
                    GameObject.Destroy(mResourceObjectItem.m_cloneObeject);
                }
            }

            if (mResourceObjectLsit.Count <= 0)
            {
                if (m_resourcesItemPoolDic.ContainsKey(resCrc))
                {
                    m_resourcesItemPoolDic.Remove(resCrc);
                }
            }
        }
Example #7
0
 /// <summary>
 /// 依据MResourceObjectItem减少Crc
 /// </summary>
 /// <returns></returns>
 public int DeCreaseResourceRef(MResourceObjectItem mResourceObjectItem, int count = 1)
 {
     if (mResourceObjectItem == null)
     {
         return(0);
     }
     return(DeCreaseResourceRef(mResourceObjectItem.m_crc, count));
 }
Example #8
0
        /// <summary>
        /// 同步加载资源 针对给ObjectManager
        /// </summary>
        /// <param name="resPath">资源路径</param>
        /// <param name="mResourceObjectItem">ObjecyResItem</param>
        public MResourceObjectItem LoadToResourceObject(string resPath, MResourceObjectItem mResourceObjectItem)
        {
            if (mResourceObjectItem == null)
            {
                return(null);
            }

            uint          crc           = mResourceObjectItem.m_crc == 0 ? MCrcHelper.GetCRC32(resPath) : mResourceObjectItem.m_crc;
            MResourceItem mResourceItem = GetCacheResourceItem(crc);

            if (mResourceItem != null && mResourceItem.m_object != null)
            {
                mResourceObjectItem.m_resItem = mResourceItem;
                return(mResourceObjectItem);
            }

            Object obj = null;

#if UNITY_EDITOR
            if (!m_isLoadFormAssetBundle)
            {
                mResourceItem = MAssetBundleManager.singleton.FindResourceItem(crc);
                if (mResourceItem == null)
                {
                    mResourceItem       = new MResourceItem();
                    mResourceItem.m_crc = crc;
                }
                mResourceItem.m_path = resPath;
                if (mResourceItem.m_object != null)
                {
                    obj = mResourceItem.m_object as Object;
                }
                else
                {
                    obj = LoadAssetFormEditor <Object>(mResourceItem.m_path);
                }
            }
#endif
            if (obj == null)
            {
                mResourceItem        = MAssetBundleManager.singleton.LoadResourcesAssetBundle(crc);
                mResourceItem.m_path = resPath;
                if (mResourceItem.m_object != null)
                {
                    obj = mResourceItem.m_object as Object;
                }
                else
                {
                    obj = mResourceItem.m_assetBundle.LoadAsset <Object>(mResourceItem.m_path);
                }
            }

            CacheResource(resPath, ref mResourceItem, crc, obj);
            mResourceItem.m_clear         = mResourceObjectItem.m_isClear;
            mResourceObjectItem.m_resItem = mResourceItem;
            return(mResourceObjectItem);
        }
Example #9
0
 public void Call(string param0, MFrameWork.MResourceObjectItem param1, object[] param2)
 {
     func.BeginPCall();
     func.Push(param0);
     func.PushObject(param1);
     func.Push(param2);
     func.PCall();
     func.EndPCall();
 }
    static int AsyncLoadResource(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 4)
            {
                MFrameWork.MResourceManager obj = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                string arg0 = ToLua.CheckString(L, 2);
                MFrameWork.OnAsyncLoadFinished arg1 = (MFrameWork.OnAsyncLoadFinished)ToLua.CheckDelegate <MFrameWork.OnAsyncLoadFinished>(L, 3);
                MFrameWork.LoadResPriority     arg2 = (MFrameWork.LoadResPriority)ToLua.CheckObject(L, 4, typeof(MFrameWork.LoadResPriority));
                obj.AsyncLoadResource(arg0, arg1, arg2);
                return(0);
            }
            else if (count == 5 && TypeChecker.CheckTypes <MFrameWork.OnAsyncLoadFinished, MFrameWork.LoadResPriority, object[]>(L, 3))
            {
                MFrameWork.MResourceManager obj = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                string arg0 = ToLua.CheckString(L, 2);
                MFrameWork.OnAsyncLoadFinished arg1 = (MFrameWork.OnAsyncLoadFinished)ToLua.ToObject(L, 3);
                MFrameWork.LoadResPriority     arg2 = (MFrameWork.LoadResPriority)ToLua.ToObject(L, 4);
                object[] arg3 = ToLua.ToObjectArray(L, 5);
                obj.AsyncLoadResource(arg0, arg1, arg2, arg3);
                return(0);
            }
            else if (count == 5 && TypeChecker.CheckTypes <MFrameWork.MResourceObjectItem, MFrameWork.OnAsyncLoadObjectFinished, MFrameWork.LoadResPriority>(L, 3))
            {
                MFrameWork.MResourceManager obj = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                string arg0 = ToLua.CheckString(L, 2);
                MFrameWork.MResourceObjectItem       arg1 = (MFrameWork.MResourceObjectItem)ToLua.ToObject(L, 3);
                MFrameWork.OnAsyncLoadObjectFinished arg2 = (MFrameWork.OnAsyncLoadObjectFinished)ToLua.ToObject(L, 4);
                MFrameWork.LoadResPriority           arg3 = (MFrameWork.LoadResPriority)ToLua.ToObject(L, 5);
                obj.AsyncLoadResource(arg0, arg1, arg2, arg3);
                return(0);
            }
            else if (count == 6)
            {
                MFrameWork.MResourceManager obj = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                string arg0 = ToLua.CheckString(L, 2);
                MFrameWork.MResourceObjectItem       arg1 = (MFrameWork.MResourceObjectItem)ToLua.CheckObject <MFrameWork.MResourceObjectItem>(L, 3);
                MFrameWork.OnAsyncLoadObjectFinished arg2 = (MFrameWork.OnAsyncLoadObjectFinished)ToLua.CheckDelegate <MFrameWork.OnAsyncLoadObjectFinished>(L, 4);
                MFrameWork.LoadResPriority           arg3 = (MFrameWork.LoadResPriority)ToLua.CheckObject(L, 5, typeof(MFrameWork.LoadResPriority));
                object[] arg4 = ToLua.CheckObjectArray(L, 6);
                obj.AsyncLoadResource(arg0, arg1, arg2, arg3, arg4);
                return(0);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: MFrameWork.MResourceManager.AsyncLoadResource"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
Example #11
0
        /// <summary>
        /// 取消异步加载
        /// </summary>
        /// <param name="asyncGuid">异步加载GUID</param>
        public void CancleAsyncResObjectLoad(long asyncGuid)
        {
            MResourceObjectItem mResourceObjectItem = null;

            if (m_asyncResourcesObjectsDic.TryGetValue(asyncGuid, out mResourceObjectItem))
            {
                m_asyncResourcesObjectsDic.Remove(asyncGuid);
                mResourceObjectItem.Reset();
                m_resourceObjectClssPool.Recycle(mResourceObjectItem);
            }
        }
Example #12
0
        //返回当前Gameobject身上的离线数据信息
        public MResOffLineDataBase GetObjOffLineData(GameObject gameObject)
        {
            MResOffLineDataBase mResOffLineData     = null;
            MResourceObjectItem mResourceObjectItem = null;

            if (m_resourceObjectDic.TryGetValue(gameObject.GetInstanceID(), out mResourceObjectItem))
            {
                if (mResourceObjectItem != null)
                {
                    mResOffLineData = mResourceObjectItem.m_resOffLineData;
                }
            }
            return(mResOffLineData);
        }
    static int DeCreaseResourceRef(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 2 && TypeChecker.CheckTypes <MFrameWork.MResourceObjectItem>(L, 2))
            {
                MFrameWork.MResourceManager    obj  = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                MFrameWork.MResourceObjectItem arg0 = (MFrameWork.MResourceObjectItem)ToLua.ToObject(L, 2);
                int o = obj.DeCreaseResourceRef(arg0);
                LuaDLL.lua_pushinteger(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <uint>(L, 2))
            {
                MFrameWork.MResourceManager obj = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                uint arg0 = (uint)LuaDLL.lua_tonumber(L, 2);
                int  o    = obj.DeCreaseResourceRef(arg0);
                LuaDLL.lua_pushinteger(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <MFrameWork.MResourceObjectItem, int>(L, 2))
            {
                MFrameWork.MResourceManager    obj  = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                MFrameWork.MResourceObjectItem arg0 = (MFrameWork.MResourceObjectItem)ToLua.ToObject(L, 2);
                int arg1 = (int)LuaDLL.lua_tonumber(L, 3);
                int o    = obj.DeCreaseResourceRef(arg0, arg1);
                LuaDLL.lua_pushinteger(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <uint, int>(L, 2))
            {
                MFrameWork.MResourceManager obj = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                uint arg0 = (uint)LuaDLL.lua_tonumber(L, 2);
                int  arg1 = (int)LuaDLL.lua_tonumber(L, 3);
                int  o    = obj.DeCreaseResourceRef(arg0, arg1);
                LuaDLL.lua_pushinteger(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: MFrameWork.MResourceManager.DeCreaseResourceRef"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
 static int CancleAsyncLoad(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         MFrameWork.MResourceManager    obj  = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
         MFrameWork.MResourceObjectItem arg0 = (MFrameWork.MResourceObjectItem)ToLua.CheckObject <MFrameWork.MResourceObjectItem>(L, 2);
         bool o = obj.CancleAsyncLoad(arg0);
         LuaDLL.lua_pushboolean(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
 static int GetObjectFromPool(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         MFrameWork.MObjectManager obj = (MFrameWork.MObjectManager)ToLua.CheckObject <MFrameWork.MObjectManager>(L, 1);
         uint arg0 = (uint)LuaDLL.luaL_checknumber(L, 2);
         MFrameWork.MResourceObjectItem o = obj.GetObjectFromPool(arg0);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
 static int LoadToResourceObject(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 3);
         MFrameWork.MResourceManager obj = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
         string arg0 = ToLua.CheckString(L, 2);
         MFrameWork.MResourceObjectItem arg1 = (MFrameWork.MResourceObjectItem)ToLua.CheckObject <MFrameWork.MResourceObjectItem>(L, 3);
         MFrameWork.MResourceObjectItem o    = obj.LoadToResourceObject(arg0, arg1);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Example #17
0
        /// <summary>
        /// 给ObjectManager的释放对象的接口 依据MResourceObjectItem对对象做释放
        /// </summary>
        /// <param name="mResourceObjectItem">ObjectManager 对象</param>
        /// <param name="destoryCompletly">是否彻底删除</param>
        public bool ReleaseResource(MResourceObjectItem mResourceObjectItem, bool destoryCompletly = false)
        {
            if (mResourceObjectItem == null || mResourceObjectItem.m_resItem == null || mResourceObjectItem.m_resItem.m_object == null)
            {
                return(false);
            }

            MResourceItem mResourceItem = null;

            if (!m_resourcesItemDic.TryGetValue(mResourceObjectItem.m_crc, out mResourceItem) && mResourceItem != null)
            {
                MDebug.singleton.AddErrorLog(" m_resourcesItemDic 不存在这个资源 resPath : " + mResourceItem.m_path);
                return(false);
            }
            Object.Destroy(mResourceObjectItem.m_cloneObeject);
            mResourceItem.RefCount--;
            DestoryResourceItem(mResourceItem, destoryCompletly);
            return(true);
        }
Example #18
0
        /// <summary>
        /// 异步加载需要实力化的GameObject的函数
        /// </summary>
        /// <param name="resPath"></param>
        /// <param name="onAsyncLoadFinished"></param>
        /// <param name="loadResPriority"></param>
        /// <param name="isSetToDefault"></param>
        /// <param name="parms"></param>
        /// <param name="isChangeSceneClear"></param>
        public long InstantiateGameObejectAsync(string resPath, OnAsyncLoadObjectFinished onAsyncLoadFinished, LoadResPriority loadResPriority,
                                                bool isSetToDefault = false, object[] parms = null, bool isChangeSceneClear = true)
        {
            if (string.IsNullOrEmpty(resPath))
            {
                return(0);
            }

            uint crc = MCrcHelper.GetCRC32(resPath);
            MResourceObjectItem mResourceObjectItem = GetObjectFromPool(crc);

            if (mResourceObjectItem != null && mResourceObjectItem.m_cloneObeject != null)
            {
                if (isSetToDefault)
                {
                    mResourceObjectItem.m_cloneObeject.transform.SetParent(DefaultObjectTrans);
                }

                if (onAsyncLoadFinished != null)
                {
                    onAsyncLoadFinished(resPath, mResourceObjectItem, parms);
                }
                return(mResourceObjectItem.m_asyncGuid);
            }
            long m_asyncGuid = MResourceManager.singleton.GetGUID();

            mResourceObjectItem                             = m_resourceObjectClssPool.Spawn(true);
            mResourceObjectItem.m_crc                       = crc;
            mResourceObjectItem.m_isSetToDefault            = isSetToDefault;
            mResourceObjectItem.m_isClear                   = isChangeSceneClear;
            mResourceObjectItem.m_onAsyncLoadObjectFinished = onAsyncLoadFinished;
            mResourceObjectItem.m_parms                     = parms;
            mResourceObjectItem.m_asyncGuid                 = m_asyncGuid;

            //添加到异步加载管理列表里
            m_asyncResourcesObjectsDic.Add(m_asyncGuid, mResourceObjectItem);
            //调用MResourceManager为Object准备的的异步加载函数
            MResourceManager.singleton.AsyncLoadResource(resPath, mResourceObjectItem, OnAsyncLoadObjectFinish, loadResPriority, parms);
            return(m_asyncGuid);
        }
Example #19
0
        /// <summary>
        /// 资源加载完成回调
        /// </summary>
        /// <param name="resPath">资源路径</param>
        /// <param name="mResourceObjectItem">中间类</param>
        /// <param name="parms">附加参数</param>
        void OnAsyncLoadObjectFinish(string resPath, MResourceObjectItem mResourceObjectItem, object[] parms = null)
        {
            if (mResourceObjectItem == null)
            {
                return;
            }
            if (mResourceObjectItem.m_resItem.m_object == null)
            {
#if UNITY_EDITOR
                MDebug.singleton.AddErrorLog("异步资源加载为空 : " + resPath);
#endif
            }
            else
            {
                mResourceObjectItem.m_cloneObeject   = GameObject.Instantiate(mResourceObjectItem.m_resItem.m_object) as GameObject;
                mResourceObjectItem.m_resOffLineData = mResourceObjectItem.m_cloneObeject.GetComponent <MResOffLineDataBase>();
            }

            //加载完成从正在加载的异步队列中移除
            if (m_asyncResourcesObjectsDic.ContainsKey(mResourceObjectItem.m_asyncGuid))
            {
                m_asyncResourcesObjectsDic.Remove(mResourceObjectItem.m_asyncGuid);
            }

            if (mResourceObjectItem.m_cloneObeject != null && mResourceObjectItem.m_isSetToDefault)
            {
                mResourceObjectItem.m_cloneObeject.transform.SetParent(DefaultObjectTrans);
            }

            if (mResourceObjectItem.m_onAsyncLoadObjectFinished != null)
            {
                int instanceId = mResourceObjectItem.m_cloneObeject.GetInstanceID();
                if (!m_resourceObjectDic.ContainsKey(instanceId))
                {
                    m_resourceObjectDic.Add(instanceId, mResourceObjectItem);
                }
                mResourceObjectItem.m_onAsyncLoadObjectFinished(resPath, mResourceObjectItem, parms);
            }
        }
Example #20
0
        /// <summary>
        /// 实例化资源异步加载 为MObjectManager提供的接口
        /// </summary>
        /// <param name="resPath">资源路径</param>
        /// <param name="mResourceObjectItem"></param>
        /// <param name="onAsyncLoadObjectFinished">加载回掉</param>
        /// <param name="loadResPriority">优先级</param>
        /// <param name="parms"></param>
        public void AsyncLoadResource(string resPath, MResourceObjectItem mResourceObjectItem, OnAsyncLoadObjectFinished onAsyncLoadObjectFinished, LoadResPriority loadResPriority, object[] parms = null)
        {
            if (string.IsNullOrEmpty(resPath))
            {
                return;
            }

            MResourceItem mResourceItem = GetCacheResourceItem(mResourceObjectItem.m_crc);

            if (mResourceItem != null && mResourceItem.m_object != null)
            {
                if (onAsyncLoadObjectFinished != null)
                {
                    onAsyncLoadObjectFinished(resPath, mResourceObjectItem, parms);
                }
                return;
            }

            //判断下对象是不是在加载中
            AsyncLoadResParam asyncLoadResParam = null;

            if (!m_asyncLoadingAssetDic.TryGetValue(mResourceObjectItem.m_crc, out asyncLoadResParam) || asyncLoadResParam == null)
            {
                asyncLoadResParam                   = m_asyncLoadResParamPool.Spawn(true);
                asyncLoadResParam.m_crc             = mResourceObjectItem.m_crc;
                asyncLoadResParam.m_resPath         = resPath;
                asyncLoadResParam.m_loadResPriority = loadResPriority;
                //结果保存
                m_asyncAssetLoadingList[(int)loadResPriority].Add(asyncLoadResParam);
                m_asyncLoadingAssetDic.Add(mResourceObjectItem.m_crc, asyncLoadResParam);
            }

            //添加回调
            AsyncCallBack m_asyncCallBack = m_asyncCallBackPool.Spawn(true);

            m_asyncCallBack.m_onAsyncLoadObjectFinished = onAsyncLoadObjectFinished;
            m_asyncCallBack.m_resourceObjectItem        = mResourceObjectItem;
            asyncLoadResParam.m_asyncCallBacks.Add(m_asyncCallBack);
        }
Example #21
0
        /// <summary>
        /// 异步加载的携程
        /// </summary>
        /// <returns></returns>
        IEnumerator AsyncLoader()
        {
            List <AsyncCallBack> callBackList;
            //用于记录上次的加载时间
            long lastReturnTime = System.DateTime.Now.Ticks;

            while (true)
            {
                //标志位 用于判读在For循环中是否已经return过了
                bool isYieldReturn = false;

                for (int i = 0; i < (int)LoadResPriority.RES_LOAD_LEVEL_COUNT; i++)
                {
                    List <AsyncLoadResParam> cAsyncLoadResList = m_asyncAssetLoadingList[i];
                    if (m_asyncAssetLoadingList[i] == null)
                    {
                        continue;
                    }
                    if (cAsyncLoadResList.Count <= 0)
                    {
                        continue;
                    }

                    AsyncLoadResParam asyncLoadResParam = cAsyncLoadResList[0];
                    cAsyncLoadResList.RemoveAt(0);
                    callBackList = asyncLoadResParam.m_asyncCallBacks;

                    Object        obj           = null;
                    MResourceItem mResourceItem = null;
#if UNITY_EDITOR
                    if (!m_isLoadFormAssetBundle)
                    {
                        obj = LoadAssetFormEditor <Object>(asyncLoadResParam.m_resPath);
                        //模拟异步
                        yield return(new WaitForSeconds(0.2f));

                        mResourceItem = MAssetBundleManager.singleton.FindResourceItem(asyncLoadResParam.m_crc);
                        if (mResourceItem == null)
                        {
                            mResourceItem       = new MResourceItem();
                            mResourceItem.m_crc = asyncLoadResParam.m_crc;
                        }
                    }
#endif
                    if (obj == null)
                    {
                        mResourceItem = MAssetBundleManager.singleton.LoadResourcesAssetBundle(asyncLoadResParam.m_crc);
                        if (mResourceItem != null && mResourceItem.m_assetBundle != null)
                        {
                            AssetBundleRequest assetBundleRequest = null;
                            if (asyncLoadResParam.m_isSprite)
                            {
                                assetBundleRequest = mResourceItem.m_assetBundle.LoadAssetAsync <Sprite>(mResourceItem.m_assetName);
                            }
                            else
                            {
                                assetBundleRequest = mResourceItem.m_assetBundle.LoadAssetAsync(mResourceItem.m_assetName);
                            }
                            yield return(assetBundleRequest);

                            if (assetBundleRequest.isDone)
                            {
                                obj = assetBundleRequest.asset;
                            }
                            lastReturnTime = System.DateTime.Now.Ticks;
                        }
                    }
                    //资源缓存
                    CacheResource(asyncLoadResParam.m_resPath, ref mResourceItem, asyncLoadResParam.m_crc, obj, callBackList.Count);
                    //处理加载完成的回调
                    for (int z = 0; z < callBackList.Count; z++)
                    {
                        AsyncCallBack callBack = callBackList[z];

                        //------------------------异步加载处理需要实例化的资源----------------------------
                        if (callBack != null && callBack.m_onAsyncLoadObjectFinished != null && callBack.m_resourceObjectItem != null)
                        {
                            MResourceObjectItem mResourceObjectItem = callBack.m_resourceObjectItem;
                            callBack.m_resourceObjectItem.m_resItem = mResourceItem;
                            callBack.m_onAsyncLoadObjectFinished(asyncLoadResParam.m_resPath, callBack.m_resourceObjectItem, callBack.m_resourceObjectItem.m_parms);
                            callBack.m_onAsyncLoadObjectFinished = null;
                        }

                        //------------------------异步加载处理不需要实例化的Object资源----------------------------
                        if (callBack != null && callBack.m_onAsyncLoadFinished != null)
                        {
                            callBack.m_onAsyncLoadFinished(asyncLoadResParam.m_resPath, obj, callBack.m_parms);
                            callBack.m_onAsyncLoadFinished = null;
                        }
                        //异步加载CallBack对象回收
                        callBack.Reset();
                        m_asyncCallBackPool.Recycle(callBack);
                    }

                    obj = null;
                    callBackList.Clear();
                    //从正在异步加载的Dic里面移除
                    m_asyncLoadingAssetDic.Remove(mResourceItem.m_crc);

                    //异步加载中间对象回收
                    asyncLoadResParam.Reset();
                    m_asyncLoadResParamPool.Recycle(asyncLoadResParam);

                    //上下写了两个同样的Return逻辑 是因为如果加载比较大的资源 可能在这个For循环内就需要多帧 所以这里也做了一个判定
                    if (!isYieldReturn || System.DateTime.Now.Ticks - lastReturnTime > MAX_WAITTIME)
                    {
                        lastReturnTime = System.DateTime.Now.Ticks;
                        yield return(null);

                        isYieldReturn = true;
                    }
                }

                if (!isYieldReturn || System.DateTime.Now.Ticks - lastReturnTime > MAX_WAITTIME)
                {
                    lastReturnTime = System.DateTime.Now.Ticks;
                    yield return(null);
                }
            }
        }
Example #22
0
        /// <summary>
        /// 是否由对象池创建的
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool IsObjectManagerCreate(GameObject obj)
        {
            MResourceObjectItem mResourceObjectItem = m_resourceObjectDic[obj.GetInstanceID()];

            return(mResourceObjectItem != null);
        }
Example #23
0
        /// <summary>
        /// ObjectManager的释放 对创建出来的Gameobject的释放
        /// </summary>
        /// <param name="gameObject">释放对象</param>
        /// <param name="maxCatchCount">最大缓存个数</param>
        /// <param name="destoryCompletly">是否彻底删除</param>
        /// <param name="isToRecycleParent">是非设置默认父节点</param>
        public void ReleaseObject(GameObject gameObject, int maxCatchCount = -1, bool destoryCompletly = false, bool isToRecycleParent = true)
        {
            if (gameObject == null)
            {
                return;
            }

            int tempInstanceId = gameObject.GetInstanceID();
            MResourceObjectItem mResourceObjectItem = null;

            if (!m_resourceObjectDic.TryGetValue(tempInstanceId, out mResourceObjectItem))
            {
                MDebug.singleton.AddErrorLog("这个对象不是用ObjectManager创建的GameObject Name:" + gameObject.name);
                return;
            }
            if (mResourceObjectItem == null)
            {
                MDebug.singleton.AddErrorLog("本地没有缓存这个GameObject Name:" + gameObject.name);
                return;
            }
            if (mResourceObjectItem.m_isAlreadyRelease)
            {
                MDebug.singleton.AddErrorLog("重复释放对象 Name:" + gameObject.name);
                return;
            }

#if UNITY_EDITOR
            gameObject.name += "(Recycle)";
#endif
            List <MResourceObjectItem> listObjectItems = null;
            //maxCatchCount == 0 说明不缓存 对象释放
            if (maxCatchCount == 0)
            {
                m_resourceObjectDic.Remove(tempInstanceId);
                MResourceManager.singleton.ReleaseResource(mResourceObjectItem, destoryCompletly);
                mResourceObjectItem.Reset();
                m_resourceObjectClssPool.Recycle(mResourceObjectItem);
            }
            else
            {
                if (!m_resourcesItemPoolDic.TryGetValue(mResourceObjectItem.m_crc, out listObjectItems) || listObjectItems == null)
                {
                    listObjectItems = new List <MResourceObjectItem>();
                    m_resourcesItemPoolDic.Add(mResourceObjectItem.m_crc, listObjectItems);
                }

                if (mResourceObjectItem.m_cloneObeject != null)
                {
                    if (isToRecycleParent)
                    {
                        mResourceObjectItem.m_cloneObeject.transform.SetParent(RecycleObjectPoolTrans);
                    }
                    else
                    {
                        mResourceObjectItem.m_cloneObeject.SetActiveEx(false);
                    }
                }

                //没有达到最大缓存个数的时候 进行缓存
                if (maxCatchCount < 0 || listObjectItems.Count < maxCatchCount)
                {
                    listObjectItems.Add(mResourceObjectItem);
                    mResourceObjectItem.m_isAlreadyRelease = true;
                    MResourceManager.singleton.DeCreaseResourceRef(mResourceObjectItem.m_crc);
                }
                else
                {
                    //多出来的资源卸载掉
                    m_resourceObjectDic.Remove(tempInstanceId);
                    MResourceManager.singleton.ReleaseResource(mResourceObjectItem, destoryCompletly);
                    mResourceObjectItem.Reset();
                    m_resourceObjectClssPool.Recycle(mResourceObjectItem);
                }
            }
            //todo
        }
    static int ReleaseResource(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 2 && TypeChecker.CheckTypes <MFrameWork.MResourceObjectItem>(L, 2))
            {
                MFrameWork.MResourceManager    obj  = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                MFrameWork.MResourceObjectItem arg0 = (MFrameWork.MResourceObjectItem)ToLua.ToObject(L, 2);
                bool o = obj.ReleaseResource(arg0);
                LuaDLL.lua_pushboolean(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <UnityEngine.Object>(L, 2))
            {
                MFrameWork.MResourceManager obj  = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                UnityEngine.Object          arg0 = (UnityEngine.Object)ToLua.ToObject(L, 2);
                bool o = obj.ReleaseResource(arg0);
                LuaDLL.lua_pushboolean(L, o);
                return(1);
            }
            else if (count == 2 && TypeChecker.CheckTypes <string>(L, 2))
            {
                MFrameWork.MResourceManager obj = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                string arg0 = ToLua.ToString(L, 2);
                bool   o    = obj.ReleaseResource(arg0);
                LuaDLL.lua_pushboolean(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <MFrameWork.MResourceObjectItem, bool>(L, 2))
            {
                MFrameWork.MResourceManager    obj  = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                MFrameWork.MResourceObjectItem arg0 = (MFrameWork.MResourceObjectItem)ToLua.ToObject(L, 2);
                bool arg1 = LuaDLL.lua_toboolean(L, 3);
                bool o    = obj.ReleaseResource(arg0, arg1);
                LuaDLL.lua_pushboolean(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <UnityEngine.Object, bool>(L, 2))
            {
                MFrameWork.MResourceManager obj  = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                UnityEngine.Object          arg0 = (UnityEngine.Object)ToLua.ToObject(L, 2);
                bool arg1 = LuaDLL.lua_toboolean(L, 3);
                bool o    = obj.ReleaseResource(arg0, arg1);
                LuaDLL.lua_pushboolean(L, o);
                return(1);
            }
            else if (count == 3 && TypeChecker.CheckTypes <string, bool>(L, 2))
            {
                MFrameWork.MResourceManager obj = (MFrameWork.MResourceManager)ToLua.CheckObject <MFrameWork.MResourceManager>(L, 1);
                string arg0 = ToLua.ToString(L, 2);
                bool   arg1 = LuaDLL.lua_toboolean(L, 3);
                bool   o    = obj.ReleaseResource(arg0, arg1);
                LuaDLL.lua_pushboolean(L, o);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: MFrameWork.MResourceManager.ReleaseResource"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }