如果不想让其他人较轻易的获取包体里的图片,可以通过给图片加密。
步骤:
1.先将图集打包成AssetBundle
2.给图集AssetBundle加密,并拷贝到StreamingAssets文件夹里
3.在游戏中加载AssetBundle,然后解密,读取纹理到内存中
4.调用并显示图片
1.先将图集打包成AssetBundle,这里我使用的是Texturepack这个软件打包出来的纹理图集
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
using System.Text;
public static class ImageEncryption
{
private static string TEMP_ASSETBUNDLE_PATH = Application.dataPath + "/TempAssetBundle";
private static string ASSETBUNDLE_NAME = "GameViewTP.assetbundle";
//将纹理打包成Assetbundle
[MenuItem("Encryption/1.CreateImageAssetBundle")]
public static void CreateImageAssetBundle()
{
List<AssetBundleBuild> builds = new List<AssetBundleBuild>();
AssetBundleBuild build1 = new AssetBundleBuild();
build1.assetBundleName = ASSETBUNDLE_NAME;
build1.assetNames = new string[] { "Assets/Sprites/GameViewTP.png", "Assets/Sprites/GameViewTP.tpsheet" };
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();
}
}
点击执行
生成如下文件
2.给红包图集AssetBundle加密,并拷贝到StreamingAssets文件夹里
//纹理图集加密,并拷贝到StreamingAssets文件夹里
[MenuItem("Encryption/2.EncryptExPackImage")]
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 = CryptoBase.EncryptEx(bytes);
string targetPath = Application.streamingAssetsPath;
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
File.WriteAllBytes(targetPath + "/" + ASSETBUNDLE_NAME.ToLower(), encryptedBytes);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
}
点击执行
在StreamingAssets生成gameviewtp.assetbundle
3.在游戏中加载AssetBundle,然后解密,读取纹理到内存中
创建一个新的脚本ReadTexture.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class ReadTexture : MonoBehaviour
{
private bool isTextureLoadFinish;
private Dictionary<string, Sprite> m_spriteCatch = new Dictionary<string, Sprite>();
public Image image1, image2;
// Start is called before the first frame update
void Start()
{
StartCoroutine(LoadPackImage());
StartCoroutine(ShowImage());
}
private IEnumerator LoadPackImage()
{
string path;
#if UNITY_ANDROID && !UNITY_EDITOR
path = Application.streamingAssetsPath + "/gameviewtp.assetbundle";
#else
path = "file://" + Application.streamingAssetsPath + "/gameviewtp.assetbundle";
#endif
var uwr = UnityWebRequest.Get(path);
yield return uwr.SendWebRequest();
//字节数组解密算法
byte[] decryptedBytes = CryptoBase.DecryptEx(uwr.downloadHandler.data);
AssetBundle bundle1 = AssetBundle.LoadFromMemory(decryptedBytes);
Sprite[] sprites = bundle1.LoadAllAssets<Sprite>();
foreach (Sprite sp in sprites)
{
try
{
if (!m_spriteCatch.ContainsKey(sp.name))
m_spriteCatch.Add(sp.name, sp);
else
Debug.LogError("sp.name already loaded " + sp.name);
}
catch (Exception e)
{
Debug.LogError("sp.name" + e);
}
}
isTextureLoadFinish = true;
}
private IEnumerator ShowImage()
{
yield return new WaitUntil(() => { return isTextureLoadFinish; });
image1.sprite = m_spriteCatch["flower"];
image2.sprite = m_spriteCatch["coinbox"];
}
}
创建一个场景,并在场景上挂载该脚本,并添加两个Image,把Image拖到脚本上
点击运行:

完结。