利用AssetBundle加解密游戏所需要的资源

本文介绍了如何在Unity中使用AssetBundle,并通过编辑器扩展实现AssetBundle的打包和加密。在游戏运行时,Mono层负责解密AssetBundle,确保资源安全。详细步骤包括在编辑器创建PictureAssetBundle,存储到StreamingAsset,使用自定义的AesMgr进行加解密操作。
摘要由CSDN通过智能技术生成

首先需要的是一个编辑器扩展,运用到在编辑器里边创建一个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()
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值