/// <summary>
/// 1、下载资源包后,把包中的加密文本资源数据进行解密
/// </summary>
IEnumerator DownLoadEncryptedData1()
{
string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
while (!Caching.ready)
yield return null;
//下载加密数据
WWW www = WWW.LoadFromCacheOrDownload(url, 1);
yield return www;
//从资源包中加载TextAsset
TextAsset textAsset = www.assetBundle.LoadAsset<TextAsset>("EncryptedData");
//获取加密的字节数据
byte[] encryptedData = textAsset.bytes;
//使用自己的解密方法进行解密处理
byte[] decryptedData = YourDecryptionMethod(encryptedData);
//接下来就可以使用字节数据,而AssetBundle将被缓存……
}
第二个方法的好处是,我们可以使用除了WWW.LoadFromCacheOrDownload以外的任何方法来传输字节数据(因为 LoadFromCacheOrDownload涉及到版本号,只能下载AssetBundle资源包,不能用于字节数据),而且数据是被完全加密的,例如我们可以使用WWW加载或者使用Socket传输。缺点是无法使用Unity的自动缓存功能进行缓存,我们可以在磁盘上手动存储文件,并使用AssetBundles.CreateFromFile来加载存储的文件。/// <summary>
/// 2、直接获取下载的字节数据,进行解密后,从数据中创建AssetBundle
/// </summary>
/// <returns></returns>
IEnumerator DownLoadEncryptedData2()
{
//下载加密的资源包
WWW www = new WWW(url);
yield return www;
//获取资源包的字节数据
byte[] encryptedData = www.bytes;
////使用特定的解密程序解密资源包数据(目前解密后的数据存储在内存中)
//byte[] decryptedData = YourDecryptionMethod(encryptedData);
////从内存中加载AssetBundleCreateRequest,然后获取AssetBundle
//AssetBundleCreateRequest acr = AssetBundle.LoadFromMemoryAsync(decryptedData);
//yield return acr;
//AssetBundle bundle = acr.assetBundle;
//接下来可以使用AssetBundle了,AssetBundle没有被缓存
……
}
/// <summary>
/// 3、下载资源包后,获取包中的文本资源,进行解密后,将文本资源中的AssetBundle加载出来
/// </summary>
IEnumerator DownLoadEncryptedData3()
{string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
while (!Caching.ready)
yield return null;
WWW www = WWW.LoadFromCacheOrDownload(url, 1);
yield return www;
//从资源包中加载TextAsset类型的资源
TextAsset textAsset = www.assetBundle.LoadAsset<TextAsset>("EncryptedData");
//得到字节数据
byte[] encryptedData = textAsset.bytes;
//进行解密处理
byte[] decryptedData = YourDecryptionMethod(encryptedData);
//然后从解密后的字节数组中创建AssetBundle
AssetBundleCreateRequest acr = AssetBundle.LoadFromMemoryAsync(decryptedData);
yield return acr;
AssetBundle bundle = acr.assetBundle;
//接下来可以使用AssetBundle了,而且AssetBundle被自动缓存
……
}