Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
12 KiB

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