加密前:
加密后,直接无法加载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;}}
}