Files
A-Fairytale-Gone-Bad-Briar-…/Assets/Scripts/Editor/Drawers/BaseInteractionDrawer.cs

80 lines
2.6 KiB
C#

using System;
using System.Linq;
using BriarQueen.Framework.Managers.Interaction.Data;
using UnityEditor;
using UnityEngine;
namespace BriarQueen.Editor.Drawers
{
[CustomPropertyDrawer(typeof(BaseInteraction), true)]
public class BaseItemInteractionDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
if (property.managedReferenceValue == null)
{
if (GUI.Button(position, "Select Interaction Type"))
{
ShowTypeMenu(property);
}
EditorGUI.EndProperty();
return;
}
var type = property.managedReferenceValue.GetType();
var headerRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
if (GUI.Button(headerRect, type.Name, EditorStyles.popup))
{
ShowTypeMenu(property);
}
var bodyRect = new Rect(
position.x,
position.y + EditorGUIUtility.singleLineHeight + 2,
position.width,
position.height - EditorGUIUtility.singleLineHeight - 2);
EditorGUI.indentLevel++;
EditorGUI.PropertyField(bodyRect, property, GUIContent.none, true);
EditorGUI.indentLevel--;
EditorGUI.EndProperty();
}
private void ShowTypeMenu(SerializedProperty property)
{
var menu = new GenericMenu();
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t =>
!t.IsAbstract &&
typeof(BaseInteraction).IsAssignableFrom(t));
foreach (var type in types)
{
menu.AddItem(new GUIContent(type.Name), false, () =>
{
property.serializedObject.Update();
property.managedReferenceValue = Activator.CreateInstance(type);
property.serializedObject.ApplyModifiedProperties();
});
}
menu.ShowAsContext();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (property.managedReferenceValue == null)
return EditorGUIUtility.singleLineHeight;
return EditorGUI.GetPropertyHeight(property, true) + EditorGUIUtility.singleLineHeight + 4;
}
}
}