Skip to content

Instantly share code, notes, and snippets.

@col000r
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save col000r/9758298 to your computer and use it in GitHub Desktop.
Save col000r/9758298 to your computer and use it in GitHub Desktop.
Drop this into the Editor folder. Press Shift+Cmd/Ctrl+Alt+N to create a new GameObject that is parented to the currently selected Object. Press Cmd/Ctrl+G to group the selected GameObject into a new GameObject.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class CreateEmptyParented : EditorWindow {
[MenuItem("GameObject/Create Empty (Parented to Selection) %&#n", false, -1)]
static void CreateEmptyParentedNow() {
List<Object> creations = new List<Object>();
Transform[] ts = Selection.transforms;
if(ts.Length > 0) {
for (int i = 0; i < ts.Length; i++) {
GameObject go = new GameObject("GameObject");
Undo.RegisterCreatedObjectUndo(go, "Create GameObject");
Undo.SetTransformParent(go.transform, ts[i], "Create Empty (Parented to Selection)");
go.transform.localPosition = Vector3.zero;
go.transform.localEulerAngles = Vector3.zero;
creations.Add(go);
}
} else {
GameObject go = new GameObject("GameObject");
Undo.RegisterCreatedObjectUndo(go, "Create Empty");
go.transform.localPosition = Vector3.zero;
go.transform.localEulerAngles = Vector3.zero;
creations.Add(go);
}
Selection.objects = creations.ToArray();
}
[MenuItem("GameObject/Group Selection %g", false, 12)]
static void GroupSelection() {
GameObject go = new GameObject("Group");
Undo.RegisterCreatedObjectUndo(go, "Created Group");
if(Selection.activeTransform) Undo.SetTransformParent(go.transform, Selection.activeTransform.parent, "Group Selection"); //Parent to activeTransform's parent
Transform[] ts = Selection.transforms;
//Find average position of selection and apply it
Vector3 pos = Vector3.zero;
for (int i = 0; i < ts.Length; i++) {
pos += ts[i].position;
}
pos = pos / ts.Length;
go.transform.position = pos;
//Now parent the selection to the new object
for (int i = 0; i < ts.Length; i++) {
Undo.SetTransformParent(ts[i], go.transform, "Group Selection");
}
}
}
@col000r
Copy link
Author

col000r commented Mar 25, 2014

If you have multiple Objects selected, multiple GameObjects will be created.

@col000r
Copy link
Author

col000r commented Mar 25, 2014

  • Cmd/Ctrl+G lets you group GameObjects
  • Supports the new Undo-System

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