OpenVRSettings.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. #if UNITY_XR_MANAGEMENT
  6. using UnityEngine.XR.Management;
  7. #endif
  8. namespace Unity.XR.OpenVR
  9. {
  10. #if UNITY_XR_MANAGEMENT
  11. [XRConfigurationData("OpenVR", "Unity.XR.OpenVR.Settings")]
  12. #endif
  13. [System.Serializable]
  14. public class OpenVRSettings : ScriptableObject
  15. {
  16. public enum StereoRenderingModes
  17. {
  18. MultiPass = 0,
  19. SinglePassInstanced
  20. }
  21. public enum InitializationTypes
  22. {
  23. Scene = 1,
  24. Overlay = 2,
  25. }
  26. public enum MirrorViewModes
  27. {
  28. None = 0,
  29. Left,
  30. Right,
  31. OpenVR,
  32. }
  33. [SerializeField, Tooltip("This will check the package version and the latest on github and prompt you to upgrade once per project load.")]
  34. public bool PromptToUpgradePackage = true;
  35. [SerializeField, Tooltip("This will check the package version and the latest on github and prompt you to upgrade once per project load.")]
  36. public bool PromptToUpgradePreviewPackages = true;
  37. [SerializeField, Tooltip("This allows developers to skip upgrade prompts for just this version.")]
  38. public string SkipPromptForVersion = null;
  39. [SerializeField, Tooltip("Set the Stereo Rendering Method")]
  40. public StereoRenderingModes StereoRenderingMode = StereoRenderingModes.SinglePassInstanced;
  41. [SerializeField, Tooltip("Most applications initialize as type Scene")]
  42. public InitializationTypes InitializationType = InitializationTypes.Scene;
  43. [SerializeField, Tooltip("A generated unique identifier for your application while in the editor")]
  44. public string EditorAppKey = null;
  45. [SerializeField, Tooltip("Internal value that tells the system what the relative path is to the manifest")]
  46. public string ActionManifestFileRelativeFilePath;
  47. [SerializeField, Tooltip("Which eye to use when rendering the headset view to the main window (none, left, right, or a composite of both + OpenVR overlays)")]
  48. public MirrorViewModes MirrorView = MirrorViewModes.Right;
  49. public const string StreamingAssetsFolderName = "SteamVR";
  50. public const string ActionManifestFileName = "legacy_manifest.json";
  51. public static string GetStreamingSteamVRPath(bool create = true)
  52. {
  53. string path = System.IO.Path.Combine(Application.streamingAssetsPath, StreamingAssetsFolderName);
  54. if (create)
  55. {
  56. CreateDirectory(new DirectoryInfo(path));
  57. }
  58. return path;
  59. }
  60. private static void CreateDirectory(DirectoryInfo directory)
  61. {
  62. if (directory.Parent.Exists == false)
  63. CreateDirectory(directory.Parent);
  64. if (directory.Exists == false)
  65. directory.Create();
  66. }
  67. [SerializeField, Tooltip("Internal value that tells the system if we have copied the default binding files yet.")]
  68. public bool HasCopiedDefaults = false;
  69. public ushort GetStereoRenderingMode()
  70. {
  71. return (ushort)StereoRenderingMode;
  72. }
  73. public ushort GetInitializationType()
  74. {
  75. return (ushort)InitializationType;
  76. }
  77. public MirrorViewModes GetMirrorViewMode()
  78. {
  79. return MirrorView;
  80. }
  81. /// <summary>
  82. /// Sets the mirror view mode (left, right, composite of both + openvr overlays) at runtime.
  83. /// </summary>
  84. /// <param name="newMode">left, right, composite of both + openvr overlays</param>
  85. public void SetMirrorViewMode(MirrorViewModes newMode)
  86. {
  87. MirrorView = newMode;
  88. SetMirrorViewMode((ushort)newMode);
  89. }
  90. public string GenerateEditorAppKey()
  91. {
  92. return string.Format("application.generated.unity.{0}.{1}.exe", CleanProductName(), ((int)(UnityEngine.Random.value * int.MaxValue)).ToString());
  93. }
  94. private static string CleanProductName()
  95. {
  96. string productName = Application.productName;
  97. if (string.IsNullOrEmpty(productName))
  98. productName = "unnamed_product";
  99. else
  100. {
  101. productName = System.Text.RegularExpressions.Regex.Replace(Application.productName, "[^\\w\\._]", "");
  102. productName = productName.ToLower();
  103. }
  104. return productName;
  105. }
  106. public static OpenVRSettings GetSettings(bool create = true)
  107. {
  108. OpenVRSettings settings = null;
  109. #if UNITY_EDITOR
  110. UnityEditor.EditorBuildSettings.TryGetConfigObject<OpenVRSettings>("Unity.XR.OpenVR.Settings", out settings);
  111. #else
  112. settings = OpenVRSettings.s_Settings;
  113. #endif
  114. if (settings == null && create)
  115. settings = OpenVRSettings.CreateInstance<OpenVRSettings>();
  116. return settings;
  117. }
  118. [DllImport("XRSDKOpenVR", CharSet = CharSet.Auto)]
  119. public static extern void SetMirrorViewMode(ushort mirrorViewMode);
  120. public bool InitializeActionManifestFileRelativeFilePath()
  121. {
  122. string oldPath = ActionManifestFileRelativeFilePath;
  123. string newPath;
  124. if (OpenVRHelpers.IsUsingSteamVRInput())
  125. {
  126. newPath = System.IO.Path.Combine(OpenVRSettings.GetStreamingSteamVRPath(false), OpenVRHelpers.GetActionManifestNameFromPlugin());
  127. string fullpath = System.IO.Path.GetFullPath(".");
  128. newPath = newPath.Remove(0, fullpath.Length + 1);
  129. if (newPath.StartsWith("Assets"))
  130. newPath = newPath.Remove(0, "Assets".Length + 1);
  131. }
  132. else
  133. {
  134. newPath = null;
  135. }
  136. #if UNITY_EDITOR
  137. if (newPath != oldPath)
  138. {
  139. ActionManifestFileRelativeFilePath = newPath;
  140. UnityEditor.EditorUtility.SetDirty(this);
  141. UnityEditor.AssetDatabase.SaveAssets();
  142. return true;
  143. }
  144. #endif
  145. return false;
  146. }
  147. #if UNITY_EDITOR
  148. public void Awake()
  149. {
  150. if (string.IsNullOrEmpty(this.EditorAppKey))
  151. {
  152. this.EditorAppKey = this.GenerateEditorAppKey();
  153. }
  154. this.InitializeActionManifestFileRelativeFilePath();
  155. }
  156. #else
  157. public static OpenVRSettings s_Settings;
  158. public void Awake()
  159. {
  160. s_Settings = this;
  161. }
  162. #endif
  163. }
  164. }