一、创建预制体:用Sphere与Cube临时创建了一个物体,就叫Sphere吧;
二、将这个预制体Sphere打包成assetbundle包,具体介绍可点击这里进行参考(是我之前写的文章);
- 选中Sphere预制体,设置AssetBundle名称为myab;
- 点击Unity菜单栏AssetBundle/AssetBundle_Window按钮。附上本次打AB包的相关代码(此代码需要放在Editor文件夹下):
[MenuItem("AssetBundle/AssetBundle_Window")]
public static void BuildAssetToWindow()
{
string outPath = Application.dataPath + "/../BuildAssetBundle/Window/";
CreateDir(outPath);
BuildPipeline.BuildAssetBundles(outPath,BuildAssetBundleOptions.ForceRebuildAssetBundle, BuildTarget.StandaloneWindows);
AssetDatabase.Refresh();
}
public static void CreateDir(string path)
{
DirectoryInfo info = new DirectoryInfo(path);
if (!info.Exists)
{
info.Create();
}
}
3. 打包好的 AB包如下图所示:
三、对myab资源进行加密(此处用的是恺撒加密),加密后的AB包为myab_Encryption.assetbundle(后缀名可写可不写):
private IEnumerator EncryptionAB()
{
WWW www = new WWW("file:///D:\\fcj\\unity2018\\VuforiaStudy\\BuildAssetBundle\\Window\\myab");
yield return www;
if (www.isDone)
{
if (www.error == null)
{
byte[] bytes = www.bytes;
for (int i = 0; i < bytes.Length; i++)//恺撒加密
{
bytes[i] += 1;
}
File.WriteAllBytes("D:\\fcj\\unity2018\\VuforiaStudy\\BuildAssetBundle\\Window\\myab_Encryption.assetbundle", bytes);
}
}
}
四、 解密并实例化被加密后的AB包:
private IEnumerator DecryptAB()
{
WWW www = new WWW("file:///D:\\fcj\\unity2018\\VuforiaStudy\\BuildAssetBundle\\Window\\myab_Encryption.assetbundle");
yield return www;
if (www.isDone)
{
if (www.error == null)
{
byte[] bytes = www.bytes;
for (int i = 0; i < bytes.Length; i++)//恺撒解密
{
bytes[i] -= 1;
}
AssetBundle ab = AssetBundle.LoadFromMemory(bytes);
GameObject go = ab.LoadAsset("Sphere") as GameObject;//注意这里是预制体名字
Instantiate(go, Vector3.zero, Quaternion.identity);
ab.Unload(false);
}
}
}
五、AES加密:https://blog.csdn.net/weixin_39766005/article/details/103990313