BITFALL/Assets/BITKit/UnityEditor/DictionaryReferenceEditorWi...

111 lines
2.9 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
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 TextField _valueTextField;
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;
}
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
}