Giter Club home page Giter Club logo

editorgenerator's Introduction

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

  1. Open the Editor Generator window from the "Tools/Editor Generator" menu item.
  2. Drag a script onto the "script" field.
  3. 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

1

editorgenerator's People

Contributors

keenanwoodall avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.