Unity3D內容加密保護,unity3d內容加密
僅管資源 (Assets) 在傳輸時可使用加密進行保護,但在資料流入客戶手中後,其內容就有可能被擷取。例如,有工具可記錄驅動程式層級上的 3D 資料,允許使用者提取傳送至 GPU 的模型和紋理。因此,我們通常希望在使用者決定提取資源時,能夠滿足其要求。
當然,如果您需要,也可以對資源套件 (AssetBundle) 檔案使用自己的資料加密。
一種方法是,使用文本資源 (AssetBundle) 類型將資料存放區為位元組。您可以加密資料檔案,並使用副檔名 .bytes 進行儲存,Unity 會將其視為文本資源 (TextAsset) 類型。在編輯器 ( Editor) 中匯入後,作為文本資源 (TextAssets) 的檔案可匯入將置於伺服器上的資源套件 (AssetBundle)。客戶可以下載資源套件 (AssetBundle) 並將儲存在文本資源 (TextAsset) 中的位元組解密為內容。藉助此方法,既不會對資源套件 (AssetBundles) 加密,又可以將資料作為文本資源 (TextAssets) 儲存。
string url = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";IEnumerator Start () {// Start a download of the encrypted assetbundleWWW www = new WWW.LoadFromCacheOrDownload (url, 1);// Wait for download to completeyield return www;// Load the TextAsset from the AssetBundleTextAsset textAsset = www.assetBundle.Load("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 = "http://www.mywebsite.com/mygame/assetbundles/assetbundle1.unity3d";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 = www.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 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 assetbundleWWW www = new WWW.LoadFromCacheOrDownload (url, 1);// Wait for download to completeyield return www;// Load the TextAsset from the AssetBundleTextAsset textAsset = www.assetBundle.Load("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}