2
0

SteamVR_Settings.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEngine.Serialization;
  7. namespace Valve.VR
  8. {
  9. public class SteamVR_Settings : ScriptableObject
  10. {
  11. private static SteamVR_Settings _instance;
  12. public static SteamVR_Settings instance
  13. {
  14. get
  15. {
  16. LoadInstance();
  17. return _instance;
  18. }
  19. }
  20. public bool pauseGameWhenDashboardVisible = true;
  21. public bool lockPhysicsUpdateRateToRenderFrequency = true;
  22. public ETrackingUniverseOrigin trackingSpace
  23. {
  24. get
  25. {
  26. return trackingSpaceOrigin;
  27. }
  28. set
  29. {
  30. trackingSpaceOrigin = value;
  31. if (SteamVR_Behaviour.isPlaying)
  32. SteamVR_Action_Pose.SetTrackingUniverseOrigin(trackingSpaceOrigin);
  33. }
  34. }
  35. [SerializeField]
  36. [FormerlySerializedAsAttribute("trackingSpace")]
  37. private ETrackingUniverseOrigin trackingSpaceOrigin = ETrackingUniverseOrigin.TrackingUniverseStanding;
  38. [Tooltip("Filename local to StreamingAssets/SteamVR/ folder")]
  39. public string actionsFilePath = "actions.json";
  40. [Tooltip("Path local to the directory the SteamVR folder as in")]
  41. public string steamVRInputPath = "SteamVR_Input";
  42. public SteamVR_UpdateModes inputUpdateMode = SteamVR_UpdateModes.OnUpdate;
  43. public SteamVR_UpdateModes poseUpdateMode = SteamVR_UpdateModes.OnPreCull;
  44. public bool activateFirstActionSetOnStart = true;
  45. [Tooltip("This is the app key the unity editor will use to identify your application. (can be \"steam.app.[appid]\" to persist bindings between editor steam)")]
  46. public string editorAppKey;
  47. [Tooltip("The SteamVR Plugin can automatically make sure VR is enabled in your player settings and if not, enable it.")]
  48. public bool autoEnableVR = true;
  49. [Space()]
  50. [Tooltip("This determines if we use legacy mixed reality mode (3rd controller/tracker device connected) or the new input system mode (pose / input source)")]
  51. public bool legacyMixedRealityCamera = true;
  52. [Tooltip("[NON-LEGACY] This is the pose action that will be used for positioning a mixed reality camera if connected")]
  53. public SteamVR_Action_Pose mixedRealityCameraPose = SteamVR_Input.GetPoseAction("ExternalCamera");
  54. [Tooltip("[NON-LEGACY] This is the input source to check on the pose for the mixed reality camera")]
  55. public SteamVR_Input_Sources mixedRealityCameraInputSource = SteamVR_Input_Sources.Camera;
  56. [Tooltip("[NON-LEGACY] Auto enable mixed reality action set if file exists")]
  57. public bool mixedRealityActionSetAutoEnable = true;
  58. [Tooltip("[EDITOR ONLY] The (left) prefab to be used for showing previews while posing hands")]
  59. public GameObject previewHandLeft;
  60. [Tooltip("[EDITOR ONLY] The (right) prefab to be used for showing previews while posing hands")]
  61. public GameObject previewHandRight;
  62. private const string previewLeftDefaultAssetName = "vr_glove_left_model_slim";
  63. private const string previewRightDefaultAssetName = "vr_glove_right_model_slim";
  64. public bool IsInputUpdateMode(SteamVR_UpdateModes tocheck)
  65. {
  66. return (inputUpdateMode & tocheck) == tocheck;
  67. }
  68. public bool IsPoseUpdateMode(SteamVR_UpdateModes tocheck)
  69. {
  70. return (poseUpdateMode & tocheck) == tocheck;
  71. }
  72. public static void VerifyScriptableObject()
  73. {
  74. LoadInstance();
  75. }
  76. private static void LoadInstance()
  77. {
  78. if (_instance == null)
  79. {
  80. _instance = Resources.Load<SteamVR_Settings>("SteamVR_Settings");
  81. if (_instance == null)
  82. {
  83. _instance = SteamVR_Settings.CreateInstance<SteamVR_Settings>();
  84. #if UNITY_EDITOR
  85. string localFolderPath = SteamVR.GetSteamVRResourcesFolderPath(true);
  86. string assetPath = System.IO.Path.Combine(localFolderPath, "SteamVR_Settings.asset");
  87. UnityEditor.AssetDatabase.CreateAsset(_instance, assetPath);
  88. UnityEditor.AssetDatabase.SaveAssets();
  89. #endif
  90. }
  91. SetDefaultsIfNeeded();
  92. }
  93. }
  94. public static void Save()
  95. {
  96. #if UNITY_EDITOR
  97. UnityEditor.EditorUtility.SetDirty(instance);
  98. UnityEditor.AssetDatabase.SaveAssets();
  99. #endif
  100. }
  101. private const string defaultSettingsAssetName = "SteamVR_Settings";
  102. private static void SetDefaultsIfNeeded()
  103. {
  104. if (string.IsNullOrEmpty(_instance.editorAppKey))
  105. {
  106. _instance.editorAppKey = SteamVR.GenerateAppKey();
  107. Debug.Log("<b>[SteamVR Setup]</b> Generated you an editor app key of: " + _instance.editorAppKey + ". This lets the editor tell SteamVR what project this is. Has no effect on builds. This can be changed in Assets/SteamVR/Resources/SteamVR_Settings");
  108. #if UNITY_EDITOR
  109. UnityEditor.EditorUtility.SetDirty(_instance);
  110. UnityEditor.AssetDatabase.SaveAssets();
  111. #endif
  112. }
  113. #if UNITY_EDITOR
  114. if (_instance.previewHandLeft == null)
  115. _instance.previewHandLeft = FindDefaultPreviewHand(previewLeftDefaultAssetName);
  116. if (_instance.previewHandRight == null)
  117. _instance.previewHandRight = FindDefaultPreviewHand(previewRightDefaultAssetName);
  118. #endif
  119. #if OPENVR_XR_API
  120. Unity.XR.OpenVR.OpenVRSettings settings = Unity.XR.OpenVR.OpenVRSettings.GetSettings();
  121. settings.ActionManifestFileRelativeFilePath = SteamVR_Input.GetActionsFilePath();
  122. #if UNITY_EDITOR
  123. settings.EditorAppKey = _instance.editorAppKey;
  124. #endif
  125. #endif
  126. }
  127. private static GameObject FindDefaultPreviewHand(string assetName)
  128. {
  129. #if UNITY_EDITOR
  130. string[] defaultPaths = UnityEditor.AssetDatabase.FindAssets(string.Format("t:Prefab {0}", assetName));
  131. if (defaultPaths != null && defaultPaths.Length > 0)
  132. {
  133. string defaultGUID = defaultPaths[0];
  134. string defaultPath = UnityEditor.AssetDatabase.GUIDToAssetPath(defaultGUID);
  135. GameObject defaultAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(defaultPath);
  136. if (defaultAsset == null)
  137. Debug.LogError("[SteamVR] Could not load default hand preview prefab: " + assetName + ". Found path: " + defaultPath);
  138. return defaultAsset;
  139. }
  140. //else //todo: this will generally fail on the first try but will try again before its an issue.
  141. //Debug.LogError("[SteamVR] Could not load default hand preview prefab: " + assetName);
  142. #endif
  143. return null;
  144. }
  145. }
  146. }