Awesome
EditorGenerator
A tool that generates custom editors in Unity for any script that is a subclass of MonoBehaviour
or ScriptableObject
.
This is my first foray in code generation, so my code (as well as the generated code) is pretty ugly right now and will probably break easily. This is more of an intitial proof of concept than a finished tool.
How to use
- Open the Editor Generator window from the "Tools/Editor Generator" menu item.
- Drag a script onto the "script" field.
- Click "Generate" and pick where you want the generated editor to be saved. Remember that custom editors have to be in an Editor folder.
Example
Here's an example MonoBehaviour
using UnityEngine;
public class ExampleScript : MonoBehaviour
{
[Tooltip ("A public, automatically serialized float")]
public float aPublicFloat;
[SerializeField, Tooltip ("A private, manually serialized float")]
private float privateSerializedVariable;
[Header ("A header")]
public float aFloatWithAHeader;
[Range (-1f, 1f)]
public float aSliderFloat;
[Space]
public float aFloatWithASpace;
// These will get ignored by the editor generator because it know's they aren't serialized
[System.NonSerialized]
public float publicNonSerializedVariable;
private float privateVariable;
}
Here's it's generated editor (I can't figure out how to prevent the xml comment at the top from being added 🙁)
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using UnityEditor;
[UnityEditor.CanEditMultipleObjects()]
[UnityEditor.CustomEditor(typeof(ExampleScript))]
public class ExampleScriptEditor : Editor
{
private UnityEngine.GUIContent aPublicFloatContent = new UnityEngine.GUIContent("A Public Float", "A public, automatically serialized float");
private UnityEngine.GUIContent privateSerializedVariableContent = new UnityEngine.GUIContent("Private Serialized Variable", "A private, manually serialized float");
private UnityEngine.GUIContent aFloatWithAHeaderContent = new UnityEngine.GUIContent("A Float With A Header");
private UnityEngine.GUIContent aSliderFloatContent = new UnityEngine.GUIContent("A Slider Float");
private UnityEngine.GUIContent aFloatWithASpaceContent = new UnityEngine.GUIContent("A Float With A Space");
public override void OnInspectorGUI()
{
serializedObject.UpdateIfRequiredOrScript();
UnityEditor.EditorGUILayout.PropertyField(serializedObject.FindProperty("aPublicFloat"), aPublicFloatContent);
UnityEditor.EditorGUILayout.PropertyField(serializedObject.FindProperty("privateSerializedVariable"), privateSerializedVariableContent);
UnityEditor.EditorGUILayout.LabelField("A header", EditorStyles.boldLabel);
UnityEditor.EditorGUILayout.PropertyField(serializedObject.FindProperty("aFloatWithAHeader"), aFloatWithAHeaderContent);
UnityEditor.EditorGUILayout.Slider(serializedObject.FindProperty("aSliderFloat"), -1F, 1F, aSliderFloatContent);
UnityEditor.EditorGUILayout.GetControlRect(false, 8F);
UnityEditor.EditorGUILayout.PropertyField(serializedObject.FindProperty("aFloatWithASpace"), aFloatWithASpaceContent);
serializedObject.ApplyModifiedProperties();
}
}
This is what the editor looks like in the inspector