OVRPlatformMenu.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /************************************************************************************
  2. Copyright : Copyright 2017 Oculus VR, LLC. All Rights reserved.
  3. Licensed under the Oculus VR Rift SDK License Version 3.4.1 (the "License");
  4. you may not use the Oculus VR Rift 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 VR 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. using UnityEngine;
  16. using VR = UnityEngine.VR;
  17. using System.Collections;
  18. using System.Collections.Generic;
  19. /// <summary>
  20. /// Shows the Oculus plaform UI.
  21. /// </summary>
  22. public class OVRPlatformMenu : MonoBehaviour
  23. {
  24. /// <summary>
  25. /// The key code.
  26. /// </summary>
  27. private OVRInput.RawButton inputCode = OVRInput.RawButton.Back;
  28. public enum eHandler
  29. {
  30. ShowConfirmQuit,
  31. RetreatOneLevel,
  32. };
  33. public eHandler shortPressHandler = eHandler.ShowConfirmQuit;
  34. /// <summary>
  35. /// Callback to handle short press. Returns true if ConfirmQuit menu should be shown.
  36. /// </summary>
  37. public System.Func<bool> OnShortPress;
  38. private static Stack<string> sceneStack = new Stack<string>();
  39. enum eBackButtonAction
  40. {
  41. NONE,
  42. SHORT_PRESS
  43. };
  44. eBackButtonAction HandleBackButtonState()
  45. {
  46. eBackButtonAction action = eBackButtonAction.NONE;
  47. if (OVRInput.GetDown(inputCode))
  48. {
  49. action = eBackButtonAction.SHORT_PRESS;
  50. }
  51. return action;
  52. }
  53. /// <summary>
  54. /// Instantiate the cursor timer
  55. /// </summary>
  56. void Awake()
  57. {
  58. if (shortPressHandler == eHandler.RetreatOneLevel && OnShortPress == null)
  59. OnShortPress = RetreatOneLevel;
  60. if (!OVRManager.isHmdPresent)
  61. {
  62. enabled = false;
  63. return;
  64. }
  65. sceneStack.Push(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
  66. }
  67. /// <summary>
  68. /// Show the confirm quit menu
  69. /// </summary>
  70. void ShowConfirmQuitMenu()
  71. {
  72. #if UNITY_ANDROID && !UNITY_EDITOR
  73. Debug.Log("[PlatformUI-ConfirmQuit] Showing @ " + Time.time);
  74. OVRManager.PlatformUIConfirmQuit();
  75. #endif
  76. }
  77. /// <summary>
  78. /// Sample handler for short press which retreats to the previous scene that used OVRPlatformMenu.
  79. /// </summary>
  80. private static bool RetreatOneLevel()
  81. {
  82. if (sceneStack.Count > 1)
  83. {
  84. string parentScene = sceneStack.Pop();
  85. UnityEngine.SceneManagement.SceneManager.LoadSceneAsync (parentScene);
  86. return false;
  87. }
  88. return true;
  89. }
  90. /// <summary>
  91. /// Tests for long-press and activates global platform menu when detected.
  92. /// as per the Unity integration doc, the back button responds to "mouse 1" button down/up/etc
  93. /// </summary>
  94. void Update()
  95. {
  96. #if UNITY_ANDROID
  97. eBackButtonAction action = HandleBackButtonState();
  98. if (action == eBackButtonAction.SHORT_PRESS)
  99. {
  100. if (OnShortPress == null || OnShortPress())
  101. {
  102. ShowConfirmQuitMenu();
  103. }
  104. }
  105. #endif
  106. }
  107. }