Unity3d给纹理图片加密

如果不想让其他人较轻易的获取包体里的图片,可以通过给图片加密。
步骤:
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拖到脚本上
在这里插入图片描述

在这里插入图片描述

点击运行:
在这里插入图片描述
完结。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值