Last active
May 18, 2020 16:22
-
-
Save ryanmillerca/bca15dc7a6daece70ad96853ae00ec67 to your computer and use it in GitHub Desktop.
Group tool for Unity3D
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
// 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