优化动态反射界面类型

This commit is contained in:
2026-06-21 23:23:44 +08:00
parent 7c2e43efbb
commit 6783ab1657
14 changed files with 333 additions and 23 deletions
@@ -43,7 +43,10 @@ namespace XericUIEditor.ReflectionUIGenerator
// 基本属性
EditorGUILayout.PropertyField(_themeNameProp, new GUIContent("主题名称 (Theme)"));
EditorGUILayout.PropertyField(_uiNameProp, new GUIContent("UI 名称 (uiName)"));
EditorGUILayout.PropertyField(_valueTypeNameProp, new GUIContent("绑定类型 (valueType)"));
// 绑定类型 — 带快捷选择 + 自动纠错
DrawValueTypeField();
EditorGUILayout.Space();
// 预制体 + 自动扫描
@@ -136,5 +139,119 @@ namespace XericUIEditor.ReflectionUIGenerator
serializedObject.ApplyModifiedProperties();
}
private static List<string> _cachedTypeNames;
private static List<string> _cachedFullNames;
/// <summary>
/// 从 AppDomain 动态收集所有可用类型名(Editor 侧缓存)
/// </summary>
private static void EnsureEditorTypeCache()
{
if (_cachedTypeNames != null) return;
_cachedTypeNames = new List<string>();
_cachedFullNames = new List<string>();
var seen = new HashSet<string>();
foreach (var asm in System.AppDomain.CurrentDomain.GetAssemblies())
{
try
{
foreach (var type in asm.GetTypes())
{
var full = type.FullName;
if (string.IsNullOrEmpty(full) || full.Contains("`") || full.Contains("<")) continue;
if (!seen.Add(full)) continue;
_cachedTypeNames.Add(type.Name);
_cachedFullNames.Add(full);
}
}
catch { }
}
}
private void DrawValueTypeField()
{
EditorGUILayout.LabelField("绑定类型 (valueType)", EditorStyles.label);
// 输入行
EditorGUILayout.BeginHorizontal();
var currentValue = _valueTypeNameProp.stringValue;
var newValue = EditorGUILayout.TextField(currentValue);
if (newValue != currentValue)
{
var corrected = XuiavrgPropertyGroupScriptableObject.NormalizeTypeName(newValue);
_valueTypeNameProp.stringValue = corrected;
currentValue = corrected;
}
// "搜索类型" 按钮
if (GUILayout.Button("...", GUILayout.Width(30)))
{
var menu = new GenericMenu();
EnsureEditorTypeCache();
// 如果有输入,用 LevenshteinDistance 排序,最近的排前面
var ranked = new List<(int dist, string name, string full)>();
for (int i = 0; i < _cachedTypeNames.Count; i++)
{
XericLibrary.Runtime.MacroLibrary.MacroMath.LevenshteinDistance(
new[] { _cachedTypeNames[i] }, currentValue, out int dist, out _);
ranked.Add((dist, _cachedTypeNames[i], _cachedFullNames[i]));
}
ranked.Sort((a, b) => a.dist.CompareTo(b.dist));
int shown = 0;
// 完全匹配的排最前面
foreach (var (dist, name, full) in ranked)
{
if (dist != 0 || shown >= 30) break;
menu.AddItem(new GUIContent($"{full} ({name})"), false, () =>
{
_valueTypeNameProp.stringValue = full;
serializedObject.ApplyModifiedProperties();
});
shown++;
}
// 用分隔线隔开,然后显示近匹配
if (shown > 0 && ranked.Count > shown)
menu.AddSeparator("");
int nearTotal = 0;
foreach (var (dist, name, full) in ranked)
{
if (dist == 0) continue;
if (nearTotal >= 20) break;
menu.AddItem(new GUIContent($"{full} ({name})"), false, () =>
{
_valueTypeNameProp.stringValue = full;
serializedObject.ApplyModifiedProperties();
});
nearTotal++;
}
menu.ShowAsContext();
}
EditorGUILayout.EndHorizontal();
// 纠错提示
var normalized = XuiavrgPropertyGroupScriptableObject.NormalizeTypeName(currentValue);
if (normalized != currentValue && !string.IsNullOrEmpty(currentValue))
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.HelpBox($"识别为: {normalized}", MessageType.Info);
if (GUILayout.Button("纠正", GUILayout.Width(60), GUILayout.Height(38)))
{
_valueTypeNameProp.stringValue = normalized;
}
EditorGUILayout.EndHorizontal();
}
}
}
}