Created
November 10, 2019 01:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is included in the Utilities package here:
https://github.com/PixelWizards/com.pixelwizards.utilities