12using System.Collections.Generic;
16 public static class Hotkeys {
18 [MenuItem(
"GameObject/Make Group %g")]
19 public static void MakeGroup() {
20 if (!CorePreferences.allowGroupObjectsHotkey) {
24 GameObject[] objs = Selection.GetFiltered<GameObject>(SelectionMode.ExcludePrefab | SelectionMode.Editable);
25 if (objs.Length == 0) {
29 Transform first = objs[0].transform;
31 List<Transform> hierarchy =
new List<Transform>();
33 Transform parent = first.parent;
34 while (parent !=
null) {
35 hierarchy.Add(parent);
36 parent = parent.parent;
40 parent = hierarchy.FirstOrDefault();
43 foreach (var obj
in objs) {
44 Transform t = obj.transform;
45 while (!t.IsChildOf(parent) || t == parent) {
47 if (index >= hierarchy.Count) {
51 parent = hierarchy[index];
60 GameObject root =
new GameObject(
"Group");
61 root.transform.SetParent(parent);
62 root.transform.localPosition =
Vector3.zero;
63 root.transform.localRotation =
Quaternion.identity;
64 root.transform.localScale =
Vector3.one;
65 Undo.RegisterCreatedObjectUndo(root,
"Created group object.");
67 List<Transform> allTransforms =
new List<Transform>();
69 var sceneRoots = root.scene.GetRootGameObjects();
70 foreach (var sceneRoot
in sceneRoots) {
71 allTransforms.AddRange(sceneRoot.GetComponentsInChildren<Transform>());
74 allTransforms.AddRange(parent.GetComponentsInChildren<Transform>());
77 foreach (var obj
in allTransforms) {
78 if (objs.Contains(obj.gameObject)) {
79 Transform originalParent = obj.transform.parent;
80 obj.transform.SetParent(root.transform, worldPositionStays:
true);
82 Vector3 newPos = obj.transform.localPosition;
83 Quaternion newRot = obj.transform.localRotation;
84 Vector3 newScale = obj.transform.localScale;
86 obj.transform.SetParent(originalParent, worldPositionStays:
true);
87 Undo.SetTransformParent(obj.transform, root.transform,
"Moved " + obj.name +
" into group.");
88 Undo.RecordObject(obj.transform,
"Set new transform for " + obj.name +
".");
90 obj.transform.localPosition = newPos;
91 obj.transform.localRotation = newRot;
92 obj.transform.localScale = newScale;
96 Selection.activeGameObject = root;
97 Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
100 [MenuItem(
"GameObject/Reset Local Transform %e")]
101 public static void ResetAll() {
102 if (!CorePreferences.allowClearTransformHotkey) {
106 GameObject[] objs = Selection.GetFiltered<GameObject>(SelectionMode.ExcludePrefab | SelectionMode.Editable);
107 foreach (var obj
in objs) {
108 Undo.RecordObject(obj.transform,
"Cleared transform for " + obj.name +
".");
109 obj.transform.localPosition =
Vector3.zero;
110 obj.transform.localRotation =
Quaternion.identity;
111 obj.transform.localScale =
Vector3.one;
115 [MenuItem(
"GameObject/Reset Local Position and Rotation %#e")]
116 public static void ResetPositionRotation() {
117 if (!CorePreferences.allowClearTransformHotkey) {
121 GameObject[] objs = Selection.GetFiltered<GameObject>(SelectionMode.ExcludePrefab | SelectionMode.Editable);
122 foreach (var obj
in objs) {
123 Undo.RecordObject(obj.transform,
"Cleared local position and rotation for " + obj.name +
".");
124 obj.transform.localPosition =
Vector3.zero;
125 obj.transform.localRotation =
Quaternion.identity;
129 [MenuItem(
"GameObject/Deselect All %#d")]
130 static void DeselectAll() {
131 if (!CorePreferences.allowClearTransformHotkey) {
135 Selection.objects =
new Object[0];