Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Apply transformation

Discussion in 'Editor & General Support' started by unity_E94C2E8A95E908A9A7AB, May 1, 2024.

  1. unity_E94C2E8A95E908A9A7AB

    unity_E94C2E8A95E908A9A7AB

    Joined:
    Apr 4, 2024
    Posts:
    2
    Hello! I Have some house prefabs at my scene but the rotation of the prefabs x axis is -90 degrees. I want to make it 0 but I don't want the objects to be rotated. How can I apply the transformations of the prefabs?

    upload_2024-5-1_17-23-11.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,374
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    You could un-parent all the child items, set the parent to rotation 0,0,0 and then re-parent the children. However, this will break the prefab connection and is cumbersome.

    Depending on how much time you want to spend, extending the Editor functionality is useful.
    Personally I found I needed to do tasks like these this numerous times, so I created an Editor script to accomplish this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. public static class DebugMenu
    5. {
    6.     [MenuItem ("Debug/Transform/MakeLocalRotation0 (Child Adjust)")]
    7.     public static void MakeLocalRotation0 ()
    8.     {    
    9.         if (Selection.activeGameObject != null) {
    10.  
    11.             var selectedTransform = Selection.activeGameObject.transform;
    12.  
    13.             //Create a list of all the GOs children
    14.             List<Transform> childrenList = new List<Transform> ();
    15.             for (int i = 0; i < selectedTransform.childCount; i++) {
    16.                 childrenList.Add (selectedTransform.GetChild (i));
    17.             }
    18.  
    19.             var numChildren = childrenList.Count;
    20.  
    21.             for (int i = numChildren - 1; i >= 0; i--) {
    22.                 var child = childrenList [i];
    23.                 child.transform.parent = null;
    24.             }          
    25.             selectedTransform.localEulerAngles = Vector3.zero;
    26.  
    27.             //Restore children
    28.             for (int i = 0; i < numChildren; i++) {
    29.                 var child = childrenList [i];
    30.                 child.transform.parent = selectedTransform;
    31.             }
    32.  
    33.             DebugMenu.MarkSceneDirty ();
    34.  
    35.         } else {
    36.             Debug.LogError("No object selected");
    37.         }
    38.     }
    39. }
    40.  
    Put this in a .cs file in an Editor folder.
    Unity will create a "Debug" menu in the upper menu bar. You then select the gameObject, then choose
    Debug->MakeLocalRotation0 .
     
  4. unity_E94C2E8A95E908A9A7AB

    unity_E94C2E8A95E908A9A7AB

    Joined:
    Apr 4, 2024
    Posts:
    2

    This code saved me a lot of time! thank you! However I add the code to editor but it didn't work. After some time chatting with chat.gpt I got this code

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.SceneManagement; // Added this line for EditorSceneManager
    4. using System.Collections.Generic;
    5.  
    6. public static class DebugMenu
    7. {
    8.     [MenuItem("Debug/Transform/MakeLocalRotation0 (Child Adjust)")]
    9.     public static void MakeLocalRotation0()
    10.     {
    11.         if (Selection.activeGameObject != null)
    12.         {
    13.             var selectedTransform = Selection.activeGameObject.transform;
    14.  
    15.             //Create a list of all the GOs children
    16.             List<Transform> childrenList = new List<Transform>();
    17.             for (int i = 0; i < selectedTransform.childCount; i++)
    18.             {
    19.                 childrenList.Add(selectedTransform.GetChild(i));
    20.             }
    21.  
    22.             var numChildren = childrenList.Count;
    23.  
    24.             for (int i = numChildren - 1; i >= 0; i--)
    25.             {
    26.                 var child = childrenList[i];
    27.                 child.transform.parent = null;
    28.             }
    29.             selectedTransform.localEulerAngles = Vector3.zero;
    30.  
    31.             //Restore children
    32.             for (int i = 0; i < numChildren; i++)
    33.             {
    34.                 var child = childrenList[i];
    35.                 child.transform.parent = selectedTransform;
    36.             }
    37.  
    38.             MarkSceneDirty();
    39.  
    40.         }
    41.         else
    42.         {
    43.             Debug.LogError("No object selected");
    44.         }
    45.     }
    46.  
    47.     private static void MarkSceneDirty()
    48.     {
    49.         if (!Application.isPlaying)
    50.         {
    51.             EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
    52.         }
    53.     }
    54. }
    55.  
    I'm not sure what it changed and how it works :D I don't know C# but it did