Unity3D内容加密保护

仅管资源 (Assets) 在传输时可使用加密进行保护,但在数据流入客户手中后。其内容就有可能被获取。比如,有工具可记录驱动程序级别上的 3D 数据,同意用户提取传送至 GPU 的模型和纹理。

因此,我们通常希望在用户决定提取资源时。可以满足其要求。

当然,假设您须要。也能够对资源包 (AssetBundle) 文件使用自己的数据加密。

一种方法是,使用文本资源 (AssetBundle) 类型将数据存储为字节。您能够加密数据文件,并使用扩展名 .bytes 进行保存,Unity 会将其视为文本资源 (TextAsset) 类型。在编辑器 ( Editor) 中导入后。作为文本资源 (TextAssets) 的文件可导入将置于server上的资源包 (AssetBundle)。客户能够下载资源包 (AssetBundle) 并将存储在文本资源 (TextAsset) 中的字节解密为内容。借助此方法。既不会对资源包 (AssetBundles) 加密,又能够将数据作为文本资源 (TextAssets) 保存。

string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
IEnumerator Start () {
// Start a download of the encrypted assetbundle
WWW www = new WWW.LoadFromCacheOrDownload (url, 1);

// Wait for download to complete
yield return www;

// Load the TextAsset from the AssetBundle
TextAsset textAsset = www.assetBundle.Load("EncryptedData", typeof(TextAsset));

// Get the byte data
byte[] encryptedData = textAsset.bytes;

// Decrypt the AssetBundle data
byte[] decryptedData = YourDecryptionMethod(encryptedData);

// Use your byte array.The AssetBundle will be cached
}

还有一可用方法是对资源中的资源包 (AssetBundles) 全然加密。然后使用 WWW 类下载资源包。仅仅要server将其作为二进制数据提供 ,则可用不论什么您喜欢的文件扩展名命名。

下载完毕后。您能够使用 WWW 实例的 .bytes 属性数据相关的解密程序获取解密的资源包 (AssetBundle) 文件数据。并使用 AssetBundle.CreateFromMemory 在内存中创建资源包 (AssetBundle)。

string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
IEnumerator Start () {
// Start a download of the encrypted assetbundle
WWW www = new WWW (url);

// Wait for download to complete
yield return www;

// Get the byte data
byte[] encryptedData = www.bytes;

// Decrypt the AssetBundle data
byte[] decryptedData = YourDecryptionMethod(encryptedData);

// Create an AssetBundle from the bytes array
AssetBundle bundle = AssetBundle.CreateFromMemory(decryptedData);

// You can now use your AssetBundle.The AssetBundle is not cached.
}

另外一种方法之于第一种方法的优势在于。可使用不论什么类函数(AssetBundles.LoadFromCacheOrDownload 除外)传输字节,而且可对数据进行全然加密 – 比如。插件中的套接字。

缺点在于无法使用 Unity 的自己主动缓存功能进行缓存。

可使用全部播放器(网页播放器 (WebPlayer) 除外)在磁盘上手动存储文件,并使用 AssetBundles.CreateFromFile 载入文件。

第三种方法结合了前两种方法的长处,可将资源包 (AssetBundle) 另存为其它普通资源包中的文本资源 (TextAsset)。系统会缓存包括已加密资源包 (AssetBundle) 的未加密资源包。然后会将原始资源包 (AssetBundle) 载入到内存。并使用 AssetBundle.CreateFromMemory 解密并实例化。

string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";
IEnumerator Start () {
// Start a download of the encrypted assetbundle
WWW www = new WWW.LoadFromCacheOrDownload (url, 1);

// Wait for download to complete
yield return www;

// Load the TextAsset from the AssetBundle
TextAsset textAsset = www.assetBundle.Load("EncryptedData", typeof(TextAsset));

// Get the byte data
byte[] encryptedData = textAsset.bytes;

// Decrypt the AssetBundle data
byte[] decryptedData = YourDecryptionMethod(encryptedData);

// Create an AssetBundle from the bytes array
AssetBundle bundle = AssetBundle.CreateFromMemory(decryptedData);

// You can now use your AssetBundle.The wrapper AssetBundle is cached
}
posted @ 2017-04-18 08:57  wzjhoutai  阅读(4263)  评论(1编辑  收藏  举报