Last active
October 2, 2017 07:14
-
-
Save IvanMurzak/084f553d6813277d486f8df9e9b21449 to your computer and use it in GitHub Desktop.
Files in "Editor" directory: - MonoBehaviourCustomEditor.cs
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
using System; | |
[AttributeUsage(AttributeTargets.Method)] | |
public class ExposeMethodInUnityEditorAttribute : Attribute | |
{ | |
} |
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
using System; | |
[AttributeUsage(AttributeTargets.Method)] | |
public class ExposeMethodInUnityEditorPlayingOnlyAttribute : Attribute | |
{ | |
} |
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
using UnityEngine; | |
using UnityEditor; | |
using System.Reflection; | |
using System; | |
namespace Project.Template.Editor | |
{ | |
[CanEditMultipleObjects] | |
[CustomEditor(typeof(MonoBehaviour), true)] | |
public class MonoBehaviourCustomEditor : UnityEditor.Editor | |
{ | |
public override void OnInspectorGUI() | |
{ | |
DrawDefaultInspector(); | |
var type = target.GetType(); | |
foreach (var method in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) | |
{ | |
var editorAttributes = method.GetCustomAttributes(typeof(ExposeMethodInUnityEditorAttribute), true); | |
if (editorAttributes.Length > 0) | |
{ | |
DrawButton(method.Name, type, target); | |
} | |
else if (Application.isPlaying) | |
{ | |
var editorPlayingOnlyAttributes = method.GetCustomAttributes(typeof(ExposeMethodInUnityEditorPlayingOnlyAttribute), true); | |
if (editorPlayingOnlyAttributes.Length > 0) | |
{ | |
DrawButton(method.Name, type, target); | |
} | |
} | |
} | |
} | |
private static void DrawButton(string methodName, Type type, object target) | |
{ | |
if (GUILayout.Button(methodName)) | |
{ | |
type.GetMethod(methodName).Invoke(target, null); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment