Skip to content

Instantly share code, notes, and snippets.

@neuroKip
Last active February 6, 2020 11:23
Show Gist options
  • Save neuroKip/24c2d31388e8128e6d3f628685b4e80c to your computer and use it in GitHub Desktop.
Save neuroKip/24c2d31388e8128e6d3f628685b4e80c to your computer and use it in GitHub Desktop.
A Unity3D editor script to add reset buttons to the transform inspector and Vector3 copy paste functionality.Add it to a folder called Editor in your project to use it.
using UnityEngine;
using UnityEditor;
/// <summary>
/// Adds reset buttons to transforms to quickly reset position, rotation and scale to default values
/// Features:
/// - Copy and paste values in another transform by clicking on a label (E.G. Position) and using CTR+C CTRL+V
/// </summary>
[CustomEditor( typeof( Transform ) )]
public class Inspector_Transform : Editor
{
public override void OnInspectorGUI()
{
Transform t = (Transform)target;
Undo.RecordObject( t, "Transform Change" );
Vector3 p = t.localPosition;
Vector3 r = t.localEulerAngles;
Vector3 s = t.localScale;
// Position
EditorGUILayout.BeginHorizontal();
{
if( GUILayout.Button( "0", GUILayout.Width( 20 ) ) )
t.localPosition = Vector3.zero;
GUI.SetNextControlName( "Position" );
p = EditorGUILayout.Vector3Field( "Position", t.localPosition );
CopyPaste( "Position", ref p );
}
EditorGUILayout.EndHorizontal();
// Rotation
EditorGUILayout.BeginHorizontal();
{
if( GUILayout.Button( "0", GUILayout.Width( 20 ) ) )
t.localEulerAngles = Vector3.zero;
GUI.SetNextControlName( "Rotation" );
r = EditorGUILayout.Vector3Field( "Rotation", t.localEulerAngles );
CopyPaste( "Rotation", ref r );
}
EditorGUILayout.EndHorizontal();
// Scale
EditorGUILayout.BeginHorizontal();
{
// Float field for uniform scale
float uniScale = (t.localScale == (Vector3.one * t.localScale.x)) ? t.localScale.x : 0.0f;
EditorGUI.BeginChangeCheck();
//if( GUILayout.Button( "1", GUILayout.Width( 20 ) ) ) // Comment this out if you want a button to reset scale to 1
// uniScale = 1.0f;
uniScale = EditorGUILayout.FloatField( uniScale, GUILayout.Width( 20 ) );
if( EditorGUI.EndChangeCheck() )
t.localScale = Vector3.one * uniScale;
GUI.SetNextControlName( "Scale" );
s = EditorGUILayout.Vector3Field( "Scale", t.localScale );
CopyPaste( "Scale", ref s );
}
EditorGUILayout.EndHorizontal();
if( GUI.changed )
{
t.localPosition = p;
t.localEulerAngles = r;
t.localScale = s;
}
}
void CopyPaste( string controlName, ref Vector3 v3 )
{
if( Event.current.isKey == false || GUI.GetNameOfFocusedControl() != controlName )
return;
if( Event.current.control && Event.current.keyCode == KeyCode.C )
{
EditorGUIUtility.systemCopyBuffer = JsonUtility.ToJson( v3 );
GUI.changed = true;
}
else if( Event.current.control && Event.current.keyCode == KeyCode.V )
{
v3 = JsonUtility.FromJson<Vector3>( EditorGUIUtility.systemCopyBuffer );
GUI.changed = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment