欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > 【Unity3D】AB包加密(AssetBundle加密)

【Unity3D】AB包加密(AssetBundle加密)

2025/1/9 16:28:53 来源:https://blog.csdn.net/qq_39574690/article/details/144993890  浏览:    关键词:【Unity3D】AB包加密(AssetBundle加密)

加密前:

加密后,直接无法加载ab,所以无法正常看到ab内容。

using UnityEngine;
using UnityEditor;
using System.IO;
public static class AssetBundleDemoTest
{[MenuItem("Tools/打包!")]public static void Build(){//注意:StreamingAssets文件夹需手动创建//参数:打包路径(Assets/StreamingAssets文件夹)、打包方式、打包平台(运行平台)BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.ChunkBasedCompression,EditorUserBuildSettings.activeBuildTarget);//刷新编辑器AssetDatabase.Refresh();}[MenuItem("Tools/加密AB")]public static void EncryptAB(){string[] filePaths = Directory.GetFiles(Application.dataPath + "/StreamingAssets");foreach (string filePath in filePaths){byte[] bytes = File.ReadAllBytes(filePath);byte[] newBytes = new byte[bytes.Length + 8];for (int i = 0; i < bytes.Length; i++){newBytes[8 + i] = bytes[i];}File.WriteAllBytes(filePath, newBytes);}}
}

测试代码如下,加载名为"prefab_ab"的AB包,并加载名为"Cube"的资源,是一个预制体。

关于资源加载详情:【Unity】资源加载方式大全_streamingassets manifest-CSDN博客

利用public static AssetBundle LoadFromFile(string path, uint crc, ulong offset);API从磁盘加载AB包,其中crc填0,代表没有任何CRC校验,offset填加密所使用的偏移量8(你可以改,但一定和加密时偏移量一样。) 

using System.Collections.Generic;
using UnityEngine;public class AssetBundleTest : MonoBehaviour
{Dictionary<string, GameObject> resourcesDict = new Dictionary<string, GameObject>();void Start(){GameObject goPrefab = LoadResourceByResourceNameFromAb("prefab_ab", "Cube");if (goPrefab != null){GameObject go = GameObject.Instantiate(goPrefab);}}private GameObject LoadResourceByResourceNameFromAb(string abName, string resourceName){if (!resourcesDict.ContainsKey(abName + "|" + resourceName)){//加载ab包 本地加载方式 一个名为apple的ab包文件AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abName, 0, 8);if (assetBundle != null){//加载源资源时先加载依赖资源//1.加载位于StreamingAssets目录下的StreamingAssets包AssetBundle streamingAssetsBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/StreamingAssets", 0, 8);if (streamingAssetsBundle == null){Debug.Log("streamingAssetsBundle is not exist!");return null;}//加载StreamingAssets包下的AssetBundleManifest文件(可打开StreamingAssets.manifest文件看第三行的名字就是它)AssetBundleManifest assetBundleManifest = streamingAssetsBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");//获取abName包的全部依赖名称!即StreamingAssets.manifest文件的指定Info为abName的Dependencies的数组内容string[] deps = assetBundleManifest.GetAllDependencies(abName);//加载abName包的全部依赖资源for (int i = 0; i < deps.Length; i++){//Debug.Log("依赖资源名称" + deps[i]);//因为依赖名称是一个相对路径,例如:StreamingAssets下的pp文件夹的m2资源是写为 pp/m2,所以加载是没问题AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + deps[i], 0, 8);}//加载源资源GameObject go = assetBundle.LoadAsset<GameObject>(resourceName);if (go != null){resourcesDict.Add(abName + "|" + resourceName, go);return go;}else{Debug.LogWarning("abNmae:" + abName + ",resourceName:" + resourceName + " is not exist!");return null;}}else{Debug.LogWarning("abNmae:" + abName + " is not exist!");return null;}}else{GameObject go = null;resourcesDict.TryGetValue(abName + "|" + resourceName, out go);return go;}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com