/// <summary> /// 加密文件 /// </summary> private List <string> EncryptFiles(AssetBundleBuilder.BuildParametersContext buildParameters, BuildMapContext buildMapContext) { var encryptionServices = buildParameters.Parameters.EncryptionServices; // 加密资源列表 List <string> encryptList = new List <string>(); // 如果没有设置加密类 if (encryptionServices == null) { return(encryptList); } int progressValue = 0; foreach (var bundleInfo in buildMapContext.BundleInfos) { if (encryptionServices.Check(bundleInfo.BundleName)) { if (bundleInfo.IsRawFile) { UnityEngine.Debug.LogWarning($"Encryption not support raw file : {bundleInfo.BundleName}"); continue; } encryptList.Add(bundleInfo.BundleName); // 注意:通过判断文件合法性,规避重复加密一个文件 string filePath = $"{buildParameters.PipelineOutputDirectory}/{bundleInfo.BundleName}"; byte[] fileData = File.ReadAllBytes(filePath); if (EditorTools.CheckBundleFileValid(fileData)) { byte[] bytes = encryptionServices.Encrypt(fileData); File.WriteAllBytes(filePath, bytes); BuildRunner.Log($"文件加密完成:{filePath}"); } } // 进度条 EditorTools.DisplayProgressBar("加密资源包", ++progressValue, buildMapContext.BundleInfos.Count); } EditorTools.ClearProgressBar(); if (encryptList.Count == 0) { UnityEngine.Debug.LogWarning($"没有发现需要加密的文件!"); } return(encryptList); }
/// <summary> /// 获取AssetBundle对象,如果没有被缓存就重新加载。 /// </summary> public static AssetBundle GetAssetBundle(string filePath) { // 如果文件不存在 if (File.Exists(filePath) == false) { Debug.LogWarning($"Not found asset bundle file : {filePath}"); return(null); } // 验证文件有效性(可能文件被加密) byte[] fileData = File.ReadAllBytes(filePath); if (EditorTools.CheckBundleFileValid(fileData) == false) { Debug.LogWarning($"The asset bundle file is invalid and may be encrypted : {filePath}"); return(null); } if (_loadedAssetBundles.TryGetValue(filePath, out AssetBundle bundle)) { return(bundle); } else { AssetBundle newBundle = AssetBundle.LoadFromFile(filePath); if (newBundle != null) { string[] assetNames = newBundle.GetAllAssetNames(); foreach (string name in assetNames) { newBundle.LoadAsset(name); } _loadedAssetBundles.Add(filePath, newBundle); } return(newBundle); } }