Transform Inspector

Yes. Yes I did. I wrote my own transform inspector to use instead of Unity’s built-in version. Why? For several reasons. First, I wanted to be able to easily switch between global/local data display. And secondly, I wanted to see and perform operations on hidden components in the entity (I could have written a new editing-only component to allow me this but I decided that the best place will be on the Transform display itself).

So let’s get to it. First, global/local information. To switch between these views we’ll need to save which view we’re currently on. Could use a boolean. Better to write an enum.

public enum ObjectSpace
{
  Local = 0,
  Global = 1,
}

Now let’s create our basic transform inspector.

[CustomEditor(typeof(Transform))]
public static class TransformInspector
  : UnityEditor.Editor
{
  public static ObjectSpace Space;

  public override void OnInspectorGUI()
  {
    Space = (ObjectSpace) EditorGUILayout.EnumPopup("Space", Space);
  }
}

With that simple concept out of the way. We now need to actually inspect our values based on the selected space. For that we’ll need to retrieve the correct data given some space value. So let’s create a utility for that (For brevity’s sake I’ll just operate on positions).

public static class GetPosition
{
  public static Vector3 Perform(Transform transform, ObjectSpace space)
  {
    switch (space)
    {
      case ObjectSpace.Local: return transform.localPosition;
      case ObjectSpace.Global: return transform.position;
      default: throw new NotImplementedException();
    }
  }
}

Let’s inspect our positions.

[CustomEditor(typeof(Transform))]
public static class TransformInspector
  : UnityEditor.Editor
{
  public static ObjectSpace Space;

  public override void OnInspectorGUI()
  {
    Space = (ObjectSpace) EditorGUILayout.EnumPopup("Space", Space);
    
    var transform = (Transform) target;
    var position = EditorGUILayout.InspectVector3("Position", GetPosition.Perform(transform, Space));
  }
}

Not too shabby. Now we do the same but instead of getting positions based on space we set position based on space.

public static class SetPosition
{
  public static Transform Perform(Transform transform, Vector3 value, ObjectSpace space)
  {
    switch (space)
    {
      case ObjectSpace.Local: transform.localPosition = value; break;
      case ObjectSpace.Global: transform.position = value; break;
      default: throw new NotImplementedException();
    }
    return transform;
  }
}

As for why I’m returning the transform? Just following some advice from the wonderful Effective C++ where were we? Ah yeah. Setting positions.

[CustomEditor(typeof(Transform))]
public static class TransformInspector
  : UnityEditor.Editor
{
  public static ObjectSpace Space;

  public override void OnInspectorGUI()
  {
    Space = (ObjectSpace) EditorGUILayout.EnumPopup("Space", Space);
    
    var transform = (Transform) target;
    var position = EditorGUILayout.InspectVector3("Position", GetPosition.Perform(transform, Space));
    SetPosition.Perform(transform, position, Space);
  }
}

Simple. Extend this for rotations and scale (remember, in Unity only local scale can be set).
Eh, this post is a bit longer than I expected. So I’ll separate it into two. The next post will describe the hidden components inspection.

“There was music in the cafés at night, And revolution in the air”

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