Esempio n. 1
0
        //根据单个AbName 加载AB
        private AssetBundle LoadAssetBundle(string assetBundleName)
        {
            MAssetBundleItem assetBundleItem = null;
            uint             crc             = MCrcHelper.GetCRC32(assetBundleName);

            if (!m_assetBundleItemDic.TryGetValue(crc, out assetBundleItem))
            {
                AssetBundle assetBundle = null;
                string      fullPath    = MPathUtils.ASSETBUNDLE_PATH + "/" + assetBundleName;

                if (File.Exists(fullPath))
                {
                    assetBundle = AssetBundle.LoadFromFile(fullPath);
                }

                if (assetBundle == null)
                {
                    MDebug.singleton.AddErrorLog("没有找到这个AssetBundle " + assetBundleName);
                }
                assetBundleItem             = m_assetBundleItemPool.Spawn(true);
                assetBundleItem.assetBundle = assetBundle;
                assetBundleItem.refCount++;
                m_assetBundleItemDic.Add(crc, assetBundleItem);
            }
            else
            {
                assetBundleItem.refCount++;
            }

            return(assetBundleItem.assetBundle);
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        /// <summary>
        /// 资源的预加载 这里和同步加载微小区别 就是不需要引用计数 设置该预先加载的资源 在卸载的时候不卸载
        /// 就是直接加载 然后卸载
        /// </summary>
        /// <param name="resPath">Res path.</param>
        public void PreLoadRes(string resPath)
        {
            if (string.IsNullOrEmpty(resPath))
            {
                return;
            }
            uint crc = MCrcHelper.GetCRC32(resPath);
            //预加载到内存中 不需要进行应用计数
            MResourceItem mResourceItem = GetCacheResourceItem(crc, 0);

            if (mResourceItem != null)
            {
                return;
            }

            Object obj = null;

#if UNITY_EDITOR
            //编辑器模式下 直接从本地拿到资源
            if (!m_isLoadFormAssetBundle)
            {
                mResourceItem = MAssetBundleManager.singleton.FindResourceItem(crc);
                if (mResourceItem.m_object != null)
                {
                    if (mResourceItem.m_object != null)
                    {
                        obj = mResourceItem.m_object;
                    }
                    else
                    {
                        obj = LoadAssetFormEditor <Object>(resPath);
                    }
                }
            }
#endif
            if (obj == null)
            {
                mResourceItem = MAssetBundleManager.singleton.LoadResourcesAssetBundle(crc);
                if (mResourceItem != null && mResourceItem.m_assetBundle != null)
                {
                    if (mResourceItem.m_object != null)
                    {
                        obj = mResourceItem.m_object;
                    }
                    else
                    {
                        obj = mResourceItem.m_assetBundle.LoadAsset <Object>(mResourceItem.m_assetName);
                    }
                }
            }

            CacheResource(resPath, ref mResourceItem, crc, obj);
            mResourceItem.m_clear = false;
            //切换场景不清空缓存
            ReleaseResource(obj, false);
        }
Esempio n. 5
0
        //根据资源全路径返回ResourceItem
        public MResourceItem FindResourceItem(string assetPath)
        {
            uint          crc           = MCrcHelper.GetCRC32(assetPath);
            MResourceItem mResourceItem = null;

            if (m_resourcesItemDic.TryGetValue(crc, out mResourceItem))
            {
                return(mResourceItem);
            }
            return(null);
        }
Esempio n. 6
0
        /// <summary>
        /// 同步资源加载 外部直接调用 仅用于加载不需要实例化的资源 例如Texture 音频等
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="resPath">资源路径</param>
        /// <returns></returns>
        public T LoadResource <T>(string resPath) where T : UnityEngine.Object
        {
            if (string.IsNullOrEmpty(resPath))
            {
                return(null);
            }
            uint          crc           = MCrcHelper.GetCRC32(resPath);
            MResourceItem mResourceItem = GetCacheResourceItem(crc);

            if (mResourceItem != null)
            {
                return(mResourceItem.m_object as T);
            }

            T obj = null;

#if UNITY_EDITOR
            //编辑器模式下 直接从本地拿到资源
            if (!m_isLoadFormAssetBundle)
            {
                mResourceItem = MAssetBundleManager.singleton.FindResourceItem(crc);
                if (mResourceItem.m_object != null)
                {
                    if (mResourceItem.m_object != null)
                    {
                        obj = mResourceItem.m_object as T;
                    }
                    else
                    {
                        obj = LoadAssetFormEditor <T>(resPath);
                    }
                }
            }
#endif
            if (obj == null)
            {
                mResourceItem = MAssetBundleManager.singleton.LoadResourcesAssetBundle(crc);
                if (mResourceItem != null && mResourceItem.m_assetBundle != null)
                {
                    if (mResourceItem.m_object != null)
                    {
                        obj = mResourceItem.m_object as T;
                    }
                    else
                    {
                        obj = mResourceItem.m_assetBundle.LoadAsset <T>(mResourceItem.m_assetName);
                    }
                }
            }

            CacheResource(resPath, ref mResourceItem, crc, obj);
            return(obj);
        }
Esempio n. 7
0
        //释放单个Ab资源
        private void UnLoadAssetBundle(string assetBundleName)
        {
            MAssetBundleItem mAssetBundleItem = null;
            uint             crc = MCrcHelper.GetCRC32(assetBundleName);

            if (m_assetBundleItemDic.TryGetValue(crc, out mAssetBundleItem))
            {
                mAssetBundleItem.refCount--;
                if (mAssetBundleItem.refCount <= 0 && mAssetBundleItem.assetBundle != null)
                {
                    mAssetBundleItem.assetBundle.Unload(true);
                    mAssetBundleItem.Reset();
                    m_assetBundleItemPool.Recycle(mAssetBundleItem);
                    m_assetBundleItemDic.Remove(crc);
                }
            }
        }
Esempio n. 8
0
        /// <summary>
        /// 依据文件路径 不需要实例化的资源的卸载
        /// </summary>
        /// <param name="resPath">Res path.</param>
        /// <param name="destoryCompletly">If set to <c>true</c> destory completly.</param>
        public bool ReleaseResource(string resPath, bool destoryCompletly = false)
        {
            if (string.IsNullOrEmpty(resPath))
            {
                return(false);
            }
            uint          crc           = MCrcHelper.GetCRC32(resPath);
            MResourceItem mResourceItem = null;

            if (!m_resourcesItemDic.TryGetValue(crc, out mResourceItem) && mResourceItem != null)
            {
                MDebug.singleton.AddErrorLog(" m_resourcesItemDic 不存在这个资源 resPath : " + resPath);
                return(false);
            }
            mResourceItem.RefCount--;
            DestoryResourceItem(mResourceItem, destoryCompletly);
            return(true);
        }
Esempio n. 9
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);
        }
Esempio n. 10
0
        /// <summary>
        /// 异步加载资源 仅加载不需要实例化的资源 音频 图片等等
        /// </summary>
        public void AsyncLoadResource(string resPath, OnAsyncLoadFinished onAsyncLoadFinished, LoadResPriority loadResPriority, object[] parms = null)
        {
            if (string.IsNullOrEmpty(resPath))
            {
                return;
            }

            uint          crc           = MCrcHelper.GetCRC32(resPath);
            MResourceItem mResourceItem = GetCacheResourceItem(crc);

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

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

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

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

            m_asyncCallBack.m_onAsyncLoadFinished = onAsyncLoadFinished;
            m_asyncCallBack.m_parms = parms;
            asyncLoadResParam.m_asyncCallBacks.Add(m_asyncCallBack);
        }