OVRPlatformMenu.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. using UnityEngine;
  14. using VR = UnityEngine.VR;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. /// <summary>
  18. /// Shows the Oculus plaform UI.
  19. /// </summary>
  20. public class OVRPlatformMenu : MonoBehaviour
  21. {
  22. /// <summary>
  23. /// The key code.
  24. /// </summary>
  25. private OVRInput.RawButton inputCode = OVRInput.RawButton.Back;
  26. public enum eHandler
  27. {
  28. ShowConfirmQuit,
  29. RetreatOneLevel,
  30. };
  31. public eHandler shortPressHandler = eHandler.ShowConfirmQuit;
  32. /// <summary>
  33. /// Callback to handle short press. Returns true if ConfirmQuit menu should be shown.
  34. /// </summary>
  35. public System.Func<bool> OnShortPress;
  36. private static Stack<string> sceneStack = new Stack<string>();
  37. enum eBackButtonAction
  38. {
  39. NONE,
  40. SHORT_PRESS
  41. };
  42. eBackButtonAction HandleBackButtonState()
  43. {
  44. eBackButtonAction action = eBackButtonAction.NONE;
  45. if (OVRInput.GetDown(inputCode))
  46. {
  47. action = eBackButtonAction.SHORT_PRESS;
  48. }
  49. return action;
  50. }
  51. /// <summary>
  52. /// Instantiate the cursor timer
  53. /// </summary>
  54. void Awake()
  55. {
  56. if (shortPressHandler == eHandler.RetreatOneLevel && OnShortPress == null)
  57. OnShortPress = RetreatOneLevel;
  58. if (!OVRManager.isHmdPresent)
  59. {
  60. enabled = false;
  61. return;
  62. }
  63. sceneStack.Push(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
  64. }
  65. /// <summary>
  66. /// Show the confirm quit menu
  67. /// </summary>
  68. void ShowConfirmQuitMenu()
  69. {
  70. #if UNITY_ANDROID && !UNITY_EDITOR
  71. Debug.Log("[PlatformUI-ConfirmQuit] Showing @ " + Time.time);
  72. OVRManager.PlatformUIConfirmQuit();
  73. #endif
  74. }
  75. /// <summary>
  76. /// Sample handler for short press which retreats to the previous scene that used OVRPlatformMenu.
  77. /// </summary>
  78. private static bool RetreatOneLevel()
  79. {
  80. if (sceneStack.Count > 1)
  81. {
  82. string parentScene = sceneStack.Pop();
  83. UnityEngine.SceneManagement.SceneManager.LoadSceneAsync (parentScene);
  84. return false;
  85. }
  86. return true;
  87. }
  88. /// <summary>
  89. /// Tests for long-press and activates global platform menu when detected.
  90. /// as per the Unity integration doc, the back button responds to "mouse 1" button down/up/etc
  91. /// </summary>
  92. void Update()
  93. {
  94. #if UNITY_ANDROID
  95. eBackButtonAction action = HandleBackButtonState();
  96. if (action == eBackButtonAction.SHORT_PRESS)
  97. {
  98. if (OnShortPress == null || OnShortPress())
  99. {
  100. ShowConfirmQuitMenu();
  101. }
  102. }
  103. #endif
  104. }
  105. }