Search Unity

Lock Transforms in editor script

Discussion in 'Scripting' started by petey, Nov 19, 2019.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hi all!

    I keep running into this issue where I accidentally transform the wrong object in a hierarchy, so I hacked together this little script that looks for any transforms I'm doing that I shouldn't be and redirects the selection back to the object that I should be transforming as well as reseting the transform I had wrongly adjusted.

    Now this is a terrible hacky script, but it's saving me all the time at the moment so I'm using it. Just wondering if anyone has any suggestions to make it a bit nicer though.

    Thanks,
    Pete

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. #if (UNITY_EDITOR)
    4. [ExecuteAlways]
    5.  
    6. public class LockTheThing : MonoBehaviour
    7. {
    8.  
    9.     public GameObject changeSelectionTo;
    10.     [Space(12)]
    11.     public bool lockScale;
    12.     public Vector3 baseScale = Vector3.one;
    13.     public bool lockRotation;
    14.     public Vector3 baseRot;
    15.     public bool lockTranslation;
    16.     public Vector3 basePos;
    17.  
    18.     [Space(12)]
    19.     public bool lockNonUniformScale;
    20.  
    21.     void OnGUI()
    22.     {
    23.         if (Application.isPlaying) return;
    24.  
    25.         if (lockNonUniformScale && this.transform.localScale.x != this.transform.localScale.y)
    26.         {
    27.             this.transform.localScale = new Vector3(this.transform.localScale.x, this.transform.localScale.x, this.transform.localScale.x);
    28.             SelectOtherGameObject();
    29.         }
    30.  
    31.         if (lockScale && this.transform.localScale != baseScale)
    32.         {
    33.             this.transform.localScale = baseScale;
    34.             SelectOtherGameObject();
    35.         }
    36.  
    37.         if (lockRotation && this.transform.localEulerAngles != baseRot)
    38.         {
    39.             this.transform.localEulerAngles = baseRot;
    40.             SelectOtherGameObject();
    41.         }
    42.  
    43.         if (lockTranslation && this.transform.localPosition != basePos)
    44.         {
    45.             this.transform.localPosition = basePos;
    46.             SelectOtherGameObject();
    47.         }
    48.     }
    49.  
    50.     void SelectOtherGameObject()
    51.     {
    52.         //Select the object you should be transforming
    53.         if (changeSelectionTo != null)
    54.         {
    55.             UnityEditor.Selection.activeTransform = changeSelectionTo.transform;
    56.         }
    57.     }
    58. }
    59.  
    60. #endif
     
  2. Riverman_Paul

    Riverman_Paul

    Joined:
    Mar 10, 2020
    Posts:
    1
    I also find myself in this position frequently. I want the ability in the editor to lock a transform that is not intended to be changed, so that no bugs are accidentally introduced.
     
  3. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246