Skip to content

Instantly share code, notes, and snippets.

@kevinw
Last active October 24, 2018 11:32
Show Gist options
  • Save kevinw/b1e64a16d30689945313e74b513f5a37 to your computer and use it in GitHub Desktop.
Save kevinw/b1e64a16d30689945313e74b513f5a37 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
public static class ResetSelectedTransforms
{
[MenuItem("Utilities/Reset Selected Transforms &z", true)]
static bool ValidateResetSelectedTransforms() { return Selection.activeTransform != null; }
/* Use option-z or alt-z to zero out the position and rotation and set the scale to 1 for
the selected transforms. */
[MenuItem("Utilities/Reset Selected Transforms &z")]
static void ResetSelectedTransorms() {
Undo.RecordObjects(Selection.gameObjects, "Reset Transforms of Selected Objects");
foreach (var selected in Selection.gameObjects) {
var transform = selected.transform;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
transform.localScale = Vector3.one;
}
}
[MenuItem("Utilities/Parent Selected in New GameObject &p", true)]
static bool ValidateParentSelectedInNewGameObject() { return Selection.activeTransform != null; }
/* Use option-p to reparent selected objects in a new empty game object at the same level in the
hierarchy. */
[MenuItem("Utilities/Parent Selected in New GameObject &p")]
public static void ParentSelectedInNewGameObject() {
var selectedObjs = Selection.gameObjects;
if (selectedObjs.Length == 0) {
Debug.LogError("You must select objects to reparent.");
return;
}
var parent = selectedObjs[0].transform.parent;
foreach (var obj in selectedObjs) {
if (obj.transform.parent != parent) {
Debug.LogError("All selected objects must have the same parent.");
return;
}
}
Undo.RecordObjects(Selection.gameObjects, "Reparent Selected Objects");
var siblingIndex = selectedObjs[0].transform.GetSiblingIndex();
var newParent = new GameObject().transform;
newParent.position = Vector3.zero;
newParent.SetParent(parent, false);
newParent.SetSiblingIndex(siblingIndex);
foreach (var selected in Selection.gameObjects) {
selected.transform.SetParent(newParent, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment