killapp/Assets/IFix/Editor/GetBundleList.cs

117 lines
3.5 KiB
C#
Raw Normal View History

2025-11-18 09:18:48 +08:00
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using System.Security.Cryptography;
using System.Text;
using Kill.Base;
public class AssetBundleInfoGenerator : EditorWindow
{
private string selectedPath = "";
private List<AssetBundleInfo> assetBundleInfos = new List<AssetBundleInfo>();
[MenuItem("Tools/AssetBundle Info Generator")]
public static void ShowWindow()
{
GetWindow<AssetBundleInfoGenerator>("AB Info Generator");
}
private void OnGUI()
{
GUILayout.Label("AssetBundle Info Generator", EditorStyles.boldLabel);
EditorGUILayout.Space();
GUILayout.BeginHorizontal();
selectedPath = EditorGUILayout.TextField("Selected Path", selectedPath);
if (GUILayout.Button("Browse", GUILayout.Width(60)))
{
selectedPath = EditorUtility.OpenFolderPanel("Select AssetBundle Folder", "", "");
}
GUILayout.EndHorizontal();
EditorGUILayout.Space();
if (GUILayout.Button("Generate List.json"))
{
if (!string.IsNullOrEmpty(selectedPath) && Directory.Exists(selectedPath))
{
GenerateAssetBundleInfo();
SaveToJson();
EditorUtility.RevealInFinder(selectedPath + "/list.json");
}
else
{
EditorUtility.DisplayDialog("Error", "Please select a valid folder path.", "OK");
}
}
EditorGUILayout.Space();
// 显示生成的信息预览
if (assetBundleInfos.Count > 0)
{
GUILayout.Label("Preview:", EditorStyles.boldLabel);
foreach (var info in assetBundleInfos)
{
EditorGUILayout.LabelField($"{info.name} - {info.size} bytes - MD5: {info.md5}");
}
}
}
private void GenerateAssetBundleInfo()
{
assetBundleInfos.Clear();
// 获取目录下所有文件
string[] files = Directory.GetFiles(selectedPath, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
// 排除.meta文件和list.json本身
if (!file.EndsWith(".ab"))
continue;
FileInfo fileInfo = new FileInfo(file);
string fileName = Path.GetFileName(file);
AssetBundleInfo info = new AssetBundleInfo
{
name = fileName,
size = fileInfo.Length,
md5 = CalculateMD5(file)
};
assetBundleInfos.Add(info);
}
}
private string CalculateMD5(string filePath)
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(filePath))
{
byte[] hash = md5.ComputeHash(stream);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
}
}
private void SaveToJson()
{
string json = JsonUtility.ToJson(new AssetBundleInfoList(assetBundleInfos), true);
string outputPath = Path.Combine(selectedPath, "list.json");
File.WriteAllText(outputPath, json);
AssetDatabase.Refresh();
Debug.Log($"Generated list.json with {assetBundleInfos.Count} asset bundles.");
}
}