众所周知,unity在2013年就放弃flash了,但是有时候,真的会需要到flash,比如web端需要unity web player插件,很多浏览器都拦截了,flash倒不会,因为他是微软的。
u3d在打包flash的时候api是不可以修改的,默认就是flash的api,打包出来就是as(ActionScript代码)编译。所以很多net2.0的api都不能使用,比如System.Security.Cryptography这个库,注:这个库是C#集成好的加密解密
u3d导出flash会报错,因为不支持
资源加密在任何平台任何程序下都是常见的,我这里使用的AES加密。大部分平台都集成了加密解密库,
flash的as加密解密库,下载地址:as crypt 其中很多种方式加密解密。比如 aes、des等等,我使用的aes加密解密
在assets下新建ActionScript文件夹,把下载as加密解密库的放进去,
测试用例,
c#代码
using UnityEngine; using System.Collections; [NotConverted] [NotRenamed] public static class AesCtypt { [NotRenamed] public static string response = "5"; [NotRenamed] public static byte[] AESDecrypt(byte[] cipherText, string strKey,byte[] _key1) { return null; }这个是u3d的脚本语言,解密用,
[NotConverted]
[NotRenamed]
package { import flash.utils.ByteArray; import System.CLIArrayFactory; import System.CLIByteArray; import com.hurlant.util.Hex; import com.hurlant.crypto.symmetric.IPad; import com.hurlant.crypto.symmetric.ICipher; import com.hurlant.crypto.symmetric.IVMode; import com.hurlant.crypto.symmetric.NullPad; import com.hurlant.crypto.Crypto; public class AesCtypt { public static var response : String = ""; public function AesCtypt() { } public static function AESDecrypt(DesByteData:CLIByteArray,SECRET_KEY:String,SECRET_IV:CLIByteArray):CLIByteArray { var inputBA:ByteArray= DesByteData.elements; var key:ByteArray = Hex.toArray(Hex.fromString(SECRET_KEY)); var pad:IPad = new NullPad(); var aes:ICipher = Crypto.getCipher("aes-cbc", key, pad); var ivmode:IVMode = aes as IVMode; ivmode.IV = SECRET_IV.elements; aes.decrypt(inputBA); var aesbytedata:CLIByteArray = new CLIByteArray(); aesbytedata.elements= inputBA; return aesbytedata; } } }
测试用例
using UnityEngine; using System.Collections; public class LoadFlashAssets : MonoBehaviour { string str = ""; public void OnGUI() { if(GUI.Button(new Rect(220,10,100,25), "Calculate")) { string path = "http://100.0.0.0:10001/assets/test.u3d”;//资源路径地址 StartCoroutine(LoadFlashAsset(path)); } GUI.Label(new Rect(220,100,200,200),str); } private string key_3D = “你的密码”; IEnumerator LoadFlashAsset(string path) { WWW loader = new WWW(path); yield return loader; if (loader.error == null && loader.isDone) { AssetBundleCreateRequest request = null; AssetBundle bundle = null; byte[] _key1 = { 这里是你的iv密匙 }; byte[] decryptedDatas = AesCtypt.AESDecrypt(loader.bytes, key_3D,_key1); int length = System.Convert.ToInt32(decryptedDatas[decryptedDatas.Length - 1]); byte[] decryptedData = new byte[decryptedDatas.Length - 1 - length]; for(int i = 0; i < decryptedData.Length; i ++) { decryptedData[i] = decryptedDatas[i]; } request = AssetBundle.CreateFromMemory(decryptedData); yield return request; bundle = request.assetBundle; if (bundle != null) { GameObject obj = GameObject.Instantiate(bundle.mainAsset) as GameObject; } } } }