forked from Shardstone/trail-into-darkness
38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
using System.Reflection;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace Jovian.InspectorTools {
|
|
public class OnValueChangedAttribute : PropertyAttribute {
|
|
public readonly string methodName;
|
|
public OnValueChangedAttribute(string methodName) {
|
|
this.methodName = methodName;
|
|
}
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
namespace Jovian.InspectorTools.Internal {
|
|
|
|
[CustomPropertyDrawer(typeof(OnValueChangedAttribute))]
|
|
public class OnValueChangedAttributeDrawer : PropertyDrawer {
|
|
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
|
|
EditorGUI.BeginChangeCheck();
|
|
EditorGUI.PropertyField(position, property, label);
|
|
if(EditorGUI.EndChangeCheck()) {
|
|
property.serializedObject.ApplyModifiedProperties();
|
|
|
|
Object target = property.serializedObject.targetObject;
|
|
string methodName = ((OnValueChangedAttribute)attribute).methodName;
|
|
MethodInfo methodInfo = target.GetType().GetMethod(methodName);
|
|
if (methodInfo != null) {
|
|
methodInfo.Invoke(target, null);
|
|
}
|
|
else {
|
|
Debug.LogError($"No method named '{methodName}' found on {target}", target);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endif |