Unity3D内容加密保护

仅管资源 (Assets) 在传输时可使用加密进行保护,,但在数据流入客户手中后,其内容就有可能被获取。例如,有工具可记录驱动程序级别上的 3D 数据,允许用户提取传送至 GPU 的模型和纹理。因此,我们通常希望在用户决定提取资源时,能够满足其要求。

当然,如果您需要,也可以对资源包 (AssetBundle) 文件使用自己的数据加密。

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

string url = "";IEnumerator Start () {// Start a download of the encrypted assetbundleWWW www = new (url, 1);// Wait for download to completeyield return www;// Load the TextAsset from the AssetBundleTextAsset textAsset = ("EncryptedData", typeof(TextAsset));// Get the byte databyte[] encryptedData = textAsset.bytes;// Decrypt the AssetBundle databyte[] decryptedData = YourDecryptionMethod(encryptedData);// Use your byte array.The AssetBundle will be cached}

另一可用方法是对资源中的资源包 (AssetBundles) 完全加密,然后使用 WWW 类下载资源包。只要服务器将其作为二进制数据提供 ,则可用任何您喜欢的文件扩展名命名。下载完成后,您可以使用 WWW 实例的 .bytes 属性数据相关的解密程序获取解密的资源包 (AssetBundle) 文件数据,并使用AssetBundle.CreateFromMemory 在内存中创建资源包 (AssetBundle)。

string url = "";IEnumerator Start () {// Start a download of the encrypted assetbundleWWW www = new WWW (url);// Wait for download to completeyield return www;// Get the byte databyte[] encryptedData = ;// Decrypt the AssetBundle databyte[] decryptedData = YourDecryptionMethod(encryptedData);// Create an AssetBundle from the bytes arrayAssetBundle 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 = "";IEnumerator Start () {// Start a download of the encrypted assetbundleWWW www = new (url, 1);// Wait for download to completeyield return www;// Load the TextAsset from the AssetBundleTextAsset textAsset = ("EncryptedData", typeof(TextAsset));// Get the byte databyte[] encryptedData = textAsset.bytes;// Decrypt the AssetBundle databyte[] decryptedData = YourDecryptionMethod(encryptedData);// Create an AssetBundle from the bytes arrayAssetBundle bundle = AssetBundle.CreateFromMemory(decryptedData);// You can now use your AssetBundle.The wrapper AssetBundle is cached}

坚守自己的原则,世界上的诱-惑很多,

Unity3D内容加密保护

相关文章:

你感兴趣的文章:

标签云: