Extending the Unity Editor

Its been a while since my last post, as I have been pretty busy with a new job and learning new tools. After going through a bunch of tutorials I have familiarized myself with the Unity3d workflow and C#, and busied myself writing art tools for my new workplace.

The Unity Editor is pretty fun to work with, and has a very extendable interface. With just a few lines of code (C# by preference) it's possible to create a customized interface window.

Here is a bare bones example of how to make a custom window in Unity 4.2, and make it do stuff. The example is written in CSharp and does a very simple task, makes a window and tells you the name of whatever you have selected.

// Example Window- Get the name of whatever game object 
// that is currently selected

using UnityEngine;
using UnityEditor;

public class ExampleWindow : EditorWindow

    // Strings
    string objectName = "";
    // Update specific variables
    // Using the repaint flag, GUI updates are done once, rather than spammed
    // every frame. 
    private string lastSelected = "";
    private bool repaint = true;

    // This creates a new Menu item where we can access our tool from.
    static void Init()
        ExampleWindow window = (ExampleWindow)EditorWindow.GetWindow(typeof(ExampleWindow));

    // A Button Function
    void myFancyFunction()
        // Log something to the comments console. 
        Debug.Log("You pressed a fancy button!");

    // This is my actual window
    private void OnGUI()
        // Create a label
        GUILayout.Label("Select an object in the hierarchy view");

        // Create a text field that will update whenever we select a different object
        EditorGUILayout.TextField("Object Name: ", objectName);

        // Create a button that calls our fancy function. 
        if (GUI.Button(new Rect(32, 42, 256, 16), "Fancy function!"))


    private void Update()

        if (Selection.activeGameObject && Selection.activeGameObject.name != lastSelected)
            objectName = Selection.activeGameObject.name;

            // This is to prevent spamming GUI updates
            lastSelected = Selection.activeGameObject.name;
            repaint = true;

        else if (Selection.activeGameObject == null && repaint == true)
            objectName = "Please Select an Object";

            // This is to prevent spamming GUI updates
            repaint = false;
            lastSelected = "";

All of this results in a window you can then access from Window > ExampleWindow.
Probably the most useful tool ever made. 

No comments:

Post a Comment