static void GenerateAssetVersionInfo() { string platform = GameFrameworkCommon.GetPlatformName(); string dirpath = System.IO.Path.Combine(UnityEngine.Application.dataPath.Replace("Assets", ""), "AssetBundles/" + platform); AssetBundleVersionInfo abversion = new AssetBundleVersionInfo(); abversion.IsEncrypt = false; abversion.Version = 10000; abversion.ManifestAssetBundle = platform; var infos = new List <ResourcesInfo>(); var resources = CalculateMd5(dirpath); foreach (var item in resources) { ResourcesInfo info = new ResourcesInfo(); info.Name = item.Key.Substring(item.Key.IndexOf(platform) + platform.Length + 1).Replace(@"\\", "/"); info.MD5 = item.Value; infos.Add(info); } abversion.Resources = infos; string assetversion = EditorJsonUtility.ToJson(abversion); System.IO.File.WriteAllText(System.IO.Path.Combine(ResourceManager.GetDeafultPath(PathType.ReadOnly), CheckResourceState.AssetVersionTxt), assetversion); ATFileOp.ShowExplorerWindow(System.IO.Path.Combine(ResourceManager.GetDeafultPath(PathType.ReadOnly), CheckResourceState.AssetVersionTxt)); }
public static void BuildAssetBundles() { string buildPath = EditorConfigInfo.BuildPath; if (!Directory.Exists(buildPath)) { Debug.LogError("Please set build path!"); return; } //打包资源 Debug.Log("开始打包!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); BuildAssetBundleOptions option = (BuildAssetBundleOptions)EditorConfigInfo.ZipMode; BuildTarget target = (BuildTarget)EditorConfigInfo.BuildTarget; BuildPipeline.BuildAssetBundles(buildPath, option, target); Debug.Log("打包完成!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); //资源加密 if ((EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES) { string keyPath = Application.dataPath + "/Resources"; if (!Directory.Exists(keyPath)) { Directory.CreateDirectory(keyPath); } if (!File.Exists(keyPath + "/Key.asset")) { EnciphererKey ek = ScriptableObject.CreateInstance <EnciphererKey>(); ek.GeneraterKey(); AssetDatabase.CreateAsset(ek, "Assets/Resources/Key.asset"); } EnciphererKey keyAsset = Resources.Load("Key") as EnciphererKey; DirectoryInfo dir = new DirectoryInfo(buildPath); FileSystemInfo[] infos = dir.GetFileSystemInfos(); Debug.Log("开始加密!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); foreach (FileSystemInfo info in infos) { if (info is FileInfo) { if (info.Extension == "") { byte[] bs = File.ReadAllBytes(info.FullName); byte[] cipbs = Encipherer.AESEncrypt(bs, keyAsset); File.WriteAllBytes(info.FullName, cipbs); Debug.Log("完成资源包 " + info.Name + " 的加密!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); } } } Debug.Log("加密完成!" + System.DateTime.Now.ToString("HH:mm:ss:fff")); } //写入版本号信息 string assetVersionPath = buildPath + "/AssetVersion.txt"; AssetBundleVersionInfo version = new AssetBundleVersionInfo(); version.Version = EditorConfigInfo.AssetVersion; version.IsEncrypt = (EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES; version.Resources = new List <ResourcesInfo>(); int index = buildPath.LastIndexOf("/", StringComparison.Ordinal); if (index > 0) { version.ManifestAssetBundle = buildPath.Substring(index + 1, buildPath.Length - index - 1); } DirectoryInfo dir1 = new DirectoryInfo(buildPath); FileSystemInfo[] infos1 = dir1.GetFileSystemInfos(); foreach (FileSystemInfo info in infos1) { if (info is FileInfo) { if (info.Extension == "") { ResourcesInfo ri = new ResourcesInfo(); ri.Name = info.Name; ri.Hash = info.GetHashCode().ToString(); version.Resources.Add(ri); } } } string content = JsonUtility.ToJson(version); File.WriteAllText(assetVersionPath, content); //版本号迭代 EditorConfigInfo.AssetVersion += 1; SaveEditorConfigInfo(); AssetDatabase.Refresh(); //打开打包文件夹 EditorUtility.OpenWithDefaultApp(buildPath); }
public static void BuildAssetBundles() { string buildPath = EditorConfigInfo.BuildPath; if (!Directory.Exists(buildPath)) { Debug.LogError("Please set build path!"); return; } //平台资源的信息 string platformVersionPath = Path.Combine(buildPath, "AssetPlatformVersion.txt"); AssetPlatformVersionInfo assetPlatformVersionInfo = new AssetPlatformVersionInfo(); assetPlatformVersionInfo.Platforms = new List <string>(); //资源加密 EnciphererKey keyAsset = null; if ((EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES) { string keyPath = Application.dataPath + "/Resources"; if (!Directory.Exists(keyPath)) { Directory.CreateDirectory(keyPath); } if (!File.Exists(keyPath + "/Key.asset")) { EnciphererKey ek = ScriptableObject.CreateInstance <EnciphererKey>(); ek.GeneraterKey(); AssetDatabase.CreateAsset(ek, "Assets/Resources/Key.asset"); AssetDatabase.Refresh(); } keyAsset = Resources.Load("Key") as EnciphererKey; } //根据各个平台打包 foreach (var item in EditorConfigInfo.BuildTargets) { //打包 BuildTarget target = (BuildTarget)item; string targetName = target.ToString().ToLower(); //添加平台的信息 assetPlatformVersionInfo.Platforms.Add(targetName); string targetBuildPath = Path.Combine(buildPath, targetName); if (!Directory.Exists(targetBuildPath)) { Directory.CreateDirectory(targetBuildPath); } Debug.Log("开始打包--" + targetName + System.DateTime.Now.ToString(" HH:mm:ss:fff")); BuildAssetBundleOptions option = (BuildAssetBundleOptions)EditorConfigInfo.ZipMode; BuildPipeline.BuildAssetBundles(targetBuildPath, option, target); Debug.Log("打包完成--" + targetName + System.DateTime.Now.ToString(" HH:mm:ss:fff")); //写入版本号信息 string assetVersionPath = targetBuildPath + "/AssetVersion.txt"; AssetBundleVersionInfo version = new AssetBundleVersionInfo(); version.ManifestAssetBundle = targetName; version.Version = EditorConfigInfo.AssetVersion; version.IsEncrypt = (EncryptMode)EditorConfigInfo.EncryptMode == EncryptMode.AES; version.Resources = new List <ResourcesInfo>(); List <FileInfo> allFiles = GetAllFiles(targetBuildPath); for (int i = 0; i < allFiles.Count; i++) { FileInfo fileInfo = allFiles[i]; if (fileInfo.Extension == ".manifest") { File.Delete(fileInfo.FullName); } else if (fileInfo.Name == "AssetVersion.txt") { continue; } //加密 else { byte[] bs = File.ReadAllBytes(fileInfo.FullName); if (keyAsset != null) { byte[] cipbs = Encipherer.AESEncrypt(bs, keyAsset); File.WriteAllBytes(fileInfo.FullName, cipbs); //加密后md5的值应该刷新 bs = cipbs; } ResourcesInfo resourcesInfo = new ResourcesInfo(); string fullPath = Path.GetFullPath(targetBuildPath); resourcesInfo.Name = fileInfo.FullName.Replace(fullPath + "\\", ""); System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); //计算字节数组的哈希值 byte[] toData = md5.ComputeHash(bs); string fileMD5 = ""; for (int j = 0; j < toData.Length; j++) { fileMD5 += toData[j].ToString("x2"); } resourcesInfo.MD5 = fileMD5; version.Resources.Add(resourcesInfo); } } //保存AssetVersion文件 string content = JsonUtility.ToJson(version); File.WriteAllText(assetVersionPath, content); } //保存平台信息文件 string platformContent = JsonUtility.ToJson(assetPlatformVersionInfo); File.WriteAllText(platformVersionPath, platformContent); //版本号迭代 EditorConfigInfo.AssetVersion += 1; SaveEditorConfigInfo(); AssetDatabase.Refresh(); //打开打包文件夹 EditorUtility.OpenWithDefaultApp(buildPath); }