From 8108b0411d69d8adf6a7a856761da8fa0d4f58d5 Mon Sep 17 00:00:00 2001 From: Shynkiewicz Date: Tue, 18 Jan 2022 09:14:10 +0100 Subject: [PATCH] Call methods on components without a CustomEditor #80 --- Runtime/UIToolkit/InspectorButtonAttribute.cs | 26 +++++++++ .../InspectorButtonAttribute.cs.meta | 11 ++++ Runtime/UIToolkit/InspectorButtonDrawer.cs | 57 +++++++++++++++++++ .../UIToolkit/InspectorButtonDrawer.cs.meta | 11 ++++ package.json | 2 +- 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 Runtime/UIToolkit/InspectorButtonAttribute.cs create mode 100644 Runtime/UIToolkit/InspectorButtonAttribute.cs.meta create mode 100644 Runtime/UIToolkit/InspectorButtonDrawer.cs create mode 100644 Runtime/UIToolkit/InspectorButtonDrawer.cs.meta diff --git a/Runtime/UIToolkit/InspectorButtonAttribute.cs b/Runtime/UIToolkit/InspectorButtonAttribute.cs new file mode 100644 index 0000000..d468a6c --- /dev/null +++ b/Runtime/UIToolkit/InspectorButtonAttribute.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace StansAssets.Foundation +{ + public enum InspectorButtonMode + { + AllModes, + OnlyPlayMode, + OnlyEditorMode, + } + + public class InspectorButtonAttribute : PropertyAttribute + { + public string MethodName { get; } + public InspectorButtonMode Mode { get; } + + public InspectorButtonAttribute(string methodName, InspectorButtonMode mode = InspectorButtonMode.AllModes) + { + Mode = mode; + MethodName = methodName; + } + } +} \ No newline at end of file diff --git a/Runtime/UIToolkit/InspectorButtonAttribute.cs.meta b/Runtime/UIToolkit/InspectorButtonAttribute.cs.meta new file mode 100644 index 0000000..eefbc72 --- /dev/null +++ b/Runtime/UIToolkit/InspectorButtonAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 84b21cf42924782488f93b2119613f57 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/UIToolkit/InspectorButtonDrawer.cs b/Runtime/UIToolkit/InspectorButtonDrawer.cs new file mode 100644 index 0000000..7cc36e0 --- /dev/null +++ b/Runtime/UIToolkit/InspectorButtonDrawer.cs @@ -0,0 +1,57 @@ +using System.Collections; +using System.Collections.Generic; +using System.Reflection; +using UnityEditor; +using UnityEngine; + +namespace StansAssets.Foundation +{ + [CustomPropertyDrawer(typeof(InspectorButtonAttribute))] + public class InspectorButtonDrawer : PropertyDrawer + { + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + var buttonAttribute = attribute as InspectorButtonAttribute; + if (buttonAttribute == null) + { + return; + } + + string methodName = buttonAttribute.MethodName; + UnityEngine.Object target = property.serializedObject.targetObject; + System.Type type = target.GetType(); + System.Reflection.MethodInfo method = type.GetMethod(methodName, + BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); + + if (method == null) + { + GUI.Label(position, $"Method {methodName} could not be found"); + return; + } + + if (method.GetParameters().Length > 0) + { + GUI.Label(position, "Method cannot have parameters."); + return; + } + + switch (buttonAttribute.Mode) + { + case InspectorButtonMode.OnlyEditorMode: + GUI.enabled = Application.isPlaying == false; + break; + + case InspectorButtonMode.OnlyPlayMode: + GUI.enabled = Application.isPlaying; + break; + } + + if (GUI.Button(position, method.Name)) + { + method.Invoke(target, null); + } + + GUI.enabled = true; + } + } +} \ No newline at end of file diff --git a/Runtime/UIToolkit/InspectorButtonDrawer.cs.meta b/Runtime/UIToolkit/InspectorButtonDrawer.cs.meta new file mode 100644 index 0000000..4e030ea --- /dev/null +++ b/Runtime/UIToolkit/InspectorButtonDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e81b0cb79fee8154fbbdd98bdb46de90 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json index 3e136fc..37adb2d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.stansassets.foundation", "displayName": "Stans Assets - Foundation", - "version": "1.0.25", + "version": "1.0.26", "unity": "2018.4", "description": "Foundation package is a collection of utility methods, design patterns, and extensions for Unity.", "keywords": [