2
\$\begingroup\$

I've inherited a Unity scene with quite a deep object hierarchy. Some GameObjects in the hierarchy don't have any geometry of their own, and just exist to group their children and apply a MonoBehaviour to all of their children. Because of the way the scene was authored, some of these objects have arbitrary transforms, which makes it hard to edit the child transforms, to align the children with other objects outside the group.

As a one-off operation in the editor, I'd like to reset these objects' transforms, without changing the world-space positions and orientations of the children. Put another way, I'd like to push the effect of the transform down to the children.

What's the easiest way to achieve this in the editor? I've seen a lot of questions from people who want to achieve this in code, to have the effect of detaching the parent and child transforms while keeping the relationship, but that's not what I want. I'm just looking for a one-off, manual operation to make my scene file easier to edit.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

In the Hierarchy tab, you could click-and-drag the child Transforms to a root-level empty Transform. This will keep the children's world position, scale, and rotation, while also keeping them within a Transform you can collapse/expand easily enough as you make your changes without cluttering your scene. While you have the child Transforms selected, use save selection (Ctrl-Alt-1).

With the children safely moved, select the parent object, and in the Inspector, right-click the Transform label and choose Reset. This is the quickest way to reset the Transform.

When it comes time to reparent them again, use restore selection (Ctrl-Shift-1) to select the children again, and click-and-drag them back to their desired parent; their position, scale, and rotation will again remain the same.


If there are lots of Transforms you don't fancy reparenting by hand, you could create a MenuItem with a static method to find and manipulate your Transforms as needed.

Ways to access your Transforms from a MenuItem method include:

\$\endgroup\$
1
\$\begingroup\$

Something like this should do it.I'm sure how you want to reset the parent game object so in my code I just return it to 0,0,0 on both position and rotation.

You attach this component to the gameObject you want to changee :

using UnityEngine;
[ExecuteInEditMode]
public class FixChildren : MonoBehaviour {

    // Use this for initialization  
    public void FixTransforms()
    {
        // Make the changes to the transform here.
        transform.position = Vector3.zero;
        transform.rotation = Quaternion.identity;
    }
}

Then for easy use we create a custom Inspector for the component with a button that call the FixTransforms method :

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(FixChildren))]
public class CustomInspector : Editor {

    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        FixChildren myScript = (FixChildren)target;
        if (GUILayout.Button("Fix transforms"))
        {
            myScript.FixTransforms();
        }
    }
}

You can easily change this to go automatically trough all objects or leave it like this giving you finer control. Hope this helps.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .