From Unify Community Wiki
Revision as of 17:00, 5 March 2011 by MatthewA (Talk | contribs)

Jump to: navigation, search


TransformInspector.cs is a reverse engineered version of Unity's built-in UnityEditor.TransformInspector class. It should both look and function exactly as the original, bugs and all. Once loaded into your project, all Transform components will be routed through TransformInspector.cs allowing you to easily extend or modify the inspector.

This is currently a much better alternative to using Editor.DrawDefaultInspector in your custom Transform inspector class since DrawDefaultInspector does not preserve the inspector's unique GUI layout.

While the first code provided is apparently an exact match of Unity's original, the firstSet variable does not actually appear to do anything at all. Therefore a cut-down version is also listed below.


Place TransformInspector.cs into an Editor folder inside of your project's Assets folder.


<csharp>// Reverse engineered UnityEditor.TransformInspector

using UnityEngine; using UnityEditor; using System.Collections;

[CustomEditor(typeof(Transform))] public class TransformInspector : Editor {

private bool firstSet; private Vector3 rotation; private Quaternion oldQuaternion;

public TransformInspector () { this.firstSet = true; }

public override void OnInspectorGUI () { EditorGUIUtility.LookLikeControls(); Transform t = (Transform); EditorGUI.indentLevel = 0; if (!this.firstSet) { if (this.oldQuaternion != t.localRotation) { this.firstSet = false; this.rotation = t.localEulerAngles; this.oldQuaternion = t.localRotation; } } Vector3 position = EditorGUILayout.Vector3Field("Position", t.localPosition); this.rotation = EditorGUILayout.Vector3Field("Rotation", this.rotation); Vector3 scale = EditorGUILayout.Vector3Field("Scale", t.localScale); if (GUI.changed) { Undo.RegisterUndo(t, "Transform Change"); this.rotation = this.FixIfNaN(this.rotation); t.localPosition = this.FixIfNaN(position); t.localEulerAngles = this.rotation; this.oldQuaternion = t.localRotation; t.localScale = this.FixIfNaN(scale); } EditorGUIUtility.LookLikeInspector(); }

private Vector3 FixIfNaN (Vector3 v) { if (float.IsNaN(v.x)) { v.x = 0; } if (float.IsNaN(v.y)) { v.y = 0; } if (float.IsNaN(v.z)) { v.z = 0; } return v; }

} </csharp>

Personal tools