Exemplo n.º 1
0
        /// <summary>
        /// 加载单个assetbundle根据名字
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private AssetBundle LoadAssetBundle(string name)
        {
            AssetBundleItem item = null;
            uint            crc  = CRC32.GetCrc32(name);

            if (!m_AssetBundleItemDic.TryGetValue(crc, out item))
            {
                AssetBundle assetBundle = null;
                string      fullPath    = Application.streamingAssetsPath + "/" + name;
                assetBundle = AssetBundle.LoadFromFile(fullPath);

                if (assetBundle == null)
                {
                    DebugPlus.LogError(" Load AssetBundle Error:" + fullPath);
                }

                item             = m_AssetBundleItemPool.Spawn(true);
                item.assetBundle = assetBundle;
                item.RefCount++;
                m_AssetBundleItemDic.Add(crc, item);
            }
            else
            {
                item.RefCount++;
            }
            return(item.assetBundle);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 同步加载
        /// </summary>
        /// <param name="path"></param>
        /// <param name="bClear"></param>
        /// <returns></returns>
        public GameObject InstantiateObject(string path, bool setSceneObj = false, bool bClear = true)
        {
            uint       crc        = CRC32.GetCrc32(path);
            ResouceObj resouceObj = GetObjectFromPool(crc);

            if (resouceObj == null)
            {
                resouceObj          = m_ResourceObjClassPool.Spawn(true);
                resouceObj.m_Crc    = crc;
                resouceObj.m_bClear = bClear;
                //ResouceManager提供加载方法
                resouceObj = ResourceManager.Instance.LoadResource(path, resouceObj);

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

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

            int tempID = resouceObj.m_CloneObj.GetInstanceID();

            if (!m_ResouceObjDic.ContainsKey(tempID))
            {
                m_ResouceObjDic.Add(tempID, resouceObj);
            }

            return(resouceObj.m_CloneObj);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 预加载资源
        /// </summary>
        /// <param name="path"></param>
        public void PreloadRes(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            uint        crc  = CRC32.GetCrc32(path);
            ResouceItem item = GetCacheResouceItem(crc, 0);

            if (item != null)
            {
                return;
            }

            Object obj = null;

#if UNITY_EDITOR
            if (!m_LoadFormAssetBundle)
            {
                item = AssetBundleManager.Instance.FindResourceItme(crc);
                if (item != null && item.m_Obj != null)
                {
                    obj = item.m_Obj as Object;
                }
                else
                {
                    if (item == null)
                    {
                        item       = new ResouceItem();
                        item.m_Crc = crc;
                    }
                    obj = LoadAssetByEditor <Object>(path);
                }
            }
#endif

            if (obj == null)
            {
                item = AssetBundleManager.Instance.LoadResouceAssetBundle(crc);
                if (item != null && item.m_AssetBundle != null)
                {
                    if (item.m_Obj != null)
                    {
                        obj = item.m_Obj;
                    }
                    else
                    {
                        obj = item.m_AssetBundle.LoadAsset <Object>(item.m_AssetName);
                    }
                }
            }

            CacheResource(path, ref item, crc, obj);
            //跳场景不清空缓存
            item.m_Clear = false;
            ReleaseResource(obj, false);
        }
Exemplo n.º 4
0
        private void UnLoadAssetBundle(string name)
        {
            AssetBundleItem item = null;
            uint            crc  = CRC32.GetCrc32(name);

            if (m_AssetBundleItemDic.TryGetValue(crc, out item) && item != null)
            {
                item.RefCount--;
                if (item.RefCount <= 0 && item.assetBundle != null)
                {
                    item.assetBundle.Unload(true);
                    item.Rest();
                    m_AssetBundleItemPool.Recycle(item);
                    m_AssetBundleItemDic.Remove(crc);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 不需要实例化的资源卸载,根据路径
        /// </summary>
        /// <param name="path"></param>
        /// <param name="destoryObj"></param>
        /// <returns></returns>
        public bool ReleaseResource(string path, bool destoryObj = false)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }

            uint        crc  = CRC32.GetCrc32(path);
            ResouceItem item = null;

            if (!AssetDic.TryGetValue(crc, out item) || null == item)
            {
                DebugPlus.LogError("AssetDic里不存在改资源:" + path + "  可能释放了多次");
            }

            item.RefCount--;

            DestoryResouceItme(item, destoryObj);
            return(true);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 异步加载资源(仅仅是不需要实例化的资源,例如音频,图片等等)
        /// </summary>
        public void AsyncLoadResource(string path, OnAsyncObjFinish dealFinish, LoadResPriority priority, bool isSprite = false, object param1 = null, object param2 = null, object param3 = null, uint crc = 0)
        {
            if (crc == 0)
            {
                crc = CRC32.GetCrc32(path);
            }

            ResouceItem item = GetCacheResouceItem(crc);

            if (item != null)
            {
                if (dealFinish != null)
                {
                    dealFinish(path, item.m_Obj, param1, param2, param3);
                }
                return;
            }

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

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

            //往回调列表里面加回调
            AsyncCallBack callBack = m_AsyncCallBackPool.Spawn(true);

            callBack.m_DealObjFinish = dealFinish;
            callBack.m_Param1        = param1;
            callBack.m_Param2        = param2;
            callBack.m_Param3        = param3;
            para.m_CallBackList.Add(callBack);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 异步对象加载
        /// </summary>
        /// <param name="path"></param>
        /// <param name="fealFinish"></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 bClear = true)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(0);
            }

            uint       crc    = CRC32.GetCrc32(path);
            ResouceObj 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 = ResourceManager.Instance.CreatGuid();

            resObj                  = m_ResourceObjClassPool.Spawn(true);
            resObj.m_Crc            = crc;
            resObj.m_SetSceneParent = setSceneObject;
            resObj.m_bClear         = bClear;
            resObj.m_DealFinish     = dealFinish;
            resObj.m_Param1         = param1;
            resObj.m_Param2         = param2;
            resObj.m_Param3         = param3;
            //调用ResouceManager的异步加载接口
            ResourceManager.Instance.AsyncLoadResource(path, resObj, OnLoadResouceObjFinish, priority);
            return(guid);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 同步资源加载,外部直接调用,仅加载不需要实例化的资源,例如Texture,音频等等
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path"></param>
        /// <returns></returns>
        public T LoadResource <T>(string path) where T : UnityEngine.Object
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            uint        crc  = CRC32.GetCrc32(path);
            ResouceItem item = GetCacheResouceItem(crc);

            if (item != null)
            {
                return(item.m_Obj as T);
            }

            T obj = null;

#if UNITY_EDITOR
            if (!m_LoadFormAssetBundle)
            {
                item = AssetBundleManager.Instance.FindResourceItme(crc);
                if (item != null && item.m_AssetBundle != null)
                {
                    if (item.m_Obj != null)
                    {
                        obj = (T)item.m_Obj;
                    }
                    else
                    {
                        obj = item.m_AssetBundle.LoadAsset <T>(item.m_AssetName);
                    }
                }
                else
                {
                    if (item == null)
                    {
                        item       = new ResouceItem();
                        item.m_Crc = crc;
                    }
                    obj = LoadAssetByEditor <T>(path);
                }
            }
#endif

            if (obj == null)
            {
                item = AssetBundleManager.Instance.LoadResouceAssetBundle(crc);
                if (item != null && item.m_AssetBundle != null)
                {
                    if (item.m_Obj != null)
                    {
                        obj = item.m_Obj as T;
                    }
                    else
                    {
                        obj = item.m_AssetBundle.LoadAsset <T>(item.m_AssetName);
                    }
                }
            }

            CacheResource(path, ref item, crc, obj);
            return(obj);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 同步加载资源,针对给ObjectManager的接口
        /// </summary>
        /// <param name="path"></param>
        /// <param name="resObj"></param>
        /// <returns></returns>
        public ResouceObj LoadResource(string path, ResouceObj resObj)
        {
            if (resObj == null)
            {
                return(null);
            }

            uint crc = resObj.m_Crc == 0 ? CRC32.GetCrc32(path) : resObj.m_Crc;

            ResouceItem item = GetCacheResouceItem(crc);

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

            Object obj = null;

#if UNITY_EDITOR
            if (!m_LoadFormAssetBundle)
            {
                item = AssetBundleManager.Instance.FindResourceItme(crc);
                if (item != null && item.m_Obj != null)
                {
                    obj = item.m_Obj as Object;
                }
                else
                {
                    if (item == null)
                    {
                        item       = new ResouceItem();
                        item.m_Crc = crc;
                    }
                    obj = LoadAssetByEditor <Object>(path);
                }
            }
#endif

            if (obj == null)
            {
                item = AssetBundleManager.Instance.LoadResouceAssetBundle(crc);
                if (item != null && item.m_AssetBundle != null)
                {
                    if (item.m_Obj != null)
                    {
                        obj = item.m_Obj as Object;
                    }
                    else
                    {
                        obj = item.m_AssetBundle.LoadAsset <Object>(item.m_AssetName);
                    }
                }
            }

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

            return(resObj);
        }