BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / OVRPlatformMenu.cs

OVRPlatformMenu.cs

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