Skip to content

Instantly share code, notes, and snippets.

@ryanmillerca
Last active May 18, 2020 16:22
Show Gist options
  • Save ryanmillerca/bca15dc7a6daece70ad96853ae00ec67 to your computer and use it in GitHub Desktop.
Save ryanmillerca/bca15dc7a6daece70ad96853ae00ec67 to your computer and use it in GitHub Desktop.
Group tool for Unity3D
// Group Utility
// groups objects, registers Undo, and sets pivot as an average of children
// Made by Ryan Miller (https://www.ryanmiller.ca)
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
[ExecuteInEditMode]
public class GroupUtility : Editor {
[MenuItem("Edit/Group %g", false)]
public static void Group() {
if (Selection.transforms.Length > 0) {
GameObject group = new GameObject("Group");
// set pivot to average of selection
Vector3 pivotPosition = Vector3.zero;
foreach (Transform g in Selection.transforms) {
pivotPosition += g.transform.position;
}
pivotPosition /= Selection.transforms.Length;
group.transform.position = pivotPosition;
// register undo as we parent objects into the group
Undo.RegisterCreatedObjectUndo(group, "Group");
foreach (GameObject s in Selection.gameObjects) {
Undo.SetTransformParent(s.transform, group.transform, "Group");
}
Selection.activeGameObject = group;
}
else {
Debug.LogWarning("You must select one or more objects.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment