BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Util / OVRControllerHelper.cs

OVRControllerHelper.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRControllerHelper.cs 6.3 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 System.Collections;
19
20/// <summary>
21/// Simple helper script that conditionally enables rendering of a controller if it is connected.
22/// </summary>
23public class OVRControllerHelper : MonoBehaviour
24{
25 /// <summary>
26 /// The root GameObject that represents the GearVr Controller model.
27 /// </summary>
28 public GameObject m_modelGearVrController;
29
30 /// <summary>
31 /// The root GameObject that represents the Oculus Go Controller model.
32 /// </summary>
33 public GameObject m_modelOculusGoController;
34
35 /// <summary>
36 /// The root GameObject that represents the Oculus Touch for Quest And RiftS Controller model (Left).
37 /// </summary>
38 public GameObject m_modelOculusTouchQuestAndRiftSLeftController;
39
40 /// <summary>
41 /// The root GameObject that represents the Oculus Touch for Quest And RiftS Controller model (Right).
42 /// </summary>
43 public GameObject m_modelOculusTouchQuestAndRiftSRightController;
44
45 /// <summary>
46 /// The root GameObject that represents the Oculus Touch for Rift Controller model (Left).
47 /// </summary>
48 public GameObject m_modelOculusTouchRiftLeftController;
49
50 /// <summary>
51 /// The root GameObject that represents the Oculus Touch for Rift Controller model (Right).
52 /// </summary>
53 public GameObject m_modelOculusTouchRiftRightController;
54
55 /// <summary>
56 /// The controller that determines whether or not to enable rendering of the controller model.
57 /// </summary>
58 public OVRInput.Controller m_controller;
59
60 private enum ControllerType
61 {
62 GearVR, Go, QuestAndRiftS, Rift
63 }
64
65 private ControllerType activeControllerType = ControllerType.Rift;
66
67 private bool m_prevControllerConnected = false;
68 private bool m_prevControllerConnectedCached = false;
69
70 void Start()
71 {
72 OVRPlugin.SystemHeadset headset = OVRPlugin.GetSystemHeadsetType();
73 switch (headset)
74 {
75 case OVRPlugin.SystemHeadset.Oculus_Go:
76 activeControllerType = ControllerType.Go;
77 break;
78 case OVRPlugin.SystemHeadset.Oculus_Quest:
79 activeControllerType = ControllerType.QuestAndRiftS;
80 break;
81 case OVRPlugin.SystemHeadset.Rift_CV1:
82 activeControllerType = ControllerType.Rift;
83 break;
84 case OVRPlugin.SystemHeadset.Rift_S:
85 activeControllerType = ControllerType.QuestAndRiftS;
86 break;
87 case OVRPlugin.SystemHeadset.GearVR_R320:
88 case OVRPlugin.SystemHeadset.GearVR_R321:
89 case OVRPlugin.SystemHeadset.GearVR_R322:
90 case OVRPlugin.SystemHeadset.GearVR_R323:
91 case OVRPlugin.SystemHeadset.GearVR_R324:
92 case OVRPlugin.SystemHeadset.GearVR_R325:
93 activeControllerType = ControllerType.GearVR;
94 break;
95 default:
96#if UNITY_EDITOR || !UNITY_ANDROID
97 activeControllerType = ControllerType.Rift;
98#else
99 activeControllerType = ControllerType.GearVR;
100#endif
101 break;
102 }
103
104 Debug.LogFormat("OVRControllerHelp: Active controller type: {0} for product {1}", activeControllerType, OVRPlugin.productName);
105 if ((activeControllerType != ControllerType.GearVR) && (activeControllerType != ControllerType.Go))
106 {
107 if (m_controller == OVRInput.Controller.LTrackedRemote)
108 {
109 m_controller = OVRInput.Controller.LTouch;
110 }
111 else if (m_controller == OVRInput.Controller.RTrackedRemote)
112 {
113 m_controller = OVRInput.Controller.RTouch;
114 }
115 }
116 else
117 {
118 if (m_controller == OVRInput.Controller.LTouch)
119 {
120 m_controller = OVRInput.Controller.LTrackedRemote;
121 }
122 else if (m_controller == OVRInput.Controller.RTouch)
123 {
124 m_controller = OVRInput.Controller.RTrackedRemote;
125 }
126 }
127 }
128
129 void Update()
130 {
131 bool controllerConnected = OVRInput.IsControllerConnected(m_controller);
132
133 if ((controllerConnected != m_prevControllerConnected) || !m_prevControllerConnectedCached)
134 {
135 if (activeControllerType == ControllerType.GearVR || activeControllerType == ControllerType.Go)
136 {
137 m_modelOculusGoController.SetActive(controllerConnected && (activeControllerType == ControllerType.Go));
138 m_modelGearVrController.SetActive(controllerConnected && (activeControllerType != ControllerType.Go));
139 m_modelOculusTouchQuestAndRiftSLeftController.SetActive(false);
140 m_modelOculusTouchQuestAndRiftSRightController.SetActive(false);
141 m_modelOculusTouchRiftLeftController.SetActive(false);
142 m_modelOculusTouchRiftRightController.SetActive(false);
143 }
144 else if (activeControllerType == ControllerType.QuestAndRiftS)
145 {
146 m_modelOculusGoController.SetActive(false);
147 m_modelGearVrController.SetActive(false);
148 m_modelOculusTouchQuestAndRiftSLeftController.SetActive(controllerConnected && (m_controller == OVRInput.Controller.LTouch));
149 m_modelOculusTouchQuestAndRiftSRightController.SetActive(controllerConnected && (m_controller == OVRInput.Controller.RTouch));
150 m_modelOculusTouchRiftLeftController.SetActive(false);
151 m_modelOculusTouchRiftRightController.SetActive(false);
152 }
153 else // if (activeControllerType == ControllerType.Rift)
154 {
155 m_modelOculusGoController.SetActive(false);
156 m_modelGearVrController.SetActive(false);
157 m_modelOculusTouchQuestAndRiftSLeftController.SetActive(false);
158 m_modelOculusTouchQuestAndRiftSRightController.SetActive(false);
159 m_modelOculusTouchRiftLeftController.SetActive(controllerConnected && (m_controller == OVRInput.Controller.LTouch));
160 m_modelOculusTouchRiftRightController.SetActive(controllerConnected && (m_controller == OVRInput.Controller.RTouch));
161 }
162
163 m_prevControllerConnected = controllerConnected;
164 m_prevControllerConnectedCached = true;
165 }
166
167 if (!controllerConnected)
168 {
169 return;
170 }
171 }
172}