TH1/Unity/Assets/Scripts/TH1_Logic/Editor/AITrainEditorWindow.cs
2026-06-10 11:58:18 +08:00

99 lines
3.1 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.

/*
* @Author: 白哉
* @Description:
* @Date: 2025年07月25日 星期五 14:07:55
* @Modify:
*/
using System;
using System.Linq;
using Logic.Action;
using Logic.Config;
using TH1_Logic.AITrain;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
namespace Logic.Editor
{
public class AITrainEditorWindow : EditorWindow
{
// 背景
private GUIStyle _redBoxStyle;
private GUIStyle _whiteBoxStyle;
[MenuItem("Tools/AI训练窗口")]
private static void ShowWindow()
{
var window = GetWindow<AITrainEditorWindow>();
window.titleContent = new GUIContent("AI训练窗口");
window.Show();
}
private void OnEnable()
{
}
private void OnGUI()
{
if (_redBoxStyle == null)
{
_redBoxStyle = InspectorUtils.GetHelpBoxStyle();
InspectorUtils.AddBorder(_redBoxStyle, new Color(0.5f, 0.4f, 0.4f, 0.6f));
}
if (_whiteBoxStyle == null)
{
_whiteBoxStyle = InspectorUtils.GetHelpBoxStyle();
InspectorUtils.AddBorder(_whiteBoxStyle, new Color(1f, 1f, 1f, 0.2f));
}
if (InspectorUtils.InspectorButtonWithTextWidth($"构建 AI 行为集"))
{
ActionLogicIdData data = null;
var bytes = LoadActionLogicIdData();
if (bytes.Length > 0) data = MemoryPack.MemoryPackSerializer.Deserialize<ActionLogicIdData>(bytes);
if (data == null) data = new ActionLogicIdData();
var dict = ActionLogicFactory.GetActionLogicDict();
foreach (var actionId in dict.Keys) data.AddActionId(actionId);
SaveActionLogicIdData(MemoryPack.MemoryPackSerializer.Serialize(data));
Debug.LogError($"Action数量 {data.ActionIdList.Count}");
}
}
private byte[] LoadActionLogicIdData()
{
TextAsset asset = TH1Resource.ResourceLoader.Load<TextAsset>($"CommonIdData/CommonIdData");
return asset?.bytes ?? Array.Empty<byte>();
}
private void SaveActionLogicIdData(byte[] data)
{
// 构建完整路径Resources 子目录)
string directory = "Assets/BundleResources/CommonIdData";
string filePath = $"{directory}/CommonIdData.bytes";
// 检查目录,不存在则创建
if (!System.IO.Directory.Exists(directory))
{
System.IO.Directory.CreateDirectory(directory);
}
// 写入文件(存在则覆盖,不存在则创建)
try
{
System.IO.File.WriteAllBytes(filePath, data);
// 刷新 Unity 资源数据库
AssetDatabase.Refresh();
}
catch (Exception e)
{
Debug.LogError($"写入文件失败: {e.Message}");
}
}
}
}