[TASK] Content fixes

This commit is contained in:
2019-08-26 00:51:03 +02:00
parent b4543142de
commit dc140068d6
24 changed files with 433 additions and 15 deletions

View File

@@ -0,0 +1,81 @@
using System;
namespace EasyButtons
{
public enum ButtonMode
{
AlwaysEnabled,
EnabledInPlayMode,
DisabledInPlayMode
}
[Flags]
public enum ButtonSpacing
{
None = 0,
Before = 1,
After = 2
}
/// <summary>
/// Attribute to create a button in the inspector for calling the method it is attached to.
/// The method must have no arguments.
/// </summary>
/// <example>
/// [Button]
/// public void MyMethod()
/// {
/// Debug.Log("Clicked!");
/// }
/// </example>
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class ButtonAttribute : Attribute
{
private string name = null;
private ButtonMode mode = ButtonMode.AlwaysEnabled;
private ButtonSpacing spacing = ButtonSpacing.None;
public string Name { get { return name; } }
public ButtonMode Mode { get { return mode; } }
public ButtonSpacing Spacing { get { return spacing; } }
public ButtonAttribute()
{
}
public ButtonAttribute(string name)
{
this.name = name;
}
public ButtonAttribute(ButtonMode mode)
{
this.mode = mode;
}
public ButtonAttribute(ButtonSpacing spacing)
{
this.spacing = spacing;
}
public ButtonAttribute(string name, ButtonMode mode)
{
this.name = name;
this.mode = mode;
}
public ButtonAttribute(string name, ButtonSpacing spacing)
{
this.name = name;
this.spacing = spacing;
}
public ButtonAttribute(string name, ButtonMode mode, ButtonSpacing spacing)
{
this.name = name;
this.mode = mode;
this.spacing = spacing;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 36f9f413a12a9074ead2027c1aa7b6ab
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,61 @@
using UnityEngine;
namespace EasyButtons
{
public class ButtonsExample : MonoBehaviour
{
// Example use of the ButtonAttribute
[Button]
public void SayMyName()
{
Debug.Log(name);
}
// Example use of the ButtonAttribute that is not shown in play mode
[Button(ButtonMode.DisabledInPlayMode)]
protected void SayHelloEditor()
{
Debug.Log("Hello from edit mode");
}
// Example use of the ButtonAttribute that is only shown in play mode
[Button(ButtonMode.EnabledInPlayMode)]
private void SayHelloInRuntime()
{
Debug.Log("Hello from play mode");
}
// Example use of the ButtonAttribute with custom name
[Button("Special Name", ButtonSpacing.Before)]
private void TestButtonName()
{
Debug.Log("Hello from special name button");
}
// Example use of the ButtonAttribute with custom name and button mode
[Button("Special Name Editor Only", ButtonMode.DisabledInPlayMode)]
private void TestButtonNameEditorOnly()
{
Debug.Log("Hello from special name button for editor only");
}
// Example use of the ButtonAttribute with static method
[Button]
private static void TestStaticMethod()
{
Debug.Log("Hello from static method");
}
// Example use of the ButtonAttribute with ButtonSpacing, and mix two spacing together.
[Button("Space Before and After", ButtonSpacing.Before | ButtonSpacing.After)]
private void TestButtonSpaceBoth() {
Debug.Log("Hello from a button surround by spaces");
}
// Placeholder to show the last button have space after it.
[Button("Another Button")]
private void TestButtonEndSpace() {
Debug.Log("Hello I am here to show some spacing.");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 803cb9aa6c767b14ab0c93f41cd7aeef
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,13 @@
using UnityEngine;
namespace EasyButtons
{
public class CustomEditorButtonsExample : MonoBehaviour
{
[Button("Custom Editor Example")]
private void SayHello()
{
Debug.Log("Hello from custom editor");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5d318038542ab134cb5b81aa46a97e6b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 61aca73fced8c3d4eb17aa81e23af020
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
using UnityEditor;
namespace EasyButtons
{
/// <summary>
/// Custom inspector for Object including derived classes.
/// </summary>
[CanEditMultipleObjects]
[CustomEditor(typeof(UnityEngine.Object), true)]
public class ObjectEditor : Editor
{
public override void OnInspectorGUI()
{
this.DrawEasyButtons();
// Draw the rest of the inspector as usual
DrawDefaultInspector();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a7a3384be8a368e4c8e870e5feedb31d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
using UnityEditor;
namespace EasyButtons
{
[CustomEditor(typeof(CustomEditorButtonsExample))]
public class CustomEditorButtonsExampleEditor : Editor
{
public override void OnInspectorGUI()
{
this.DrawEasyButtons();
base.OnInspectorGUI();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f42477c48fa930844bbbf0ba8cf55828
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,49 @@
using System;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace EasyButtons
{
public static class EasyButtonsEditorExtensions
{
public static void DrawEasyButtons(this Editor editor)
{
// Loop through all methods with no parameters
var methods = editor.target.GetType()
.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
.Where(m => m.GetParameters().Length == 0);
foreach (var method in methods)
{
// Get the ButtonAttribute on the method (if any)
var ba = (ButtonAttribute)Attribute.GetCustomAttribute(method, typeof(ButtonAttribute));
if (ba != null)
{
// Determine whether the button should be enabled based on its mode
var wasEnabled = GUI.enabled;
GUI.enabled = ba.Mode == ButtonMode.AlwaysEnabled
|| (EditorApplication.isPlaying ? ba.Mode == ButtonMode.EnabledInPlayMode : ba.Mode == ButtonMode.DisabledInPlayMode);
if (((int)ba.Spacing & (int)ButtonSpacing.Before) != 0) GUILayout.Space(10);
// Draw a button which invokes the method
var buttonName = String.IsNullOrEmpty(ba.Name) ? ObjectNames.NicifyVariableName(method.Name) : ba.Name;
if (GUILayout.Button(buttonName))
{
foreach (var t in editor.targets)
{
method.Invoke(t, null);
}
}
if (((int)ba.Spacing & (int)ButtonSpacing.After) != 0) GUILayout.Space(10);
GUI.enabled = wasEnabled;
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4f80fb24d5fa6b441bd6c9d7b25f04cb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
using UnityEngine;
namespace EasyButtons
{
[CreateAssetMenu(fileName = "ScriptableObjectExample.asset", menuName = "EasyButtons/ScriptableObjectExample")]
public class ScriptableObjectExample : ScriptableObject
{
[Button]
public void SayHello()
{
Debug.Log("Hello");
}
[Button(ButtonMode.DisabledInPlayMode)]
public void SayHelloEditor()
{
Debug.Log("Hello from edit mode");
}
[Button(ButtonMode.EnabledInPlayMode)]
public void SayHelloPlayMode()
{
Debug.Log("Hello from play mode");
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ac23267699d149848936a2a4e614be65
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: