Skip to content

Commit

Permalink
Call methods on components without a CustomEditor #80
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinkievicz committed Jan 18, 2022
1 parent 17da06d commit 8108b04
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Runtime/UIToolkit/InspectorButtonAttribute.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/UIToolkit/InspectorButtonAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions Runtime/UIToolkit/InspectorButtonDrawer.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/UIToolkit/InspectorButtonDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down

0 comments on commit 8108b04

Please sign in to comment.