Skip to content

Instantly share code, notes, and snippets.

@drZool
Last active May 7, 2019 11:00
Show Gist options
  • Select an option

  • Save drZool/91d7ba12714c88533723085c79c285fb to your computer and use it in GitHub Desktop.

Select an option

Save drZool/91d7ba12714c88533723085c79c285fb to your computer and use it in GitHub Desktop.
Group selected scene objects. Place this script below a folder named "Editor"
using UnityEngine;
using System.Collections;
using UnityEditor;
public class GroupSceneObjects
{
[MenuItem("Hello/GroupSelected %g")]
static void GroupSelected(){
Transform topParent = null;
var objects = Selection.gameObjects;
int depth = 255;
foreach (var obj in objects) {
if (obj.transform.parent == null) {
topParent = null;
break;
}
int objdepth = GetDepth (obj.transform);
if (objdepth < depth) {
depth = objdepth;
topParent = obj.transform.parent;
}
}
var group = new GameObject ("Group").transform;
group.SetParent (topParent, false);
Undo.RegisterCreatedObjectUndo (group.gameObject, "Group");
foreach(var obj in objects){
Undo.SetTransformParent( obj.transform, group, "Group");
}
Selection.activeGameObject = group.gameObject;
}
static int GetDepth (Transform transform, int depth = 0)
{
if (transform.parent == null)
return depth;
return GetDepth (transform.parent, depth + 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment