killapp/Assets/Editor/YqcTools.cs
“虞渠成” d9e66a2327 feat(editor): 新增一键打包AB资源工具,支持自动生成list.json
新增BuildTool编辑器窗口,集成AssetBundle Browser实现一键打包,自动生成版本化的list.json配置文件,支持同步拷贝到StreamingAssets目录
2026-07-15 16:37:05 +08:00

209 lines
7.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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<AssetBundleListGenerator>("AB List Generator");
}
/// <summary>
/// 静态方法:一键生成 list.json供自动化打包脚本调用
/// </summary>
public static void GenerateList(string folderPath, string outputPath, string version)
{
var window = GetWindow<AssetBundleListGenerator>(utility: true);
window.folderPath = folderPath;
window.outputPath = outputPath;
window.version = version;
window.GenerateAssetBundleList();
window.Close();
}
private void OnEnable()
{
// 从 EditorPrefs 加载保存的值
version = EditorPrefs.GetString(PREF_VERSION, "1.0.0");
folderPath = EditorPrefs.GetString(PREF_FOLDER_PATH, "");
outputPath = EditorPrefs.GetString(PREF_OUTPUT_PATH, "");
}
/// <summary>
/// 保存设置到 EditorPrefs
/// </summary>
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<AssetBundleInfo> bundleInfos = new List<AssetBundleInfo>();
// 获取目录下所有.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();
}
}
}
}