188 lines
7.3 KiB
C#
188 lines
7.3 KiB
C#
using UnityEditor;
|
||
using UnityEngine;
|
||
|
||
namespace EditorArt_Tools
|
||
{
|
||
/// <summary>
|
||
/// 美术资源Thrift工具菜单
|
||
/// </summary>
|
||
public class ArtResourceThriftMenu
|
||
{
|
||
[MenuItem("美术工具/Thrift/批量生成所有Bytes文件", priority = 100)]
|
||
public static void GenerateAllBytesFiles()
|
||
{
|
||
if (!EditorUtility.DisplayDialog("生成Bytes文件",
|
||
"此操作将:\n" +
|
||
"1. 扫描所有SO表,生成 ArtResourceConfig.bytes\n" +
|
||
"2. 读取图集配置,生成 ArtAtlasConfig.bytes\n\n" +
|
||
"确定继续?",
|
||
"确定", "取消"))
|
||
{
|
||
return;
|
||
}
|
||
|
||
var startTime = System.Diagnostics.Stopwatch.StartNew();
|
||
|
||
try
|
||
{
|
||
// 强制保存所有资源
|
||
AssetDatabase.SaveAssets();
|
||
|
||
// 生成ArtResourceConfig.bytes
|
||
Debug.Log("========== 开始生成 ArtResourceConfig.bytes ==========");
|
||
GenerateArtResourceConfigBytes();
|
||
|
||
// 生成ArtAtlasConfig.bytes
|
||
Debug.Log("========== 开始生成 ArtAtlasConfig.bytes ==========");
|
||
GenerateAtlasConfigBytes();
|
||
|
||
// 刷新资源
|
||
AssetDatabase.Refresh();
|
||
|
||
startTime.Stop();
|
||
|
||
string message = $"✅ 所有Bytes文件生成完成!\n\n" +
|
||
$"总耗时: {startTime.ElapsedMilliseconds} ms\n\n" +
|
||
$"生成文件:\n" +
|
||
$"• Assets/Art_SubModule/Art_Bytes/ArtResourceConfig.bytes\n" +
|
||
$"• Assets/Art_SubModule/Art_Bytes/ArtAtlasConfig.bytes";
|
||
|
||
Debug.Log($"[ArtResourceThriftMenu] {message}");
|
||
EditorUtility.DisplayDialog("生成完成", message, "确定");
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
startTime.Stop();
|
||
string errorMsg = $"❌ 生成失败: {ex.Message}\n\n{ex.StackTrace}";
|
||
Debug.LogError($"[ArtResourceThriftMenu] {errorMsg}");
|
||
EditorUtility.DisplayDialog("生成失败", errorMsg, "确定");
|
||
}
|
||
}
|
||
|
||
private static void GenerateArtResourceConfigBytes()
|
||
{
|
||
// 直接调用ArtResourceConfigEditor的静态方法(需要添加)
|
||
// 或者模拟其逻辑
|
||
var editor = EditorWindow.GetWindow<ArtResourceConfigEditor>();
|
||
if (editor != null)
|
||
{
|
||
// 通过反射调用私有方法
|
||
var method = typeof(ArtResourceConfigEditor).GetMethod("GenerateMergedThriftBytes",
|
||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
||
if (method != null)
|
||
{
|
||
method.Invoke(editor, null);
|
||
}
|
||
editor.Close();
|
||
}
|
||
}
|
||
|
||
private static void GenerateAtlasConfigBytes()
|
||
{
|
||
// 直接调用AtlasBuilderEditor的静态方法(需要添加)
|
||
var editor = EditorWindow.GetWindow<ArtTools.AtlasBuilderEditor>();
|
||
if (editor != null)
|
||
{
|
||
// 通过反射调用私有方法
|
||
var method = typeof(ArtTools.AtlasBuilderEditor).GetMethod("SaveConfigToThriftBytes",
|
||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
||
if (method != null)
|
||
{
|
||
method.Invoke(editor, null);
|
||
}
|
||
editor.Close();
|
||
}
|
||
}
|
||
|
||
[MenuItem("美术工具/Thrift/测试加载Bytes文件", priority = 101)]
|
||
public static void TestLoadBytesFiles()
|
||
{
|
||
Debug.Log("========== 测试加载Bytes文件 ==========");
|
||
|
||
// 测试ArtResourceConfig
|
||
TestLoadArtResourceConfig();
|
||
|
||
// 测试AtlasConfig
|
||
TestLoadAtlasConfig();
|
||
|
||
EditorUtility.DisplayDialog("测试完成", "请查看Console日志", "确定");
|
||
}
|
||
|
||
private static void TestLoadArtResourceConfig()
|
||
{
|
||
const string path = "Assets/Art_SubModule/Art_Bytes/ArtResourceConfig.bytes";
|
||
|
||
var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
|
||
if (textAsset == null)
|
||
{
|
||
Debug.LogError($"❌ 文件不存在: {path}");
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
var config = new Byway.Thrift.Data.ArtResourceConfig();
|
||
using (var transport = new Thrift.Transport.Client.TMemoryBufferTransport(textAsset.bytes, new Thrift.TConfiguration()))
|
||
{
|
||
using (var protocol = new Thrift.Protocol.TBinaryProtocol(transport))
|
||
{
|
||
config.ReadAsync(protocol, System.Threading.CancellationToken.None).GetAwaiter().GetResult();
|
||
}
|
||
}
|
||
|
||
Debug.Log($"✅ ArtResourceConfig.bytes 加载成功!\n" +
|
||
$" 表数量: {config.Tables.Count}\n" +
|
||
$" 版本: {config.Version}\n" +
|
||
$" 生成时间: {config.GenerateTime}");
|
||
|
||
foreach (var table in config.Tables)
|
||
{
|
||
Debug.Log($" 表: {table.TableName} (ID: {table.TableId}, Items: {table.Items.Count})");
|
||
}
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
Debug.LogError($"❌ 加载失败: {ex.Message}\n{ex.StackTrace}");
|
||
}
|
||
}
|
||
|
||
private static void TestLoadAtlasConfig()
|
||
{
|
||
const string path = "Assets/Art_SubModule/Art_Bytes/ArtAtlasConfig.bytes";
|
||
|
||
var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(path);
|
||
if (textAsset == null)
|
||
{
|
||
Debug.LogError($"❌ 文件不存在: {path}");
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
var config = new Byway.Thrift.Data.ArtAtlasConfig();
|
||
using (var transport = new Thrift.Transport.Client.TMemoryBufferTransport(textAsset.bytes, new Thrift.TConfiguration()))
|
||
{
|
||
using (var protocol = new Thrift.Protocol.TBinaryProtocol(transport))
|
||
{
|
||
config.ReadAsync(protocol, System.Threading.CancellationToken.None).GetAwaiter().GetResult();
|
||
}
|
||
}
|
||
|
||
Debug.Log($"✅ ArtAtlasConfig.bytes 加载成功!\n" +
|
||
$" 图集数量: {config.Atlases.Count}\n" +
|
||
$" 版本: {config.Version}\n" +
|
||
$" 生成时间: {config.GenerateTime}");
|
||
|
||
foreach (var atlas in config.Atlases)
|
||
{
|
||
Debug.Log($" 图集: {atlas.Name} (Sprites: {atlas.SpritePaths.Count})");
|
||
}
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
Debug.LogError($"❌ 加载失败: {ex.Message}\n{ex.StackTrace}");
|
||
}
|
||
}
|
||
}
|
||
}
|