Writing Unity Custom Editors in C#

As your games become more complex you’re probably going to start to get frustrated with how complex the Unity Inspector can be. Editing 4 different arrays all containing linked information about an object and keeping it all in sync can start to add up, especially if you’re building these tools for game designers to come behind you and use or tweak. Writing custom editors for the inspector is an incredibly useful tool for keeping it clean and manageable on the surface while being complex under the covers.

Ok, enough trying to sell you on using customer editors. I recently decided to write a custom editor in c# and found that the documentation only gives you code for JavaScript. After grinding it out for a bit I finally figured out how to do it in C#:

First, here’s the C# snippet that’s going on my game object:

EnemyShip.cs

using UnityEngine;
using System.Collections;

public class EnemyShip : MonoBehavior {
public float speed;
}

What happens here is when I click on my enemy ship I get a little box I can type a float into that determines my enemy ship’s speed. I’d much rather have a slider, so I’m going to replace it with a custom editor. You have to create a folder called Editor in your project and put this script in that folder:

EnemyShipEditor.cs

using UnityEngine;
using UnityEditor;
using System.Collections;

[CustomEditor(typeof(EnemyShip))]
public class EnemyShipEditor : Editor {
private EnemyShip es;

void Awake()
{
es = (EnemyShip)target;
{

public override void OnInspectorGUI()
{
es.speed = EditorGUILayout.Slider(“Enemy Ship Speed:”, es.speed, 1, 100);
}
}

What we’ve done here is cast target as an EnemyShip instead of a game object. If you don’t do this part you’ll get all sorts of null reference exceptions and the inspector will fail to load properly. Since this is overriding the OnInspectorGUI class it happens even when the Unity Player isn’t running.

Ok, so hopefully that will help some of you out and cut down on time you spend researching it.

Advertisements

6 Responses to “Writing Unity Custom Editors in C#”


  1. 1 Ian May 1, 2012 at 9:47 pm

    Thank you, that was a BIG help!

  2. 3 vf October 30, 2012 at 2:30 pm

    You`ve just misspelled some parts of the code, it should be more like this:

    EnemyShipEditor.cs

    using UnityEngine;
    using UnityEditor;
    using System.Collections;

    [CustomEditor(typeof(EnemyShip))]
    public class EnemyShipEditor : Editor {
    private EnemyShip es;

    void Awake() {
    es = (EnemyShip)target;
    }

    public override void OnInspectorGUI() {
    es.speed = EditorGUILayout.Slider(“Enemy Ship Speed:”, es.speed, 1, 100);
    }

    }

  3. 5 arash February 18, 2013 at 4:19 pm

    Hi!
    Why do you use “EnemyShip” in the CustomEditor ?
    What is exactly EnemyShip?

    • 6 Timothy Johnson February 18, 2013 at 4:28 pm

      EnemyShip.cs is the class I wrote to handle my enemy ships in the game I was working on. In Unity you drag the EnemyShip.cs script onto your Game Object, and then you can edit values in the inspector. In this example I was showing how you can display a custom inspector when clicking on that game object.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: