-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSteamVR_ControllerManager.cs
248 lines (216 loc) · 6.71 KB
/
SteamVR_ControllerManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//========= Copyright 2016, Valve Corporation, All rights reserved. ===========
//
// Purpose: Enables/disables objects based on connectivity and assigned roles.
//
//=============================================================================
using UnityEngine;
using System.Collections.Generic;
using Valve.VR;
public class SteamVR_ControllerManager : MonoBehaviour
{
public GameObject left, right;
public GameObject[] objects; // populate with objects you want to assign to additional controllers
uint[] indices; // assigned
bool[] connected = new bool[OpenVR.k_unMaxTrackedDeviceCount]; // controllers only
// cached roles - may or may not be connected
uint leftIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
uint rightIndex = OpenVR.k_unTrackedDeviceIndexInvalid;
void Awake()
{
// Add left and right entries to the head of the list so we only have to operate on the list itself.
var additional = (this.objects != null) ? this.objects.Length : 0;
var objects = new GameObject[2 + additional];
indices = new uint[2 + additional];
objects[0] = right;
indices[0] = OpenVR.k_unTrackedDeviceIndexInvalid;
objects[1] = left;
indices[1] = OpenVR.k_unTrackedDeviceIndexInvalid;
for (int i = 0; i < additional; i++)
{
objects[2 + i] = this.objects[i];
indices[2 + i] = OpenVR.k_unTrackedDeviceIndexInvalid;
}
this.objects = objects;
}
void OnEnable()
{
for (int i = 0; i < objects.Length; i++)
{
var obj = objects[i];
if (obj != null) {
obj.SetActive (false);
print (obj); //both
}
}
OnTrackedDeviceRoleChanged();
for (int i = 0; i < SteamVR.connected.Length; i++)
if (SteamVR.connected [i]) {
OnDeviceConnected (i, true);
}
SteamVR_Utils.Event.Listen("input_focus", OnInputFocus);
SteamVR_Utils.Event.Listen("device_connected", OnDeviceConnected);
SteamVR_Utils.Event.Listen("TrackedDeviceRoleChanged", OnTrackedDeviceRoleChanged);
}
void OnDisable()
{
SteamVR_Utils.Event.Remove("input_focus", OnInputFocus);
SteamVR_Utils.Event.Remove("device_connected", OnDeviceConnected);
SteamVR_Utils.Event.Remove("TrackedDeviceRoleChanged", OnTrackedDeviceRoleChanged);
}
static string[] labels = { "left", "right" };
// Hide controllers when the dashboard is up.
private void OnInputFocus(params object[] args)
{
bool hasFocus = (bool)args[0];
if (hasFocus)
{
for (int i = 0; i < objects.Length; i++)
{
var obj = objects[i];
if (obj != null)
{
var label = (i < 2) ? labels[i] : (i - 1).ToString();
ShowObject(obj.transform, "hidden (" + label + ")");
}
}
}
else
{
for (int i = 0; i < objects.Length; i++)
{
var obj = objects[i];
if (obj != null)
{
var label = (i < 2) ? labels[i] : (i - 1).ToString();
HideObject(obj.transform, "hidden (" + label + ")");
}
}
}
}
// Reparents to a new object and deactivates that object (this allows
// us to call SetActive in OnDeviceConnected independently.
private void HideObject(Transform t, string name)
{
var hidden = new GameObject(name).transform;
hidden.parent = t.parent;
t.parent = hidden;
hidden.gameObject.SetActive(false);
print ("hide " + name);
}
private void ShowObject(Transform t, string name)
{
var hidden = t.parent;
if (hidden.gameObject.name != name)
return;
t.parent = hidden.parent;
Destroy(hidden.gameObject);
}
private void SetTrackedDeviceIndex(int objectIndex, uint trackedDeviceIndex)
{
// First make sure no one else is already using this index.
if (trackedDeviceIndex != OpenVR.k_unTrackedDeviceIndexInvalid)
{
for (int i = 0; i < objects.Length; i++)
{
if (i != objectIndex && indices[i] == trackedDeviceIndex)
{
var obj = objects[i];
if (obj != null) {
obj.SetActive (false);
}
indices[i] = OpenVR.k_unTrackedDeviceIndexInvalid;
}
}
}
// Only set when changed.
if (trackedDeviceIndex != indices[objectIndex])
{
indices[objectIndex] = trackedDeviceIndex;
var obj = objects[objectIndex];
if (obj != null)
{
if (trackedDeviceIndex == OpenVR.k_unTrackedDeviceIndexInvalid)
obj.SetActive(false);
else
{
obj.SetActive(true);
obj.BroadcastMessage("SetDeviceIndex", (int)trackedDeviceIndex, SendMessageOptions.DontRequireReceiver);
}
}
}
}
// Keep track of assigned roles.
private void OnTrackedDeviceRoleChanged(params object[] args)
{
Refresh();
}
// Keep track of connected controller indices.
private void OnDeviceConnected(params object[] args)
{
var index = (uint)(int)args[0];
bool changed = this.connected[index];
this.connected[index] = false;
var connected = (bool)args[1];
//check connect 1~4
// print ("OnDeviceConnected "+index+" value:"+connected);
if (connected)
{
var system = OpenVR.System;
if (system != null && system.GetTrackedDeviceClass(index) == ETrackedDeviceClass.Controller)
{
this.connected[index] = true;
changed = !changed; // if we clear and set the same index, nothing has changed
}
}
if (changed)
Refresh();
}
public void Refresh()
{
int objectIndex = 0;
var system = OpenVR.System;
if (system != null)
{
leftIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
rightIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
}
// If neither role has been assigned yet, try hooking up at least the right controller.
if (leftIndex == OpenVR.k_unTrackedDeviceIndexInvalid && rightIndex == OpenVR.k_unTrackedDeviceIndexInvalid)
{
for (uint deviceIndex = 0; deviceIndex < connected.Length; deviceIndex++)
{
if (connected[deviceIndex])
{
SetTrackedDeviceIndex(objectIndex++, deviceIndex);
print ("deviceIndex " + deviceIndex);
continue;
}
}
}
else
{
SetTrackedDeviceIndex(objectIndex++, (rightIndex < connected.Length && connected[rightIndex]) ? rightIndex : OpenVR.k_unTrackedDeviceIndexInvalid);
SetTrackedDeviceIndex(objectIndex++, (leftIndex < connected.Length && connected[leftIndex]) ? leftIndex : OpenVR.k_unTrackedDeviceIndexInvalid);
// Assign out any additional controllers only after both left and right have been assigned.
if (leftIndex != OpenVR.k_unTrackedDeviceIndexInvalid && rightIndex != OpenVR.k_unTrackedDeviceIndexInvalid)
{
for (uint deviceIndex = 0; deviceIndex < connected.Length; deviceIndex++)
{
if (objectIndex >= objects.Length)
break;
if (!connected[deviceIndex])
continue;
if (deviceIndex != leftIndex && deviceIndex != rightIndex)
{
SetTrackedDeviceIndex(objectIndex++, deviceIndex);
}
}
}
}
// Reset the rest.
while (objectIndex < objects.Length)
{
SetTrackedDeviceIndex(objectIndex++, OpenVR.k_unTrackedDeviceIndexInvalid);
}
}
}