// Assets/Editor/AssetBundleListGenerator.cs using System.Collections.Generic; using System.IO; using Kill.Managers; using UnityEditor; using UnityEngine; public class AssetBundleListGenerator : EditorWindow { private string folderPath = ""; private string outputPath = ""; private string version = "1.0.0"; // 添加版本号字段 // EditorPrefs 键名 private const string PREF_VERSION = "AssetBundleListGenerator_Version"; private const string PREF_FOLDER_PATH = "AssetBundleListGenerator_FolderPath"; private const string PREF_OUTPUT_PATH = "AssetBundleListGenerator_OutputPath"; [MenuItem("Tools/AssetBundle/List Generator")] public static void ShowWindow() { GetWindow("AB List Generator"); } private void OnEnable() { // 从 EditorPrefs 加载保存的值 version = EditorPrefs.GetString(PREF_VERSION, "1.0.0"); folderPath = EditorPrefs.GetString(PREF_FOLDER_PATH, ""); outputPath = EditorPrefs.GetString(PREF_OUTPUT_PATH, ""); } /// /// 保存设置到 EditorPrefs /// private void SaveSettings() { EditorPrefs.SetString(PREF_VERSION, version); EditorPrefs.SetString(PREF_FOLDER_PATH, folderPath); EditorPrefs.SetString(PREF_OUTPUT_PATH, outputPath); } void OnGUI() { GUILayout.Label("AssetBundle List Generator", EditorStyles.boldLabel); EditorGUILayout.Space(); // 添加版本号输入字段 version = EditorGUILayout.TextField("Version", version); EditorGUILayout.Space(); GUILayout.Label("选择包含.ab文件的目录:", EditorStyles.label); EditorGUILayout.BeginHorizontal(); folderPath = EditorGUILayout.TextField("目录路径", folderPath); if (GUILayout.Button("浏览", GUILayout.Width(60))) { string selectedPath = EditorUtility.OpenFolderPanel("选择.ab文件目录", "", ""); if (!string.IsNullOrEmpty(selectedPath)) { folderPath = selectedPath; } } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); GUILayout.Label("输出文件路径:", EditorStyles.label); EditorGUILayout.BeginHorizontal(); outputPath = EditorGUILayout.TextField("JSON路径", outputPath); if (GUILayout.Button("浏览", GUILayout.Width(60))) { string selectedPath = EditorUtility.SaveFilePanel("保存list.json", "", "list.json", "json"); if (!string.IsNullOrEmpty(selectedPath)) { outputPath = selectedPath; } } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button("生成List")) { if (!string.IsNullOrEmpty(folderPath) && Directory.Exists(folderPath)) { if (string.IsNullOrEmpty(outputPath)) { EditorUtility.DisplayDialog("错误", "请选择输出文件路径", "确定"); return; } GenerateAssetBundleList(); } else { EditorUtility.DisplayDialog("错误", "请选择有效的目录路径", "确定"); } } } private void GenerateAssetBundleList() { try { List bundleInfos = new List(); // 获取目录下所有.ab文件和.ab.manifest文件 string[] abFiles = Directory.GetFiles(folderPath, "*.ab", SearchOption.AllDirectories); string[] manifestFiles = Directory.GetFiles(folderPath, "*.ab.manifest", SearchOption.AllDirectories); foreach (string filePath in abFiles) { FileInfo fileInfo = new FileInfo(filePath); string fileName = Path.GetFileName(filePath); // 创建AssetBundleInfo对象 AssetBundleInfo info = new AssetBundleInfo() { name = fileName, size = fileInfo.Length, md5 = GetMD5HashFromFile(filePath) }; bundleInfos.Add(info); } // 添加.manifest文件 foreach (string filePath in manifestFiles) { FileInfo fileInfo = new FileInfo(filePath); string fileName = Path.GetFileName(filePath); // 保留.manifest后缀 // 创建AssetBundleInfo对象 AssetBundleInfo info = new AssetBundleInfo() { name = fileName, size = fileInfo.Length, md5 = GetMD5HashFromFile(filePath) }; bundleInfos.Add(info); } // 创建包装类并设置版本号 AssetBundleInfoList infoList = new AssetBundleInfoList(bundleInfos) { version = version // 设置版本号 }; // 序列化为JSON string jsonContent = JsonUtility.ToJson(infoList, true); // 确保输出目录存在 string outputDirectory = Path.GetDirectoryName(outputPath); if (!Directory.Exists(outputDirectory)) { Directory.CreateDirectory(outputDirectory); } // 写入文件 File.WriteAllText(outputPath, jsonContent); // 如果输出路径在Assets目录内,则刷新AssetDatabase if (outputPath.Contains(Application.dataPath)) { string assetPath = "Assets" + outputPath.Substring(Application.dataPath.Length); AssetDatabase.ImportAsset(assetPath); AssetDatabase.Refresh(); } // 保存设置 SaveSettings(); EditorUtility.DisplayDialog("成功", $"已生成list.json,共{bundleInfos.Count}个AssetBundle,版本号: {version}", "确定"); } catch (System.Exception e) { EditorUtility.DisplayDialog("错误", $"生成过程中出现错误: {e.Message}", "确定"); Debug.LogError(e); } } private string GetMD5HashFromFile(string filePath) { using (var md5 = System.Security.Cryptography.MD5.Create()) { using (var stream = File.OpenRead(filePath)) { byte[] hash = md5.ComputeHash(stream); return System.BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant(); } } } }