Skip to content

Instantly share code, notes, and snippets.

@seckincengiz
Created January 3, 2018 11:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seckincengiz/6c4dc39574f4870e0cf0b0b5b3f970c4 to your computer and use it in GitHub Desktop.
Save seckincengiz/6c4dc39574f4870e0cf0b0b5b3f970c4 to your computer and use it in GitHub Desktop.
Group unity objects in edit mode | (CTRL + G) | Unity3D
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");
// 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!");
}
}
}
@LaserKaspar
Copy link

Thank you! Exactly what I was looking for.

Add
group.transform.parent = Selection.transforms[0].parent;
after line 12 to make grouping inside groups possible

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment