Transform Inspector – Part 2

As I’ve said on the previous iteration of the transform inspector; the second main reason I wrote a custom implementation was to give me access to hidden components on the game object (something which I usually refer to as Entity but that’s a discussion for another post).

It’s not a terribly complicated process. Done in a few steps; Gather all components found on the transform’s owner then display them is a comfortable way and allow users to manipulate their hide flags. See, simple.

Let’s code!

The first thing we’re going to do, as before, is set up the basic display behaviour. In this case we want users to toggle the component view on/off.

public static class InspectHiddenComponents
{
  public static bool Show;

  public static void Perform(GameObject entity)
  {
    Show = EditorGUILayout.Foldout(Show, "Hidden Components");
    if (!Show)
      return;
  }
}

Next we want to actually see the components as they are presented in the game object; I also indent the list to keep in line with the inspection standards. Note that the component list is generated WITHOUT the transform since, well, it doesn’t like it. For one, hiding the transform will mean we lose the component display and will not be able to show it again. And secondly, as I’ve discovered, calling the layout utility on the component under inspection does some funny things to Unity.

public static class InspectHiddenComponents
{
  public static bool Show;

  public static void Perform(GameObject entity)
  {
    Show = EditorGUILayout.Foldout(Show, "Hidden Components");
    if (!Show)
      return;

    EditorGUI.indentLevel += 1;           
    
    var components = entity.GetComponents();
    components = components.Where(item => item.GetType() != typeof (Transform));
    foreach (var component in components)
    {
      var component_name = component.GetType().Name;
      var component_hidden = false;
      component_hidden = EditorGUILayout.Toggle(component_name, component_hidden);
    }
    
    EditorGUI.indentLevel -= 1;
  }
}

And finally we want to update each component’s flags field to comply with the user’s request.

public static class InspectHiddenComponents
{
  public static bool Show;

  public static void Perform(GameObject entity)
  {
    Show = EditorGUILayout.Foldout(Show, "Hidden Components");
    if (!Show)
      return;

    EditorGUI.indentLevel += 1;           
    
    var components = entity.GetComponents();
    components = components.Where(item => item.GetType() != typeof (Transform));
    foreach (var component in components)
    {
      var component_name = component.GetType().Name;
      var component_hidden = component.hideFlags == HideFlags.HideInInspector;;
      component_hidden = EditorGUILayout.Toggle(component_name, component_hidden);
      component.hideFlags = component_hidden ? HideFlags.HideInInspector : HideFlags.None
    }
    
    EditorGUI.indentLevel -= 1;
  }
}

And that’s it. Just call this function in the transform inspector. And we’re done.

We scanned the skies with rainbow eyes and saw machines of every shape and size. We talked with tall Venusians passing through.

Advertisements
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

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