Net.Like.Xue.Tokyo/Assets/BITKit/UnityEditor/DictionaryReferenceEditorWi...

164 lines
4.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
using Random = UnityEngine.Random;
namespace BITKit.GameEditor
{
public class DictionaryReferenceEditorWindow : EditorWindow
{
[MenuItem("Tools/DictionaryReference Editor")]
public static void Open()
{
var window = GetWindow<DictionaryReferenceEditorWindow>();
window.titleContent = new GUIContent("Dictionary Reference Editor");
window.Show();
}
private Button _addButton;
private Button _buildButton;
private TextField _valueTextField;
private Label reportLabel;
private void OnEnable()
{
var serializedObject = new SerializedObject(DictionaryReferenceScriptableObject.Singleton);
BITInspectorExtensions.FillDefaultInspector(rootVisualElement,serializedObject,true);
rootVisualElement.Bind(serializedObject);
_valueTextField = rootVisualElement.Create<TextField>();
_valueTextField.label = "Value,Key is random (int32)";
_addButton = rootVisualElement.Create<Button>();
_addButton.text = "Add";
_addButton.clicked += AddButtonOnClicked;
_buildButton = rootVisualElement.Create<Button>();
_buildButton.clicked += BuildAssembliesReference;
_buildButton.text = "构建引用";
_buildButton.tooltip = $"从代码中构建包括{nameof(DictionaryReferenceConfigAttribute)}的引用";
reportLabel = rootVisualElement.Create<Label>();
reportLabel.text = "等待操作中...";
}
private void BuildAssembliesReference()
{
var stringBuilder = new System.Text.StringBuilder();
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes());
var list = new List<(FieldInfo,DictionaryReferenceConfigAttribute)>();
foreach (var type in types)
{
try
{
foreach (var field in type.GetFields())
{
if (Attribute.IsDefined(field, typeof(DictionaryReferenceConfigAttribute)))
{
list.Add(new(field,field.GetCustomAttribute<DictionaryReferenceConfigAttribute>()));
}
}
}
catch (Exception)
{
continue;
}
}
foreach (var (fieldInfo,att) in list)
{
stringBuilder.AppendLine($"{att.index}:{fieldInfo.Name}");
//DictionaryReferenceScriptableObject.AddOrUpdate(fieldInfo.GetHashCode());
DictionaryReferenceScriptableObject.AddOrUpdate(att.index,fieldInfo.Name);
}
if (list.Count is 0)
{
reportLabel.text = "没有找到任何包含DictionaryReferenceConfigAttribute的类型";
}
else
{
reportLabel.text = stringBuilder.ToString();
}
}
private void AddButtonOnClicked()
{
var value = _valueTextField.value;
DictionaryReferenceScriptableObject.Add(value);
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(DictionaryReference))]
public sealed class DictionaryReferenceDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var index = property.FindPropertyRelative("index");
var value = DictionaryReferenceScriptableObject.Dictionary.TryGetValue(index.intValue, out var v)? v : "Not Found";
var keyword = property.FindPropertyRelative("keyword");
try
{
EditorGUILayout.LabelField("当前值", value);
}
catch (Exception e)
{
Debug.LogException(e);
EditorGUILayout.PrefixLabel(e.Message);
}
EditorGUILayout.BeginHorizontal();
//EditorGUILayout.DelayedTextField(keyword);
EditorGUILayout.PropertyField(keyword);
// var search = EditorGUILayout.TextField("搜索条件", keyword.stringValue);
EditorGUI.BeginChangeCheck();
var query = DictionaryReferenceScriptableObject.Dictionary.Values.Where(x =>
string.IsNullOrEmpty(keyword.stringValue) || x.ToLower().Contains(keyword.stringValue.ToLower())).ToList();
query.Insert(0, "<Click To Select>");
var newValue = EditorGUILayout.Popup(0, query.ToArray());
var nextString = query[newValue];
if (EditorGUI.EndChangeCheck() || nextString != value)
{
if (newValue > 0)
{
index.intValue = DictionaryReferenceScriptableObject.Dictionary.First(x => x.Value == nextString).Key;
}
}
EditorGUILayout.EndHorizontal();
if (EditorGUILayout.LinkButton("Open Editor"))
{
DictionaryReferenceEditorWindow.Open();
}
query.Clear();
}
}
#endif
}