Skip to content

Instantly share code, notes, and snippets.

@Talsidor
Last active August 29, 2015 14:18
Show Gist options
  • Save Talsidor/6be187e89a15796bb099 to your computer and use it in GitHub Desktop.
Save Talsidor/6be187e89a15796bb099 to your computer and use it in GitHub Desktop.
Unity CustomInspector Template. Three variants, one with tutorial comments and Rotorz's ReorderableList, one clean with Rotorz's ReorderableList, and one just clean.
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(ReplaceMe))]
public class CustomInspector_Template : Editor {
ReplaceMe myObject;
SerializedProperty boolProperty, floatProperty, listProperty;
void OnEnable () {
myObject = (ReplaceMe)target;
boolProperty = serializedObject.FindProperty("nameOfYourBool");
floatProperty = serializedObject.FindProperty("nameOfYourFloat");
listProperty = serializedObject.FindProperty("nameOfYourList");
}
public override void OnInspectorGUI () {
serializedObject.Update();
DrawDefaultInspector();
if (boolProperty.boolValue) {
myObject.exampleFloatVariable = EditorGUILayout.FloatField("Example Float", myObject.exampleFloatVariable);
}
else {
myObject.exampleFloatVariable = EditorGUILayout.FloatField("Example Float", myObject.exampleFloatVariable);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEditor;
using Rotorz.ReorderableList;
[CustomEditor(typeof(ReplaceMe))]
public class ReorderableList_CustomEditor_Template : Editor {
ReplaceMe myObject;
SerializedProperty boolProperty, floatProperty, listProperty;
void OnEnable () {
myObject = (ReplaceMe)target;
boolProperty = serializedObject.FindProperty("nameOfYourBool");
floatProperty = serializedObject.FindProperty("nameOfYourFloat");
listProperty = serializedObject.FindProperty("nameOfYourList");
}
public override void OnInspectorGUI () {
serializedObject.Update();
DrawDefaultInspector();
if (boolProperty.boolValue) {
myObject.exampleFloatVariable = EditorGUILayout.FloatField("Example Float", myObject.exampleFloatVariable);
ReorderableListGUI.Title("Reorderable List");
ReorderableListGUI.ListField(listProperty);
}
else {
myObject.exampleFloatVariable = EditorGUILayout.FloatField("Example Float", myObject.exampleFloatVariable);
}
}
}
using UnityEngine;
using System.Collections;
// We need to include these libraries (Get Rotorz's ReorderableList from here: https://bitbucket.org/rotorz)
using UnityEditor;
using Rotorz.ReorderableList;
// Replace ReplaceMe with your type, you'll have to do this a few times throughout the script
[CustomEditor(typeof(ReplaceMe))]
public class ReorderableList_CustomEditor_Template_WithComments : Editor {
// Replace ReplaceMe with your type
ReplaceMe myObject;
// Replace these properties with what you want to display from your class, plus any variables whose variables you want to manipulate the display of your inspector
SerializedProperty boolProperty, floatProperty, listProperty;
// Think of this as Start, here we have to set up how the Custom Editor interfaces with your class
void OnEnable () {
// Replace ReplaceMe with your type
myObject = (ReplaceMe)target;
// List each of your SerializedProperties here, and have them find your actual variables
boolProperty = serializedObject.FindProperty("nameOfYourBool");
floatProperty = serializedObject.FindProperty("nameOfYourFloat");
listProperty = serializedObject.FindProperty("nameOfYourList");
}
// Think of this as Update, we can do logic here to change what the inspector looks like in real-time! Amazing!
public override void OnInspectorGUI () {
serializedObject.Update();
// This will create all the default public/serialized values of your class in the inspector, it saves us from doing custom code for everything in the class. Feel free to remove it if you do want to do that.
DrawDefaultInspector();
// Alrighty, here we can use the value of a serializedObject (which is watching one of your class' variables) to change whether or not something is displayed in the inspector. This default example shows the list (as a ReorderableList) only if the bool value is true. You could also use .floatValue > 5, or .stringValue = "ham", anything!
if (boolProperty.boolValue)
{
// This is how to display a Float in your inspector, and have the entered value run back to the float in your actual class. Again just change Float to another type if need be.
myObject.exampleFloatVariable = EditorGUILayout.FloatField("Example Float", myObject.exampleFloatVariable);
// Optionally display a title on your Reorderable List
ReorderableListGUI.Title("If you want a title for your List to be displayed, put it here.");
// Here it is! This is where you display your list reorderably, just change exampleList to your List name.
ReorderableListGUI.ListField(listProperty);
}
// Optional: Want to display something different rather than nothing when the if condition above isn't met? Use an else and display the different info
else
{
myObject.exampleFloatVariable = EditorGUILayout.FloatField("Example Float", myObject.exampleFloatVariable);
}
}
}
// Once you've set up the rest of this script feel free to delete this class =)
class ReplaceMe : Object
{
[HideInInspector] // Need a public variable in your MonoBehaviour but don't want it showing in the inspector and ruining your Custom Inspector? Add this before it's declaration.
public float exampleFloatVariable;
[HideInInspector]
public List<int> exampleList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment