188 lines
6.3 KiB
C#
188 lines
6.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Reflection;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
|
||
namespace MeowmentDebugTool
|
||
{
|
||
/// <summary>
|
||
/// 自定义复选框模块 - 通过反射加载标记为DebugCheckBox的方法
|
||
/// </summary>
|
||
public class CustomCheckBoxesModule : IDebugModule
|
||
{
|
||
#region 字段
|
||
private GameObject customCheckBoxesPage;
|
||
private RectTransform checkBoxContainer;
|
||
private GameObject checkBoxPrefab;
|
||
private ScrollRect checkBoxesScrollRect;
|
||
|
||
// 保存的SDF字体资源
|
||
private TMP_FontAsset savedFontAsset = null;
|
||
|
||
// 保存每个复选框的状态
|
||
private Dictionary<string, bool> checkBoxStates = new Dictionary<string, bool>();
|
||
#endregion
|
||
|
||
#region 构造函数
|
||
public CustomCheckBoxesModule(GameObject page, RectTransform container, GameObject prefab,
|
||
ScrollRect scrollRect)
|
||
{
|
||
customCheckBoxesPage = page;
|
||
checkBoxContainer = container;
|
||
checkBoxPrefab = prefab;
|
||
checkBoxesScrollRect = scrollRect;
|
||
}
|
||
#endregion
|
||
|
||
#region IDebugModule 实现
|
||
public void Initialize()
|
||
{
|
||
Debug.Log("[CustomCheckBoxesModule] 初始化自定义复选框模块...");
|
||
LoadCustomCheckBoxes();
|
||
}
|
||
|
||
public GameObject GetPage()
|
||
{
|
||
return customCheckBoxesPage;
|
||
}
|
||
|
||
public string GetModuleName()
|
||
{
|
||
return "开关";
|
||
}
|
||
#endregion
|
||
|
||
#region 公共方法
|
||
/// <summary>
|
||
/// 设置SDF字体
|
||
/// </summary>
|
||
public void SetSDFFont(TMP_FontAsset fontAsset)
|
||
{
|
||
savedFontAsset = fontAsset;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重新加载自定义复选框
|
||
/// </summary>
|
||
public void ReloadCustomCheckBoxes()
|
||
{
|
||
LoadCustomCheckBoxes();
|
||
}
|
||
#endregion
|
||
|
||
#region 私有方法
|
||
/// <summary>
|
||
/// 加载所有自定义复选框(使用反射)
|
||
/// </summary>
|
||
private void LoadCustomCheckBoxes()
|
||
{
|
||
if (checkBoxContainer == null || checkBoxPrefab == null)
|
||
{
|
||
Debug.LogWarning("[CustomCheckBoxesModule] 复选框容器或复选框预制件未设置");
|
||
return;
|
||
}
|
||
|
||
// 清空现有复选框
|
||
foreach (Transform child in checkBoxContainer)
|
||
{
|
||
UnityEngine.Object.Destroy(child.gameObject);
|
||
}
|
||
|
||
// 查找所有标记为DebugCheckBox的方法
|
||
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||
foreach (var assembly in assemblies)
|
||
{
|
||
try
|
||
{
|
||
var types = assembly.GetTypes();
|
||
foreach (var type in types)
|
||
{
|
||
var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
||
foreach (var method in methods)
|
||
{
|
||
var attribute = method.GetCustomAttribute<DebugCheckBoxAttribute>();
|
||
if (attribute != null)
|
||
{
|
||
CreateCustomCheckBox(method, attribute);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
// 某些程序集可能无法访问,跳过
|
||
Debug.LogWarning($"[CustomCheckBoxesModule] 无法访问程序集 {assembly.FullName}: {e.Message}");
|
||
}
|
||
}
|
||
}
|
||
|
||
private void CreateCustomCheckBox(MethodInfo method, DebugCheckBoxAttribute attribute)
|
||
{
|
||
// 验证方法签名:必须接受一个 bool 参数
|
||
var parameters = method.GetParameters();
|
||
if (parameters.Length != 1 || parameters[0].ParameterType != typeof(bool))
|
||
{
|
||
Debug.LogWarning($"[CustomCheckBoxesModule] 方法 {method.Name} 必须接受一个 bool 参数");
|
||
return;
|
||
}
|
||
|
||
GameObject checkBoxObj = UnityEngine.Object.Instantiate(checkBoxPrefab, checkBoxContainer);
|
||
Toggle toggle = checkBoxObj.GetComponent<Toggle>();
|
||
TMP_Text labelText = checkBoxObj.GetComponentInChildren<TMP_Text>();
|
||
Image backgroundImage = checkBoxObj.GetComponent<Image>();
|
||
|
||
// 设置复选框文本
|
||
if (labelText != null)
|
||
{
|
||
labelText.text = string.IsNullOrEmpty(attribute.DisplayName) ? method.Name : attribute.DisplayName;
|
||
|
||
// 应用保存的字体
|
||
if (savedFontAsset != null)
|
||
{
|
||
labelText.font = savedFontAsset;
|
||
}
|
||
}
|
||
|
||
// 设置背景颜色
|
||
if (backgroundImage != null && attribute.CheckBoxColor != default(Color))
|
||
{
|
||
backgroundImage.color = attribute.CheckBoxColor;
|
||
}
|
||
|
||
// 获取或初始化状态(默认为 false)
|
||
string methodKey = $"{method.DeclaringType?.FullName}.{method.Name}";
|
||
if (!checkBoxStates.ContainsKey(methodKey))
|
||
{
|
||
checkBoxStates[methodKey] = false;
|
||
}
|
||
|
||
// 设置初始状态
|
||
if (toggle != null)
|
||
{
|
||
toggle.isOn = checkBoxStates[methodKey];
|
||
}
|
||
|
||
// 设置复选框状态改变事件
|
||
toggle.onValueChanged.AddListener((bool isOn) =>
|
||
{
|
||
try
|
||
{
|
||
// 保存状态
|
||
checkBoxStates[methodKey] = isOn;
|
||
|
||
// 调用方法,传递状态
|
||
method.Invoke(null, new object[] { isOn });
|
||
Debug.Log($"[CustomCheckBoxesModule] 执行调试方法: {method.Name}, 状态: {isOn}");
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError($"[CustomCheckBoxesModule] 执行调试方法 {method.Name} 时出错: {e.Message}");
|
||
}
|
||
});
|
||
}
|
||
#endregion
|
||
}
|
||
}
|