Tanoda
InteractionChecks.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 System;
10using UnityEngine;
11using UnityEditor;
12
14
15 using UnityObject = UnityEngine.Object;
16 using Query;
17
18 public static class InteractionChecks {
19 public const float MAX_GRAVITY_MAGNITUDE = 4.905f;
20
21#if UNITY_ANDROID
22 public const float MAX_TIMESTEP = 1.0f / 60.0f;
23#else
24 public const float MAX_TIMESTEP = 1.0f / 90.0f;
25#endif
26
27 private const string IGNORE_GRAVITY_CHECK_KEY = "LeapIEIgnoreGravityCheck";
28 private const string IGNORE_TIMESTEP_CHECK_KEY = "LeapIEIgnoreTimestepCheck";
29 private const string IGNORE_RIGID_HANDS_CHECK_KEY = "LeapIEIgnoreRigidHandsCheck";
30
31 private const string SHOULD_LAUNCH_FOR_IE = "LeapWindowPanelShouldLaunchForIE";
32
33 [InitializeOnLoadMethod]
34 private static void init() {
35 EditorApplication.delayCall += () =>
36 {
37 EditorPrefs.SetBool(SHOULD_LAUNCH_FOR_IE, false);
38 if (EditorPrefs.GetBool(SHOULD_LAUNCH_FOR_IE, defaultValue: true)) {
39 EditorPrefs.SetBool(SHOULD_LAUNCH_FOR_IE, false);
41 }
42 };
43
44 EditorApplication.playModeStateChanged -= onPlayModeStateChanged;
45 EditorApplication.playModeStateChanged += onPlayModeStateChanged;
46 }
47
48 private static void onPlayModeStateChanged(PlayModeStateChange stateChange) {
49 if (stateChange == PlayModeStateChange.EnteredPlayMode) {
50 bool allChecksPassed = runAllChecks_NoGUI();
51
52 if (!allChecksPassed) {
53 EditorApplication.delayCall += LeapUnityWindow.Init;
54 }
55 }
56 }
57
58 [LeapProjectCheck("Interaction Engine", 10)]
59 public static bool CheckInteractionSettings() {
60 EditorGUILayout.LabelField("Interaction Engine", EditorStyles.boldLabel);
61
62 bool allChecksPassed = runAllChecks_WithGUI();
63
64 EditorGUILayout.Space();
65
66 if (allChecksPassed) {
67 EditorGUILayout.HelpBox("All settings are good for the Interaction Engine!",
68 MessageType.Info);
69 }
70
71 return true;
72 }
73
74 #region Run All Checks
75
76 private static bool runAllChecks_NoGUI() {
77 bool allChecksPassed = true;
78
79 allChecksPassed &= checkGravity();
80
81 allChecksPassed &= checkTimeStep();
82
83 allChecksPassed &= checkRigidHands();
84
85 return allChecksPassed;
86 }
87
88 private static bool runAllChecks_WithGUI() {
89 bool allChecksPassed = true;
90
91 allChecksPassed &= checkGravityAndDrawGUI();
92
93 allChecksPassed &= checkTimeStepAndDrawGUI();
94
95 allChecksPassed &= checkRigidHandsAndDrawGUI();
96
97 if (!allChecksPassed && Application.isPlaying) {
98 EditorGUILayout.Space();
99
100 EditorGUILayout.HelpBox("Editor is currently in play-mode, so any project setting "
101 + "changes will be reverted upon returning to edit-mode. You should return to "
102 + "edit-mode if you want auto-fixes to stick.", MessageType.Warning);
103 }
104
105 return allChecksPassed;
106 }
107
108 #endregion
109
110 #region Check Gravity
111
112 private static bool checkGravity() {
113 if (LeapProjectChecks.CheckIgnoredKey(IGNORE_GRAVITY_CHECK_KEY)) {
114 return true;
115 }
116
117 if (Mathf.Abs(Physics.gravity.y) > MAX_GRAVITY_MAGNITUDE) {
118 return false;
119 }
120 else {
121 return true;
122 }
123 }
124
125 private static bool checkGravityAndDrawGUI() {
126 var gravityOK = checkGravity();
127
128 if (!gravityOK) {
129
130 EditorGUILayout.Space();
131
132 using (new EditorGUILayout.HorizontalScope()) {
133 EditorGUILayout.HelpBox("Your gravity magnitude is " + Physics.gravity.y
134 + " which is stronger than the recommended value "
135 + "of -4.905!\n\nGo to Edit/Project Settings/Physics "
136 + "to change the magnitude.", MessageType.Warning);
137
138 if (GUILayout.Button("Auto-fix")) {
139 Physics.gravity = Vector3.down * MAX_GRAVITY_MAGNITUDE;
140 }
141
142 if (GUILayout.Button("Ignore")) {
143 LeapProjectChecks.SetIgnoredKey(IGNORE_GRAVITY_CHECK_KEY, ignore: true);
144 }
145 }
146 }
147
148 return gravityOK;
149 }
150
151 #endregion
152
153 #region Check Timestep
154
155 private static bool checkTimeStep() {
156 if (LeapProjectChecks.CheckIgnoredKey(IGNORE_TIMESTEP_CHECK_KEY)) {
157 return true;
158 }
159
160 if (Time.fixedDeltaTime > MAX_TIMESTEP + Mathf.Epsilon) {
161 return false;
162 }
163 else {
164 return true;
165 }
166 }
167
168 private static bool checkTimeStepAndDrawGUI() {
169 var timeStepOK = checkTimeStep();
170
171 if (!timeStepOK) {
172 float roundedTimestep = (float)Math.Round(MAX_TIMESTEP, 4);
173
174 EditorGUILayout.Space();
175
176 using (new EditorGUILayout.HorizontalScope()) {
177 EditorGUILayout.HelpBox(
178 "Your fixed timestep is " + Time.fixedDeltaTime +
179 ", which is slower than the recommended value " +
180 "of " + roundedTimestep + ".\n\nGo to Edit/Project Settings/Time " +
181 "to change the fixed timestep.", MessageType.Warning);
182
183 if (GUILayout.Button("Auto-fix")) {
184 Time.fixedDeltaTime = roundedTimestep;
185 }
186
187 if (GUILayout.Button("Ignore")) {
188 LeapProjectChecks.SetIgnoredKey(IGNORE_TIMESTEP_CHECK_KEY, ignore: true);
189 }
190 }
191 }
192
193 return timeStepOK;
194 }
195
196 #endregion
197
198 #region Check Rigid Hands
199
200 private static bool checkRigidHands() {
201 var unusedRigidHandObjects = (GameObject[])null;
202 return checkRigidHands(out unusedRigidHandObjects);
203 }
204
205 private static bool checkRigidHands(out GameObject[] rigidHandObjects) {
206 if (LeapProjectChecks.CheckIgnoredKey(IGNORE_RIGID_HANDS_CHECK_KEY)) {
207 rigidHandObjects = null;
208 return true;
209 }
210
211 rigidHandObjects = UnityObject.FindObjectsOfType<RigidHand>().Query()
212 .Select(x => x.gameObject).ToArray();
213 if (rigidHandObjects.Length != 0
214 && UnityObject.FindObjectOfType<InteractionManager>() != null) {
215 return false;
216 }
217 else {
218 return true;
219 }
220 }
221
222 private static bool checkRigidHandsAndDrawGUI() {
223 var rigidHandObjects = (GameObject[])null;
224 var rigidHandsOK = checkRigidHands(out rigidHandObjects);
225
226 if (!rigidHandsOK) {
227
228 EditorGUILayout.Space();
229
230 using (new EditorGUILayout.HorizontalScope()) {
231 EditorGUILayout.HelpBox(
232 "Rigid Hands AND an Interaction Manager are present in your scene. " +
233 "Rigid Hands are not compatible with the Interaction Engine and should " +
234 "never be used in tandem with it. You should remove them " +
235 "from your scene.", MessageType.Warning);
236
237 if (GUILayout.Button(new GUIContent("Select Rigid Hands",
238 "Select RigidHand objects in the current scene."),
239 GUILayout.ExpandHeight(true), GUILayout.MaxHeight(40F))) {
240 Selection.objects = rigidHandObjects;
241 }
242
243 if (GUILayout.Button("Ignore")) {
244 LeapProjectChecks.SetIgnoredKey(IGNORE_RIGID_HANDS_CHECK_KEY, ignore: true);
245 }
246 }
247 }
248
249 return rigidHandsOK;
250 }
251
252 #endregion
253
254 }
255}
Es.InkPainter.Math Math
Definition: PaintTest.cs:7
A Query object is a type of immutable ordered collection of elements that can be used to perform usef...
Definition: Query.cs:90