Tri-Inspector/Editor.Extras/Drawers/ButtonDrawer.cs
2022-01-18 20:52:33 +03:00

34 lines
1.0 KiB
C#

using System.Reflection;
using TriInspector;
using TriInspector.Drawers;
using UnityEditor;
using UnityEngine;
[assembly: RegisterTriAttributeDrawer(typeof(ButtonDrawer), TriDrawerOrder.Drawer)]
namespace TriInspector.Drawers
{
public class ButtonDrawer : TriAttributeDrawer<ButtonAttribute>
{
public override float GetHeight(float width, TriProperty property, TriElement next)
{
return EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position, TriProperty property, TriElement next)
{
var name = Attribute.Name ?? property.DisplayName;
if (GUI.Button(position, name))
{
var methodInfo = (MethodInfo) property.MemberInfo;
for (var i = 0; i < property.PropertyTree.TargetObjects.Length; i++)
{
var parentValue = property.Parent.GetValue(i);
methodInfo.Invoke(parentValue, new object[0]);
}
}
}
}
}