BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Editor / OVREngineConfigurationUpdater.cs

OVREngineConfigurationUpdater.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVREngineConfigurationUpdater.cs 12.3 KiB Download Raw
1/************************************************************************************
2
3Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
4
5Licensed under the Oculus SDK License Version 3.4.1 (the "License");
6you may not use the Oculus SDK except in compliance with the License,
7which is provided at the time of installation or download, or which
8otherwise accompanies this software in either electronic or hard copy form.
9
10You may obtain a copy of the License at
11
12https://developer.oculus.com/licenses/sdk-3.4.1
13
14Unless required by applicable law or agreed to in writing, the Oculus SDK
15distributed under the License is distributed on an "AS IS" BASIS,
16WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17See the License for the specific language governing permissions and
18limitations under the License.
19
20************************************************************************************/
21
22using UnityEngine;
23using UnityEditor;
24using UnityEditor.Callbacks;
25using System;
26using System.IO;
27
28[InitializeOnLoad]
29class OVREngineConfigurationUpdater
30{
31 private const string prefName = "OVREngineConfigurationUpdater_Enabled";
32 private const string menuItemName = "Oculus/Tools/Use Required Project Settings";
33 private const string androidAssetsPath = "Assets/Plugins/Android/assets";
34 private const string androidManifestPath = "Assets/Plugins/Android/AndroidManifest.xml";
35 static bool setPrefsForUtilities;
36
37 [MenuItem(menuItemName)]
38 static void ToggleUtilities()
39 {
40 setPrefsForUtilities = !setPrefsForUtilities;
41 Menu.SetChecked(menuItemName, setPrefsForUtilities);
42
43 int newValue = (setPrefsForUtilities) ? 1 : 0;
44 PlayerPrefs.SetInt(prefName, newValue);
45 PlayerPrefs.Save();
46
47 Debug.Log("Using required project settings: " + setPrefsForUtilities);
48 }
49
50#if UNITY_2017_3_OR_NEWER
51 private static readonly string dashSupportEnableConfirmedKey = "Oculus_Utilities_OVREngineConfiguration_DashSupportEnableConfirmed_" + Application.unityVersion + OVRManager.utilitiesVersion;
52 private static bool dashSupportEnableConfirmed
53 {
54 get
55 {
56 return PlayerPrefs.GetInt(dashSupportEnableConfirmedKey, 0) == 1;
57 }
58
59 set
60 {
61 PlayerPrefs.SetInt(dashSupportEnableConfirmedKey, value ? 1 : 0);
62 }
63 }
64
65 private static void DashSupportWarningPrompt()
66 {
67 /// <summary>
68 /// Since Unity 2017.3.0f1 and 2017.3.0f2 have "Dash Support" enabled by default
69 /// We need prompt developers in case they never test their app with dash
70 /// </summary>
71 ///
72 if (Application.unityVersion == "2017.3.0f1" || Application.unityVersion == "2017.3.0f2")
73 {
74 if (!dashSupportEnableConfirmed)
75 {
76 bool dialogResult = EditorUtility.DisplayDialog("Oculus Dash support", "Your current Unity engine " + Application.unityVersion +
77 " has Oculus Dash Supporting enabled by default. please make sure to test your app with Dash enabled runtime 1.21 or newer," +
78 " Otherwise, you can also turn it off under XR Settings -> Oculus", "Understand", "Learn more ");
79
80 if (!dialogResult)
81 {
82 Application.OpenURL("https://developer.oculus.com/documentation/unity/latest/concepts/unity-lifecycle/");
83 }
84
85 dashSupportEnableConfirmed = true;
86 }
87 }
88 }
89#endif
90
91 static OVREngineConfigurationUpdater()
92 {
93 EditorApplication.delayCall += OnDelayCall;
94 EditorApplication.update += OnUpdate;
95
96#if UNITY_2017_3_OR_NEWER
97 DashSupportWarningPrompt();
98#endif
99 }
100
101 static void OnDelayCall()
102 {
103 setPrefsForUtilities = PlayerPrefs.GetInt(prefName, 1) != 0;
104 Menu.SetChecked(menuItemName, setPrefsForUtilities);
105
106 if (!setPrefsForUtilities)
107 return;
108
109 OVRPlugin.AddCustomMetadata("build_target", EditorUserBuildSettings.activeBuildTarget.ToString());
110 EnforceAndroidSettings();
111 EnforceInputManagerBindings();
112#if UNITY_ANDROID
113 EnforceOSIG();
114#endif
115 }
116
117 static void OnUpdate()
118 {
119 if (!setPrefsForUtilities)
120 return;
121
122 EnforceBundleId();
123#if !USING_XR_SDK
124 EnforceVRSupport();
125#endif
126 EnforceInstallLocation();
127 }
128
129 static void EnforceAndroidSettings()
130 {
131 if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android)
132 return;
133
134 if (PlayerSettings.defaultInterfaceOrientation != UIOrientation.LandscapeLeft)
135 {
136 Debug.Log("OVREngineConfigurationUpdater: Setting orientation to Landscape Left");
137 // Default screen orientation must be set to landscape left.
138 PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft;
139 }
140
141 if (!PlayerSettings.virtualRealitySupported)
142 {
143 // NOTE: This value should not affect the main window surface
144 // when Built-in VR support is enabled.
145
146 // NOTE: On Adreno Lollipop, it is an error to have antiAliasing set on the
147 // main window surface with front buffer rendering enabled. The view will
148 // render black.
149 // On Adreno KitKat, some tiling control modes will cause the view to render
150 // black.
151 if (QualitySettings.antiAliasing != 0 && QualitySettings.antiAliasing != 1)
152 {
153 Debug.Log("OVREngineConfigurationUpdater: Disabling antiAliasing");
154 QualitySettings.antiAliasing = 1;
155 }
156 }
157
158 if (QualitySettings.vSyncCount != 0)
159 {
160 Debug.Log("OVREngineConfigurationUpdater: Setting vsyncCount to 0");
161 // We sync in the TimeWarp, so we don't want unity syncing elsewhere.
162 QualitySettings.vSyncCount = 0;
163 }
164 }
165
166 static void EnforceVRSupport()
167 {
168 if (PlayerSettings.virtualRealitySupported)
169 return;
170
171 var mgrs = GameObject.FindObjectsOfType<OVRManager>();
172 for (int i = 0; i < mgrs.Length; ++i)
173 {
174 if (mgrs [i].isActiveAndEnabled)
175 {
176 Debug.Log ("Enabling Unity VR support");
177 PlayerSettings.virtualRealitySupported = true;
178
179 bool oculusFound = false;
180#if UNITY_2017_2_OR_NEWER
181 foreach (var device in UnityEngine.XR.XRSettings.supportedDevices)
182#else
183 foreach (var device in UnityEngine.VR.VRSettings.supportedDevices)
184#endif
185 oculusFound |= (device == "Oculus");
186
187 if (!oculusFound)
188 Debug.LogError("Please add Oculus to the list of supported devices to use the Utilities.");
189
190 return;
191 }
192 }
193 }
194
195 private static void EnforceBundleId()
196 {
197 if (!PlayerSettings.virtualRealitySupported)
198 return;
199
200 if (PlayerSettings.applicationIdentifier == "" || PlayerSettings.applicationIdentifier == "com.Company.ProductName")
201 {
202 string defaultBundleId = "com.oculus.UnitySample";
203 Debug.LogWarning("\"" + PlayerSettings.applicationIdentifier + "\" is not a valid bundle identifier. Defaulting to \"" + defaultBundleId + "\".");
204 PlayerSettings.applicationIdentifier = defaultBundleId;
205 }
206 }
207
208 private static void EnforceInstallLocation()
209 {
210 if (PlayerSettings.Android.preferredInstallLocation != AndroidPreferredInstallLocation.Auto)
211 PlayerSettings.Android.preferredInstallLocation = AndroidPreferredInstallLocation.Auto;
212 }
213
214 private static void EnforceInputManagerBindings()
215 {
216 try
217 {
218 BindAxis(new Axis() { name = "Oculus_GearVR_LThumbstickX", axis = 0, });
219 BindAxis(new Axis() { name = "Oculus_GearVR_LThumbstickY", axis = 1, invert = true });
220 BindAxis(new Axis() { name = "Oculus_GearVR_RThumbstickX", axis = 2, });
221 BindAxis(new Axis() { name = "Oculus_GearVR_RThumbstickY", axis = 3, invert = true });
222 BindAxis(new Axis() { name = "Oculus_GearVR_DpadX", axis = 4, });
223 BindAxis(new Axis() { name = "Oculus_GearVR_DpadY", axis = 5, invert = true });
224 BindAxis(new Axis() { name = "Oculus_GearVR_LIndexTrigger", axis = 12, });
225 BindAxis(new Axis() { name = "Oculus_GearVR_RIndexTrigger", axis = 11, });
226 BindAxis(new Axis() { name = "Oculus_CrossPlatform_Button2", positiveButton = "joystick button 0", gravity = 1000f, sensitivity = 1000f, type = 0 });
227 BindAxis(new Axis() { name = "Oculus_CrossPlatform_Button4", positiveButton = "joystick button 2", gravity = 1000f, sensitivity = 1000f, type = 0 });
228 BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryThumbstick", positiveButton = "joystick button 8", gravity = 0f, dead = 0f, sensitivity = 0.1f, type = 0 });
229 BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryThumbstick", positiveButton = "joystick button 9", gravity = 0f, dead = 0f, sensitivity = 0.1f, type = 0 });
230 BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryIndexTrigger", dead = 0.19f, type = 2, axis = 8, joyNum = 0 });
231 BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryIndexTrigger", dead = 0.19f, type = 2, axis = 9, joyNum = 0 });
232 BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryHandTrigger", dead = 0.19f, type = 2, axis = 10, joyNum = 0 });
233 BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryHandTrigger", dead = 0.19f, type = 2, axis = 11, joyNum = 0 });
234 BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryThumbstickHorizontal", dead = 0.19f, type = 2, axis = 0, joyNum = 0 });
235 BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryThumbstickVertical", dead = 0.19f, type = 2, axis = 1, joyNum = 0, invert = true });
236 BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryThumbstickHorizontal", dead = 0.19f, type = 2, axis = 3, joyNum = 0 });
237 BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryThumbstickVertical", dead = 0.19f, type = 2, axis = 4, joyNum = 0, invert = true });
238 }
239 catch
240 {
241 Debug.LogError("Failed to apply Oculus GearVR input manager bindings.");
242 }
243 }
244
245 private static void EnforceOSIG()
246 {
247 // Don't bug the user in play mode.
248 if (Application.isPlaying)
249 return;
250
251 // Don't warn if the project may be set up for submission or global signing.
252 if (File.Exists(androidManifestPath))
253 return;
254
255 bool foundPossibleOsig = false;
256 if (Directory.Exists(androidAssetsPath))
257 {
258 var files = Directory.GetFiles(androidAssetsPath);
259 for (int i = 0; i < files.Length; ++i)
260 {
261 if (!files[i].Contains(".txt"))
262 {
263 foundPossibleOsig = true;
264 break;
265 }
266 }
267 }
268
269 if (!foundPossibleOsig)
270 Debug.LogWarning("Missing Gear VR OSIG at " + androidAssetsPath + ". Please see https://dashboard.oculus.com/tools/osig-generator");
271 }
272
273 private class Axis
274 {
275 public string name = String.Empty;
276 public string descriptiveName = String.Empty;
277 public string descriptiveNegativeName = String.Empty;
278 public string negativeButton = String.Empty;
279 public string positiveButton = String.Empty;
280 public string altNegativeButton = String.Empty;
281 public string altPositiveButton = String.Empty;
282 public float gravity = 0.0f;
283 public float dead = 0.001f;
284 public float sensitivity = 1.0f;
285 public bool snap = false;
286 public bool invert = false;
287 public int type = 2;
288 public int axis = 0;
289 public int joyNum = 0;
290 }
291
292 private static void BindAxis(Axis axis)
293 {
294 SerializedObject serializedObject = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]);
295 SerializedProperty axesProperty = serializedObject.FindProperty("m_Axes");
296
297 SerializedProperty axisIter = axesProperty.Copy();
298 axisIter.Next(true);
299 axisIter.Next(true);
300 while (axisIter.Next(false))
301 {
302 if (axisIter.FindPropertyRelative("m_Name").stringValue == axis.name)
303 {
304 // Axis already exists. Don't create binding.
305 return;
306 }
307 }
308
309 axesProperty.arraySize++;
310 serializedObject.ApplyModifiedProperties();
311
312 SerializedProperty axisProperty = axesProperty.GetArrayElementAtIndex(axesProperty.arraySize - 1);
313 axisProperty.FindPropertyRelative("m_Name").stringValue = axis.name;
314 axisProperty.FindPropertyRelative("descriptiveName").stringValue = axis.descriptiveName;
315 axisProperty.FindPropertyRelative("descriptiveNegativeName").stringValue = axis.descriptiveNegativeName;
316 axisProperty.FindPropertyRelative("negativeButton").stringValue = axis.negativeButton;
317 axisProperty.FindPropertyRelative("positiveButton").stringValue = axis.positiveButton;
318 axisProperty.FindPropertyRelative("altNegativeButton").stringValue = axis.altNegativeButton;
319 axisProperty.FindPropertyRelative("altPositiveButton").stringValue = axis.altPositiveButton;
320 axisProperty.FindPropertyRelative("gravity").floatValue = axis.gravity;
321 axisProperty.FindPropertyRelative("dead").floatValue = axis.dead;
322 axisProperty.FindPropertyRelative("sensitivity").floatValue = axis.sensitivity;
323 axisProperty.FindPropertyRelative("snap").boolValue = axis.snap;
324 axisProperty.FindPropertyRelative("invert").boolValue = axis.invert;
325 axisProperty.FindPropertyRelative("type").intValue = axis.type;
326 axisProperty.FindPropertyRelative("axis").intValue = axis.axis;
327 axisProperty.FindPropertyRelative("joyNum").intValue = axis.joyNum;
328 serializedObject.ApplyModifiedProperties();
329 }
330}
331