Ejemplo n.º 1
0
        /// <summary>
        /// 保存异步加载获取的AssetBundle
        /// </summary>
        /// <param name="name">AssetBundle名称</param>
        /// <returns></returns>
        public void SaveAssetBundle(AssetBundle assetBundle)
        {
            AssetBundleItem abItem = null;

            uint crc = Crc32.GetCrc32(assetBundle.name);    //根据名称获取Crc值

            if (m_AssetBundleItemDic.TryGetValue(crc, out abItem) && abItem != null)
            {
                abItem.assetBundle = assetBundle;
            }
            else
            {
                Debug.LogError("异步加载资源后,保存资源到字典时没有找到对应Key:" + assetBundle.name);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 加载单个AssetBundle,根据名称
        /// </summary>
        /// <param name="name">AssetBundle名称</param>
        /// <returns></returns>
        public AssetBundle LoadAssetBundle(string name)
        {
            AssetBundleItem abItem = null;
            uint            crc    = Crc32.GetCrc32(name); //根据名称获取Crc值

            if (!m_AssetBundleItemDic.TryGetValue(crc, out abItem))
            {
                AssetBundle assetBundle = null;

                //热更下来的AB包放在Application.persistentDataPath + "/DownLoad" 目录下

                //打包到游戏包里的AB包放在Application.streamingAssetsPath 目录下

                //所以要根据资源文件名称来判断是否是热更的AB包

                string hotAbPath = HotPatchManager.Instance.ComputeABPath(name);

                string fullPath = string.IsNullOrEmpty(hotAbPath) ? ABLoadPath + name : hotAbPath;

                //加密后的ab包要先解密,才能加载
                if (Encrypt)
                {
                    byte[] bytes = AES.AESFileByteDecrypt(fullPath, FrameConstr.m_ABSecretKey);

                    assetBundle = AssetBundle.LoadFromMemory(bytes);
                }
                else
                {
                    assetBundle = AssetBundle.LoadFromFile(fullPath);
                }

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

                abItem             = m_AssetBundleItemPool.Spawn(true);
                abItem.assetBundle = assetBundle;
                abItem.RefCount++;
                m_AssetBundleItemDic.Add(crc, abItem);
            }
            else
            {
                abItem.RefCount++;
            }

            return(abItem.assetBundle);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 卸载AssetBundle
        /// </summary>
        /// <param name="name"></param>
        private void UnloadAssetBundle(string name)
        {
            AssetBundleItem abItem = null;

            uint crc = Crc32.GetCrc32(name); //根据名称获取Crc值

            if (m_AssetBundleItemDic.TryGetValue(crc, out abItem) && abItem != null)
            {
                abItem.RefCount--;
                if (abItem.RefCount <= 0 && abItem.assetBundle != null)
                {
                    abItem.assetBundle.Unload(true);       //卸载掉AssetBundle
                    abItem.Rest();                         //重置这个对象
                    m_AssetBundleItemPool.Recycle(abItem); //回收到对象池子里
                    m_AssetBundleItemDic.Remove(crc);      //从字典中删除这个资源
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 收集异步请求
        /// </summary>
        /// <returns></returns>
        private bool GetAssetBundleRequest(string name, out AssetBundleCreateRequest abRequest, out AssetBundle ab)
        {
            abRequest = null;
            ab        = null;

            AssetBundleItem abItem = null;
            uint            crc    = Crc32.GetCrc32(name); //根据名称获取Crc值

            //没有加载过这个AB包,创建异步加载请求
            if (!m_AssetBundleItemDic.TryGetValue(crc, out abItem))
            {
                //热更下来的AB包放在Application.persistentDataPath + "/DownLoad" 目录下

                //打包到游戏包里的AB包放在Application.streamingAssetsPath 目录下

                //所以要根据资源文件名称来判断是否是热更的AB包
                string hotAbPath = HotPatchManager.Instance.ComputeABPath(name);

                string fullPath = string.IsNullOrEmpty(hotAbPath) ? ABLoadPath + name : hotAbPath;

                //加密后的ab包要先解密,才能加载,但是异步的时候感觉就没有异步的效果了额
                if (Encrypt)
                {
                    byte[] bytes = AES.AESFileByteDecrypt(fullPath, FrameConstr.m_ABSecretKey);

                    abRequest = AssetBundle.LoadFromMemoryAsync(bytes);
                }
                else
                {
                    abRequest = AssetBundle.LoadFromFileAsync(fullPath);
                }

                abItem = m_AssetBundleItemPool.Spawn(true);
                abItem.RefCount++;
                m_AssetBundleItemDic.Add(crc, abItem);
                return(false);
            }
            else
            {
                ab = abItem.assetBundle;
                abItem.RefCount++;
                return(true);
            }
        }