Unity Editor Scripting-Quick SetActive

In this tutorial we are going to play with our hierarchy window, and add a toggle button into the hierarchywindow to quickly activate and deavtivate our gameobjects.Before we begin, you can download the source code from github.

And here how our hierarchy window will look like.(Light bulb icon next to toggle button is subject for another tutorial,it’s just a sneak peak for what you can do with your hierarchy)

Screen Shot 2014-12-19 at 11.41.50

 

TODO list for this tutorial:

  • First we need an editor script that loads on every time Unity opens.
  • Then we need a callback for every visible items OnGUI inside HierarchyWindow.
  • After handling callback we need to get the gameObject of items.
  • Then we need to draw a toggle button to fill setActive() methods parameter for every hierarchy item.

So lets begin. First lets create our script called “HierarchQuickSetActive”. You can find it inside /Assets/Tut-4-PlayingWithHierarchy/Editor folder.

  • Loading editor script when Unity opens

It’s so simple. Just place InitializeOnLoadAttribute for your class.

using UnityEngine;
using System.Collections;
using UnityEditor;
[InitializeOnLoad]
public class HierarchQuickSetActive {

}
  • Getting hierarchy callback

For this we are going to use EditorApplication.hierarchyWindowItemOnGUI event.This is a delegate for OnGUI events for every visible list item in the HierarchyWindow. We are going to register to this event on our initializer method and add our hierarchWindowOnGUI(method parameters comes from delegate something you cannot choose) method like this:

static HierarchQuickSetActive ()
{
    EditorApplication.hierarchyWindowItemOnGUI += hierarchWindowOnGUI;
}
static void hierarchWindowOnGUI (int instanceID, Rect selectionRect)
{

}
  • Getting gameobjects

One of our parameter for hierarchWindowOnGUI method is instanceID, you can see instance ids from your inspector tab selecting debug mode on the right corner.Those instance id’s are unique for every scene for every object and changes every time unity opens.To find the gameobjects from them EditorUtility class has a method called InstanceIDToObject thats what we need.

// get objects
Object o = EditorUtility.InstanceIDToObject(instanceID);
GameObject g = (GameObject)o as GameObject;
  • Drawing toggle button

The second parameter of our method is Rect. This rect is hierarchy windows gui rectangle. To make a toggle button or any other gui element we can create a new rectangle from this rect.

// make rectangle
 Rect r = new Rect (selectionRect);
 r.x = r.width - 10;
 r.width = 18;

It’s a small rect at right of each item over hierarchy window.

  • Final code

Here is the final code for our quick setactive button.

using UnityEngine;
using System.Collections;
using UnityEditor;
[InitializeOnLoad]
public class HierarchQuickSetActive {
 /// <summary>
 /// Initializer  class.
 /// </summary>
 static HierarchQuickSetActive ()
 {
     EditorApplication.hierarchyWindowItemOnGUI += hierarchWindowOnGUI;
 }
 /// <summary>
 /// Editor delegate callback
 /// </summary>
 /// İnstance id.
 /// Selection rect.
 static void hierarchWindowOnGUI (int instanceID, Rect selectionRect)
 {
     // make rectangle
     Rect r = new Rect (selectionRect);
     r.x = r.width - 10;
     r.width = 18;
     // get objects
     Object o = EditorUtility.InstanceIDToObject(instanceID);
     GameObject g = (GameObject)o as GameObject;
     // drag toggle gui
     g.SetActive(GUI.Toggle(r,g.activeSelf,string.Empty));
}

If you like this article please support my work on Patreon, thank you!

Advertisement

One thought on “Unity Editor Scripting-Quick SetActive

Add yours

  1. To properly inline content by right side instead of
    Rect r = new Rect (selectionRect);
    r.x = r.width – 10;
    r.width = 18;
    write
    Rect r = new Rect (selectionRect);
    r.x += r.width – 10;
    r.width = 18;

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Website Powered by WordPress.com.

Up ↑

%d bloggers like this: