-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call methods on components without a CustomEditor #80
- Loading branch information
1 parent
17da06d
commit 8108b04
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters