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
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM