Search Unity

Moving Large Amounts of Objects in Scene by XYZ Offset

Discussion in 'World Building' started by GXMark, Feb 10, 2020.

  1. GXMark

    GXMark

    Joined:
    Oct 13, 2012
    Posts:
    515
    I have a project where moving a large amount of objects relative to an xyz offset is required.

    Doing a drag object selection and movement is ok but if you want precise control over position its abit hit or miss doing this.

    The following editor script is very simple and accomplishes for you by only moving objects tagged with "Object".

    Code (CSharp):
    1. public class ParcelMover : EditorWindow
    2. {
    3.     static private GameObject parcelRoot = null;
    4.     static private Vector3 moveOffset = Vector3.zero;
    5.  
    6.     [MenuItem("MyUtility/Parcel Mover")]
    7.     public static void ShowWindow()
    8.     {
    9.         EditorWindow.GetWindow(typeof(ParcelMover), true, "Parcel Mover");
    10.     }
    11.  
    12.     void OnGUI()
    13.     {
    14.         GUILayout.Label("Parcel Mover");
    15.  
    16.         parcelRoot = (GameObject)EditorGUILayout.ObjectField("Parcel Root Object", parcelRoot, typeof(GameObject), true);
    17.  
    18.         moveOffset = EditorGUILayout.Vector3Field("Move Offset", moveOffset);
    19.        
    20.         if(GUILayout.Button("Move"))
    21.         {
    22.             Transform[] transforms = parcelRoot.GetComponentsInChildren<Transform>(true);
    23.  
    24.             foreach(Transform t in transforms)
    25.             {
    26.                 if (t.tag == "Object")
    27.                 {
    28.                     t.position = new Vector3(t.position.x + moveOffset.x, t.position.y + moveOffset.y, t.position.z + moveOffset.z);
    29.                 }
    30.             }
    31.  
    32.             Debug.Log("Moved Parcel Successfully by Offset " + moveOffset);
    33.         }
    34.     }
    35. }