TransformInspector

From Unify Community Wiki
Revision as of 00:50, 10 July 2010 by Mstevenson (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Description

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.

Usage

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

TransformInspector.cs

<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)this.target; 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
Namespaces

Variants
Actions
Navigation
Extras
Toolbox