Search Unity

Please add a quick way to reset Transforms position, rotation or scale

Discussion in 'Editor Workflows' started by Devil_Inside, Dec 21, 2018.

  1. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,119
    I know these actions are available when right-clicking the Transform component, but wouldn't it be 100% more convenient to have a small clickable button next to the "Position", "Rotation" and "Scale" labels that would allow me to reset any of these vectors in one click? I use these actions A LOT, so it really bothers me that there is no quicker way to do it.
     
    Ruslank100, JakubSmaga and BTStone like this.
  2. BTStone

    BTStone

    Joined:
    Mar 10, 2012
    Posts:
    1,422
    +1 for this feature.
     
  3. JakubSmaga

    JakubSmaga

    Joined:
    Aug 5, 2015
    Posts:
    417
    +1, Might be a huge time saver.
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Here is an editor extension that adds buttons next to the Transform input fields, to reset the position, rotation and scale:

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Reflection;
    5.  
    6. namespace TheEditorToolboxProject
    7. {
    8.     [CanEditMultipleObjects]
    9.     [CustomEditor(typeof(Transform))]
    10.     class TransformEditor : Editor
    11.     {
    12.         SerializedProperty m_LocalPosition;
    13.         SerializedProperty m_LocalRotation;
    14.         SerializedProperty m_LocalScale;
    15.         object m_TransformRotationGUI;
    16.  
    17.         void OnEnable()
    18.         {
    19.             if (serializedObject == null)
    20.                 return;
    21.  
    22.             m_LocalPosition = serializedObject.FindProperty("m_LocalPosition");
    23.             m_LocalRotation = serializedObject.FindProperty("m_LocalRotation");
    24.             m_LocalScale = serializedObject.FindProperty("m_LocalScale");
    25.  
    26.             if (m_TransformRotationGUI == null)
    27.                 m_TransformRotationGUI = System.Activator.CreateInstance(typeof(SerializedProperty).Assembly.GetType("UnityEditor.TransformRotationGUI", false, false));
    28.             m_TransformRotationGUI.GetType().GetMethod("OnEnable").Invoke(m_TransformRotationGUI, new object[] { m_LocalRotation, new GUIContent("Rotation") });
    29.         }
    30.  
    31.         public override void OnInspectorGUI()
    32.         {
    33.             var serObj = this.serializedObject;
    34.             if (serObj == null)
    35.                 return;
    36.  
    37.             serObj.Update();
    38.  
    39.             DrawLocalPosition();
    40.             DrawLocalRotation();
    41.             DrawLocalScale();
    42.  
    43.             DrawPropertiesExcluding(serObj, new string[] { "m_LocalPosition", "m_LocalRotation", "m_LocalScale" });
    44.  
    45.             Verify();
    46.  
    47.             serObj.ApplyModifiedProperties();
    48.         }
    49.  
    50.         void DrawLocalPosition()
    51.         {
    52.             using (new EditorGUILayout.HorizontalScope())
    53.             {
    54.                 EditorGUILayout.PropertyField(m_LocalPosition, new GUIContent("Position"));
    55.  
    56.                 if (GUILayout.Button(new GUIContent("0", "Reset Position"), EditorStyles.miniButton, GUILayout.Width(20)))
    57.                     m_LocalPosition.vector3Value = Vector3.zero;
    58.             }
    59.         }
    60.  
    61.         void DrawLocalRotation()
    62.         {
    63.             using (new EditorGUILayout.HorizontalScope())
    64.             {
    65.                 m_TransformRotationGUI.GetType().GetMethod("RotationField", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new[] { typeof(bool) }, null).Invoke(m_TransformRotationGUI, new object[] { false });
    66.  
    67.                 if (GUILayout.Button(new GUIContent("0", "Reset Rotation"), EditorStyles.miniButton, GUILayout.Width(20)))
    68.                     m_LocalRotation.quaternionValue = Quaternion.identity;
    69.             }
    70.         }
    71.  
    72.         void DrawLocalScale()
    73.         {
    74.             using (new EditorGUILayout.HorizontalScope())
    75.             {
    76.                 EditorGUILayout.PropertyField(m_LocalScale, new GUIContent("Scale"));
    77.  
    78.                 if (GUILayout.Button(new GUIContent("1", "Reset Scale"), EditorStyles.miniButton, GUILayout.Width(20)))
    79.                     m_LocalScale.vector3Value = Vector3.one;
    80.             }
    81.         }
    82.  
    83.         void Verify()
    84.         {
    85.             var transform = target as Transform;
    86.             var position = transform.position;
    87.             if (Mathf.Abs(position.x) > 100000f || Mathf.Abs(position.y) > 100000f || Mathf.Abs(position.z) > 100000f)
    88.                 EditorGUILayout.HelpBox("Due to floating-point precision limitations, it is recommended to bring the world coordinates of the GameObject within a smaller range.", MessageType.Warning);
    89.         }
    90.     }
    91. }
    92. #endif
    93.  
     
    Last edited: Aug 28, 2023
  5. I don't think this should be the part of the standard UI, it's crowded enough already and it provides the proper means to do the job.
     
    enhawk likes this.
  6. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,119
    Thanks a lot for this! I've been using a similar script since I've seen this being used in NGUI. Your script is so much cleaner solution than what I've been using previously, so I'll start using your script instead!
    I think a lot of people are missing out when not using something like this. Most of the user base probably doesn't even know that these actions can be implemented in such an accessible way. I really think this should be in Unity by default.
     
    Peter77 likes this.
  7. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    wait didn't they have a reset button on the left side of each transform component, it's an "R" button i think. is this removed?
     
  8. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    Thats a plugin
     
  9. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    Tab 0 tab 0 tab 0 is fast enough ;)
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Now do that for rotation and position, you got 6 times. And maybe you have few objects as well ...
    Pure productive fun ;)
     
  11. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    no it's not a plugin, but it got removed i think. And it's PRS apparently not R
    upload_2019-2-18_16-45-35.png
     
    Last edited: Feb 18, 2019
  12. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    If you have it on a few objects just select of all of them and do it once
     
  13. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    Pretty sure this is the NGui plugin. Not seen those buttons in native Unity before
     
  14. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    weird, i'm not using NGUI
     
  15. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,119
    That definitely looks like NGUI. If you're not using NGUI, there was a script floating around that was adding these buttons just like NGUI did, and it looked just like this. I was using it myself, so you probably have it somewhere in your project.
     
    Last edited: Feb 18, 2019
  16. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    yeah probably, i just don't remember what was i put in my project :D
     
  17. As_day

    As_day

    Joined:
    Nov 21, 2013
    Posts:
    5
  18. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Updated my post and added the code, see here
     
    Gamedelta and Reanimate_L like this.
  19. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    This is perfect @Peter77 , Thanks a lot. Can we get this as a built in feature?, would be a good QOL addition i think
    upload_2023-8-29_15-57-14.png
    EDIT : hmmm i just realized it changed the Contraint Proportion Buttons. . . .
     
    Last edited: Aug 29, 2023