Created
April 6, 2013 14:47
-
-
Save tsubaki/5326339 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using UnityEditor; | |
[CustomEditor(typeof(Transform))] | |
public class TransformEditorWith2D : Editor | |
{ | |
public override void OnInspectorGUI () | |
{ | |
Transform t = target as Transform; | |
GUI.changed = false; | |
if (t.GetComponent<Panel2D> () != null) { | |
Vector3 v3 = EditorGUILayout.Vector2Field ("position", t.localPosition); | |
t.localScale = EditorGUILayout.Vector2Field ("scale", t.localScale); | |
float depth = (float)EditorGUILayout.IntField ("depth", (int)t.localPosition.z); | |
if (GUI.changed) | |
t.localPosition = new Vector3 ( v3.x, v3.y, depth); | |
} else if (t.GetComponents<Component> ().Length == 1) { | |
t.localPosition = Vector3.zero; | |
t.localScale = new Vector3 (1, 1, 1); | |
t.rotation = Quaternion.identity; | |
} else { | |
EditorGUIUtility.LookLikeControls (); | |
EditorGUI.indentLevel = 0; | |
Vector3 position = EditorGUILayout.Vector3Field ("Position", t.localPosition); | |
Vector3 eulerAngles = EditorGUILayout.Vector3Field ("Rotation", t.localEulerAngles); | |
Vector3 scale = EditorGUILayout.Vector3Field ("Scale", t.localScale); | |
EditorGUIUtility.LookLikeInspector (); | |
if (GUI.changed) { | |
Undo.RegisterUndo (t, "Transform Change"); | |
t.localPosition = FixIfNaN (position); | |
t.localEulerAngles = FixIfNaN (eulerAngles); | |
t.localScale = FixIfNaN (scale); | |
} | |
} | |
} | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment