killapp/Assets/Editor/YqcTools.cs
2026-03-30 16:25:00 +08:00

152 lines
5.2 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 UnityEditor;
using UnityEngine;
using Kill.Base;
public class AssetBundleListGenerator : EditorWindow
{
private string folderPath = "";
private string outputPath = "";
private string version = "1.0.0"; // 添加版本号字段
[MenuItem("Tools/AssetBundle/List Generator")]
public static void ShowWindow()
{
GetWindow<AssetBundleListGenerator>("AB List Generator");
}
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文件
string[] abFiles = Directory.GetFiles(folderPath, "*.ab", SearchOption.AllDirectories);
foreach (string filePath in abFiles)
{
FileInfo fileInfo = new FileInfo(filePath);
string fileName = Path.GetFileNameWithoutExtension(filePath);
// 创建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();
}
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();
}
}
}
}