首先需要的是一个编辑器扩展,运用到在编辑器里边创建一个PictureAssetBundle打包,然后将PictureBundle打包在TempStreamingAsset下,然后将TempStreamingAsset下的Assetbundle复制到StreamingAsset下,然后给AssetBundle加解密,等下具体代码附上
public static class ImageEncryption
{
private static string TEMP_ASSETBUNDLE_PATH = Application.dataPath + "/TempAssetBundle";
private static string ASSETBUNDLE_NAME = "Picture.assetbundle";
private static string EncryptKey = "SayYes";
//将纹理打包成Assetbundle
[MenuItem("编辑器扩展关于图集/创建PictureBundle")]
public static void CreateImageAssetBundle()
{
List<AssetBundleBuild> builds = new List<AssetBundleBuild>();
AssetBundleBuild build1 = new AssetBundleBuild();
build1.assetBundleName = ASSETBUNDLE_NAME;
build1.assetNames = new string[] {
"Assets/TestTest/8.png", "Assets/TestTest/9.png","Assets/TestTest/10.png","Assets/TestTest/11.png","Assets/TestTest/12.png","Assets/TestTest/13.png","Assets/TestTest/14.png" };
builds.Add(build1);
if (!Directory.Exists(TEMP_ASSETBUNDLE_PATH))
Directory.CreateDirectory(TEMP_ASSETBUNDLE_PATH);
if (BuildPipeline.BuildAssetBundles(TEMP_ASSETBUNDLE_PATH, builds.ToArray(), BuildAssetBundleOptions.None, BuildTarget.Android))
{
Debug.Log("资源打包成功");
}
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
}
[MenuItem("编辑器扩展关于图集/拷贝PictureBundle")]
//纹理图集加密,并拷贝到StreamingAssets文件夹里
public static void EncryptExPackImage()
{
string assetbundle_file_path = Path.Combine(TEMP_ASSETBUNDLE_PATH, ASSETBUNDLE_NAME.ToLower());
byte[] bytes = File.ReadAllBytes(assetbundle_file_path);
//字节数组加密,这个可以自己网上搜一下相应的加密算法
byte[] encryptedBytes = AesMgr.AESEncrypt(bytes,EncryptKey);
string targetPath = Application.streamingAssetsPath;
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
File.WriteAllBytes(targetPath + "/" + ASSETBUNDLE_NAME.ToLower(), encryptedBytes);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
}
}
然后在Mono层解密调用,这个可以做在一个场景里边,然后那个场景加载完全部的数据,保存在一个静态类里边,下边简单附上代码,本来那个dictionary应该是用public static 生命才对
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
public class LoadAssetBundle : MonoBehaviour
{
private bool isTextureLoadFinish;
private Dictionary<string, Sprite> spritecatch = new Dictionary<string, Sprite>();
public Image imageOne, imageTwo;
public string AssetbundleName = "picture";
private string EncryptKey = "SayYes";
// Start is called before the first frame update
void Start()