Created
September 13, 2019 19:07
-
-
Save Ranner198/551addbc17b543483517830955d207fd to your computer and use it in GitHub Desktop.
Example code snippet of how to extend the unity editor
This file contains hidden or 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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
public class SampleClass : MonoBehaviour | |
{ | |
[HideInInspector] // <---- This will hide the public variable in the insepector | |
public GameObject goVariable; | |
[HideInInspector] | |
public Object objectVariable; | |
// Numbers | |
[SerializeField] // <----- This will make a private object show up in the inspector, Cannot get a refernce of a private variable without a accessor in the editor script | |
private int intergerVariable; | |
[HideInInspector] | |
public float floatVariable; | |
[HideInInspector] | |
public double doubleVariable; | |
// String | |
public string stringVariable; | |
//Readonly variable in inspector | |
[HideInInspector] | |
public int readOnlyVariable; | |
// Public method we can access from the editor script | |
public void SomeMethod() | |
{ | |
Debug.Log("I was called from the Editor Script!"); | |
} | |
} | |
// This is need for the build of the application to work, We don't want the editor to run during build because it will remove the Editor functionality from the build and crash here | |
#if UNITY_EDITOR | |
// Defined the editor script as a custom editor | |
[CustomEditor( typeof( SampleClass ) )] | |
public class CustomEditorPanel : Editor // <---- We must derive from the Editor Class for the script to work correctly | |
{ | |
public override void OnInspectorGUI() // <---- basically the editors version of Update() that will be called every frame while in editor | |
{ | |
// Draws the default inspector to the editor window | |
DrawDefaultInspector(); | |
// This is how we get a reference to the script we are extending | |
SampleClass script = target as SampleClass; | |
EditorGUILayout.LabelField("I am just a label, look at me!"); | |
// Game Object | |
script.goVariable = EditorGUILayout.ObjectField("An Object Reference", script.goVariable, typeof(GameObject), true /*<--- This allows for scene obejcts to be used as references*/) as GameObject; | |
script.objectVariable = EditorGUILayout.ObjectField("An Object Reference", script.objectVariable, typeof(Object), true) as Object; // <---- How you would make like a script refernce or something besides a Game Object (This also includes Game Objects since GO derives from the Object class) | |
// Float | |
script.floatVariable = EditorGUILayout.FloatField("I am a float input", script.floatVariable); | |
// Double | |
script.doubleVariable = EditorGUILayout.DoubleField("Double tap B)", script.doubleVariable); | |
// String | |
script.stringVariable = EditorGUILayout.TextField("String Cheese?", script.stringVariable); | |
// Readonly Variable | |
EditorGUILayout.IntField("I am uneditable :)", script.readOnlyVariable); | |
script.SomeMethod(); // This we call the Debug.Log method to print that it was called using the editor | |
// For a GUI button on the inspector you can make Button in a if statement, upon clicking on it, it will execute the inside of the if statment | |
if (GUILayout.Button("Button Name")) | |
{ | |
// Call Something Here | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment