Exemplo n.º 1
0
 /// <summary>
 /// 卸载一个AssetData和其中的AssetBundle
 /// </summary>
 /// <param name="data"></param>
 public void ReleaseAsset(StorageAssetData data)
 {
     data.bundle.Unload(true);
     if (storageAssetDic.ContainsKey(data.assetName))
     {
         storageAssetDic.Remove(data.assetName);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 加载一个AssetData和其中的AssetBundle
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public StorageAssetData LoadAsset(string fileName)
        {
            if (!File.Exists(fileName))
            {
                Debug.Log("No such File! " + fileName);
                return(null);
            }

            byte[] bytes = File.ReadAllBytes(fileName);

            StorageAssetData data = DecryptAsset(bytes);

            return(data);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 解密Asset
        /// </summary>
        /// <param name="assetBytes"></param>
        /// <returns></returns>
        private StorageAssetData DecryptAsset(byte[] assetBytes)
        {
            StorageAssetData data = null;

            byte[] bt    = null;
            int    index = 0;

            // 读取文件名长度
            int assetNameLen;

            BytesUtility.GetIntFromBytes(assetBytes, out assetNameLen, ref index);
            Debug.Log("Read asset name Length=" + assetNameLen);

            // 读取文件名
            bt = new byte[assetNameLen];
            Array.Copy(assetBytes, index, bt, 0, assetNameLen);
            index += assetNameLen;

            string assetName = Encoding.UTF8.GetString(bt);

            Debug.Log("Read asset name=" + assetName);

            // 读取Asset
            // 判断是否已经加载该资源
            if (storageAssetDic.ContainsKey(assetName))
            {
                data = storageAssetDic[assetName];
                Debug.Log("Has loaded this asset!");
            }
            else
            {
                data           = new StorageAssetData();
                data.assetName = assetName;

                // 读取文件路径长度
                int fileNameLen;
                BytesUtility.GetIntFromBytes(assetBytes, out fileNameLen, ref index);

                Debug.Log("Read asset file name Length=" + fileNameLen);

                // 读取文件路径
                bt = new byte[fileNameLen];
                Array.Copy(assetBytes, index, bt, 0, fileNameLen);
                index += fileNameLen;

                data.assetFileName = Encoding.UTF8.GetString(bt);
                Debug.Log("Read file name=" + data.assetFileName);

                // 读取Asset文件长度
                long fileLen;
                BytesUtility.GetLongFromBytes(assetBytes, out fileLen, ref index);
                Debug.Log("Read file len=" + fileLen);

                byte[] assetData = new byte[fileLen];
                Array.Copy(assetBytes, index, assetData, 0, (int)fileLen);

                data.bundle = AssetBundle.LoadFromMemory(assetData);
                Debug.Log("Read AssetBundle " + data.bundle);

                storageAssetDic.Add(data.assetName, data);
            }


            return(data);
        }