using MyGame.Battle;
using Sirenix.Utilities.Editor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace MyGame.Editor
{
public class CardEffectEditor : EditorWindow
{
private static CardEffectEditor window;
private readonly Type baseType = typeof(CardEffect);
private string[] baseTypeFields;
private string[] tableKeys;
private Type[] tableValues;
private int selectIndex;
private int currentIndex;
private SerializedObject serializedObject;
private SerializedProperty[] properties;
[MenuItem("MyMenu/CardEffect编辑器")]
private static void OpenWindow()
{
window = GetWindow<CardEffectEditor>("CardEffect编辑器");
}
private void OnEnable()
{
baseTypeFields = GetFields(baseType).Select(x => x.Name).ToArray();
tableKeys = CardEffectTable.Table.Keys.Select(x => x.ToString()).ToArray();
tableValues = CardEffectTable.Table.Values.ToArray();
selectIndex = 0;
currentIndex = 0;
SelectIndex(0);
}
private void OnGUI()
{
// effect type dropdown
selectIndex = EditorGUILayout.Popup("EffectType", selectIndex, tableKeys);
// copy button
if (GUILayout.Button("Copy json data to clipboard"))
{
string json = JsonUtility.ToJson(serializedObject.targetObject);
Clipboard.Copy(json);
}
EditorGUILayout.Space(10);
// object properties
if (serializedObject == null) return;
foreach (var property in properties)
{
EditorGUILayout.PropertyField(property);
}
serializedObject.ApplyModifiedProperties();
}
private void OnInspectorUpdate()
{
if (currentIndex == selectIndex) return;
currentIndex = selectIndex;
SelectIndex(currentIndex);
}
private IEnumerable<FieldInfo> GetFields(Type type)
{
return type.GetFields()
.Where(x => x.IsDefined(typeof(HideInInspector), true) == false);
}
private void SelectIndex(int index)
{
Type type = tableValues[index];
ScriptableObject obj = CreateInstance(type);
serializedObject = new SerializedObject(obj);
properties = GetFields(type)
.OrderBy(x => baseTypeFields.Contains(x.Name) == false) // baseType 的
fields 会排在前面
.Select(x => serializedObject.FindProperty(x.Name))
.ToArray();
}
}
}
大概4这样
窝要去吃炸鸡了