Tanoda
Hotkeys.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using UnityEditor;
11using System.Linq;
12using System.Collections.Generic;
13
14namespace Leap.Unity {
15
16 public static class Hotkeys {
17
18 [MenuItem("GameObject/Make Group %g")]
19 public static void MakeGroup() {
20 if (!CorePreferences.allowGroupObjectsHotkey) {
21 return;
22 }
23
24 GameObject[] objs = Selection.GetFiltered<GameObject>(SelectionMode.ExcludePrefab | SelectionMode.Editable);
25 if (objs.Length == 0) {
26 return;
27 }
28
29 Transform first = objs[0].transform;
30
31 List<Transform> hierarchy = new List<Transform>();
32
33 Transform parent = first.parent;
34 while (parent != null) {
35 hierarchy.Add(parent);
36 parent = parent.parent;
37 }
38
39 int index = 0;
40 parent = hierarchy.FirstOrDefault();
41
42 if (parent != null) {
43 foreach (var obj in objs) {
44 Transform t = obj.transform;
45 while (!t.IsChildOf(parent) || t == parent) {
46 index++;
47 if (index >= hierarchy.Count) {
48 parent = null;
49 break;
50 } else {
51 parent = hierarchy[index];
52 }
53 }
54 if (parent == null) {
55 break;
56 }
57 }
58 }
59
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.");
66
67 List<Transform> allTransforms = new List<Transform>();
68 if (parent == null) {
69 var sceneRoots = root.scene.GetRootGameObjects();
70 foreach (var sceneRoot in sceneRoots) {
71 allTransforms.AddRange(sceneRoot.GetComponentsInChildren<Transform>());
72 }
73 } else {
74 allTransforms.AddRange(parent.GetComponentsInChildren<Transform>());
75 }
76
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);
81
82 Vector3 newPos = obj.transform.localPosition;
83 Quaternion newRot = obj.transform.localRotation;
84 Vector3 newScale = obj.transform.localScale;
85
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 + ".");
89
90 obj.transform.localPosition = newPos;
91 obj.transform.localRotation = newRot;
92 obj.transform.localScale = newScale;
93 }
94 }
95
96 Selection.activeGameObject = root;
97 Undo.CollapseUndoOperations(Undo.GetCurrentGroup());
98 }
99
100 [MenuItem("GameObject/Reset Local Transform %e")]
101 public static void ResetAll() {
102 if (!CorePreferences.allowClearTransformHotkey) {
103 return;
104 }
105
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;
112 }
113 }
114
115 [MenuItem("GameObject/Reset Local Position and Rotation %#e")]
116 public static void ResetPositionRotation() {
117 if (!CorePreferences.allowClearTransformHotkey) {
118 return;
119 }
120
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;
126 }
127 }
128
129 [MenuItem("GameObject/Deselect All %#d")]
130 static void DeselectAll() {
131 if (!CorePreferences.allowClearTransformHotkey) {
132 return;
133 }
134
135 Selection.objects = new Object[0];
136 }
137
138 }
139}
UnityEngine.Object Object