Files
bansonic_beta_main/Assets/Destro2DMain/Editor/Destro2DMenu.cs
T
2026-06-30 21:21:30 +08:00

81 lines
2.3 KiB
C#

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using System;
using System.Linq;
namespace KD.Destro2D.Editor {
[CustomPropertyDrawer(typeof(Destruction), true)]
public class Destro2DMenu : PropertyDrawer
{
const float PAD = 2f;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
Rect header = new Rect(
position.x,
position.y,
position.width,
EditorGUIUtility.singleLineHeight
);
string title =
property.managedReferenceValue == null
? "Select Destruction Type"
: property.managedReferenceValue.GetType().Name;
if (GUI.Button(header, title, EditorStyles.popup))
{
var menu = new GenericMenu();
var types = TypeCache.GetTypesDerivedFrom<Destruction>()
.Where(t => !t.IsAbstract && !t.IsGenericType);
foreach (var type in types)
{
menu.AddItem(
new GUIContent(type.Name),
false,
() =>
{
property.managedReferenceValue = Activator.CreateInstance(type);
property.serializedObject.ApplyModifiedProperties();
}
);
}
menu.ShowAsContext();
}
if (property.managedReferenceValue != null)
{
Rect body = new Rect(
position.x,
position.y + EditorGUIUtility.singleLineHeight + PAD,
position.width,
EditorGUI.GetPropertyHeight(property, true)
);
EditorGUI.indentLevel++;
EditorGUI.PropertyField(body, property, GUIContent.none, true);
EditorGUI.indentLevel--;
}
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (property.managedReferenceValue == null)
return EditorGUIUtility.singleLineHeight;
return EditorGUIUtility.singleLineHeight +
PAD +
EditorGUI.GetPropertyHeight(property, true);
}
}
}
#endif