Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.