Skip to content

Instantly share code, notes, and snippets.

@gekidoslair
Created November 10, 2019 01:07
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gekidoslair/a1fff9d32784b2831cb6fe5cc76c23a7 to your computer and use it in GitHub Desktop.
Save gekidoslair/a1fff9d32784b2831cb6fe5cc76c23a7 to your computer and use it in GitHub Desktop.
Quick utility to reset a parent GameObject position to 0,0,0 without screwing up the child positions
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace MWU.Shared.Utilities
{
public static class ResetParentTransform
{
[MenuItem("Tools/Edit/Reset Parent Transform %_r")]
public static void DistributeObjectsEvenly()
{
var selectedObjects = Selection.gameObjects;
var sourcePosition = selectedObjects[0];
Undo.RegisterCompleteObjectUndo(selectedObjects, "Reset Parent Transforms");
foreach (GameObject selectedObject in selectedObjects)
{
ResetChildTransforms(selectedObject.transform);
}
}
private static void ResetChildTransforms( Transform parent)
{
// make a temp parent
var newGo = new GameObject();
// grab all of the children
var children = new List<Transform>();
for( var i = 0; i < parent.childCount; i++)
{
children.Add(parent.GetChild(i));
}
// move the children out of the parent temporarily
foreach( var child in children)
{
child.parent = newGo.transform;
}
// reset the parent transform
parent.position = Vector3.zero;
parent.rotation = Quaternion.identity;
// and reparent
foreach( var child in children)
{
child.parent = parent;
}
GameObject.DestroyImmediate(newGo);
}
}
}
@gekidoslair
Copy link
Author

This is included in the Utilities package here:
https://github.com/PixelWizards/com.pixelwizards.utilities

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment