Make all selected game objects child of an empty game object?

Hi,

How can I make some selected game objects as child of an newly created game object? So it would create a new game object, put the selected GO’s as children of it.

Thanks.

ok closest I can get is the context menu on the transform in the inspector since the Hierarchy Context menu is not directly available through the api.

    [MenuItem("CONTEXT/Transform/Add To New GameObject")]
    static void AddToNew(MenuCommand c)
    {
        Transform[] t = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.Editable);
        if (t[0] != null && t[0].Equals(c.context)) {
            GameObject go = new GameObject();
            for (int i = 0; i < t.Length; t[i++].parent = go.transform) ;
        }
    }

Since MenuItem is triggered for each item in the selection the trick is to check the first element against the context in order to only run once as the sample above does.