Unity Editor Script – Group Selected

posted in: Uncategorized | 0

This post offers an editor convenience class for Maya-like grouping of objects in the Unity scene hierarchy. It configures this script to run with the hotkey (Ctrl+G). Drop this into the editor. Your artists will love it. It currently does not take into account any parent groups that might be above your selected transforms. It will always try to group the selected to the an empty node at the world root.


using UnityEngine;
using UnityEditor;
using System.Collections;

public class GroupSelected : EditorWindow
    /// <summary>
    ///  Creates an empty node at the center of all selected nodes and parents all selected underneath it. 
    ///  Basically a nice re-creation of Maya grouping!
    /// </summary>

    [MenuItem("GameObject/Group Selected %g", priority = 80)]
    static void Init()
        Transform[] selected = Selection.GetTransforms(SelectionMode.ExcludePrefab | SelectionMode.TopLevel);

        GameObject emptyNode = new GameObject();
        Vector3 averagePosition = Vector3.zero;
        foreach(Transform node in selected)
            averagePosition += node.position;
        if (selected.Length > 0)
            averagePosition /= selected.Length;
        emptyNode.transform.position = averagePosition;
        emptyNode.name = "group";
        foreach (Transform node in selected)
            node.parent = emptyNode.transform;