Last active
August 29, 2015 13:57
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} | |
} |
- 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
If you have multiple Objects selected, multiple GameObjects will be created.