OpenVRSettingsEditor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using Unity.XR.OpenVR;
  6. namespace Unity.XR.OpenVR.Editor
  7. {
  8. [CustomEditor(typeof(OpenVRSettings))]
  9. public class OpenVRSettingsEditor : UnityEditor.Editor
  10. {
  11. private const string kStereoRenderingMode = "StereoRenderingMode";
  12. static GUIContent s_StereoRenderingMode = EditorGUIUtility.TrTextContent("Stereo Rendering Mode");
  13. private SerializedProperty m_StereoRenderingMode;
  14. private const string kInitializationType = "InitializationType";
  15. static GUIContent s_InitializationType = EditorGUIUtility.TrTextContent("Application Type");
  16. private SerializedProperty m_InitializationType;
  17. private const string kMirrorViewModeKey = "MirrorView";
  18. static GUIContent s_MirrorViewMode = EditorGUIUtility.TrTextContent("Mirror View Mode");
  19. private SerializedProperty m_MirrorViewMode;
  20. public GUIContent WindowsTab;
  21. private int tab = 0;
  22. public void OnEnable()
  23. {
  24. WindowsTab = new GUIContent("", EditorGUIUtility.IconContent("BuildSettings.Standalone.Small").image);
  25. }
  26. public static void CloseWindowHelper()
  27. {
  28. var window = SettingsService.OpenProjectSettings("Project/XR Plug-in Management");
  29. if (window)
  30. {
  31. Debug.LogWarning("<b>[Community OpenXR]</b> Switching away from OpenVR Project settings window to avoid data corruption.");
  32. }
  33. EditorApplication.update -= CloseWindowHelper;
  34. closing = false;
  35. }
  36. private static bool closing = false;
  37. public override void OnInspectorGUI()
  38. {
  39. if (Application.isPlaying)
  40. {
  41. //need to close this window if we're in play mode. There's a bug that resets settings otherwise.
  42. EditorGUILayout.LabelField("Unity XR settings are unavailable while in play mode.");
  43. if (!closing)
  44. {
  45. closing = true;
  46. EditorApplication.update += CloseWindowHelper;
  47. }
  48. return;
  49. }
  50. if (serializedObject == null || serializedObject.targetObject == null)
  51. return;
  52. if (m_StereoRenderingMode == null)
  53. {
  54. m_StereoRenderingMode = serializedObject.FindProperty(kStereoRenderingMode);
  55. }
  56. if (m_InitializationType == null)
  57. {
  58. m_InitializationType = serializedObject.FindProperty(kInitializationType);
  59. }
  60. if (m_MirrorViewMode == null)
  61. {
  62. m_MirrorViewMode = serializedObject.FindProperty(kMirrorViewModeKey);
  63. }
  64. serializedObject.Update();
  65. int currentMode = m_MirrorViewMode.intValue;
  66. if (m_MirrorViewMode != null)
  67. tab = GUILayout.Toolbar(tab, new GUIContent[] {WindowsTab},EditorStyles.toolbarButton);
  68. EditorGUILayout.Space();
  69. EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true));
  70. if (tab == 0)
  71. {
  72. EditorGUILayout.PropertyField(m_InitializationType, s_InitializationType);
  73. EditorGUILayout.PropertyField(m_StereoRenderingMode, s_StereoRenderingMode);
  74. EditorGUILayout.PropertyField(m_MirrorViewMode, s_MirrorViewMode);
  75. }
  76. EditorGUILayout.EndVertical();
  77. serializedObject.ApplyModifiedProperties();
  78. /*
  79. //can't have the settings window open during play mode
  80. int newMode = m_MirrorViewMode.intValue;
  81. if (currentMode != newMode && Application.isPlaying)
  82. {
  83. OpenVRSettings.SetMirrorViewMode((ushort)newMode);
  84. }*/
  85. }
  86. }
  87. }