Created
November 4, 2021 20:35
-
-
Save LaserKaspar/fe70d8f5b100074140d170426d40351f to your computer and use it in GitHub Desktop.
(UnityEngine) Adds Ctrl+G to group selected objects in inspector. Creates empty objects and set the parent of the objects to a new empty.
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 UnityEngine; | |
using UnityEditor; | |
[ExecuteInEditMode] | |
public class GroupUnityObjects : Editor | |
{ | |
[MenuItem("Edit/Group %g", false)] | |
public static void Group() | |
{ | |
if (Selection.transforms.Length > 0) | |
{ | |
GameObject group = new GameObject("Group"); | |
group.transform.parent = Selection.transforms[0].parent; | |
// Set Pivot | |
Vector3 pivotPosition = Vector3.zero; | |
foreach (Transform g in Selection.transforms) | |
{ | |
pivotPosition += g.transform.position; | |
} | |
pivotPosition /= Selection.transforms.Length; | |
group.transform.position = pivotPosition; | |
// Undo action | |
Undo.RegisterCreatedObjectUndo(group, "Group"); | |
foreach (GameObject s in Selection.gameObjects) | |
{ | |
Undo.SetTransformParent(s.transform, group.transform, "Group"); | |
} | |
Selection.activeGameObject = group; | |
} | |
else | |
{ | |
Debug.LogWarning("No selection!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment