Skip to content

Instantly share code, notes, and snippets.

@LaserKaspar
Created November 4, 2021 20:35
Show Gist options
  • Save LaserKaspar/fe70d8f5b100074140d170426d40351f to your computer and use it in GitHub Desktop.
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.
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