Skip to content

Instantly share code, notes, and snippets.

@ztffn
Created May 24, 2021 12:52
Show Gist options
  • Save ztffn/7a8a57abcc1527f36c1a1ba505630605 to your computer and use it in GitHub Desktop.
Save ztffn/7a8a57abcc1527f36c1a1ba505630605 to your computer and use it in GitHub Desktop.
Adds tiny icons to the Unity Editor Transform Inspector for quick and easy resetting position and scale
using UnityEngine;
using UnityEditor;
[CanEditMultipleObjects]
[CustomEditor(typeof(Transform), true)]
public class NewTransformInspector : Editor
{
/// <summary>
/// Draw the inspector widget.
/// </summary>
public override void OnInspectorGUI ()
{
Transform t = (Transform)target;
float oldLabelWidth = EditorGUIUtility.labelWidth;
EditorGUIUtility.labelWidth = 15;
EditorGUILayout.BeginHorizontal();
bool resetPos = GUILayout.Button("P", GUILayout.Width(20f));
Vector3 position = EditorGUILayout.Vector3Field("", t.localPosition);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
bool resetRot = GUILayout.Button("R", GUILayout.Width(20f));
Vector3 eulerAngles = EditorGUILayout.Vector3Field("", t.localEulerAngles);
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
bool resetScale = GUILayout.Button("S", GUILayout.Width(20f));
Vector3 scale = EditorGUILayout.Vector3Field("", t.localScale);
EditorGUILayout.EndHorizontal();
EditorGUIUtility.labelWidth = oldLabelWidth;
if (resetPos) position = Vector3.zero;
if (resetRot) eulerAngles = Vector3.zero;
if (resetScale) scale = Vector3.one;
if (GUI.changed)
{
Undo.RecordObject(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