Wednesday 8 January 2014

Unity: Toggle GameObject active state using a shortcut key


As a daily Unity user I always found it cumbersome to press the 'set active' checkbox in the selected object's inspector view.

I wrote this originally for Unity 3.x because every time you toggled the checkbox, you would get a dialog asking you if you want to apply the change to all the children and most times you click yes.

Fortunately things changed in Unity 4.x and by default, applies to the whole hierarchy. But nevertheless, if you prefer using a shortcut over clicking that tiny checkbox, go ahead and give it shot. It's also very easy one time setup.

1. Create a folder named Editor inside Assets. It can be inside a subfolder

2. Inside this folder create an new script, replace it with this:

using UnityEditor;
using UnityEngine;
using System.Collections;

public class EditorHelper : MonoBehaviour
{
}

3. Copy paste the following into the class body

[MenuItem("GameObject/Toggle Selected GameObject _g")]
static void ToggleGameObject ()
{
  //var selected = Selection.transforms;
  if( Selection.transforms != null )
  {
    foreach( Transform t in Selection.transforms)
      t.gameObject.SetActive( !t.gameObject.activeInHierarchy );
  }
}

4. Now go to unity and select multiple objects from either the scene view or from the hierarchy. Press 'g' on the keyboard to toggle their GameObject's active state.

No comments:

Post a Comment

Contributors

Followers