@@ -51,7 +51,7 @@ public enum Axis2DTypes
51
51
private float buttonA ;
52
52
private float buttonB ;
53
53
54
- private Dictionary < ButtonTypes , WebXRControllerButton > buttonStates = new Dictionary < ButtonTypes , WebXRControllerButton > ( ) ;
54
+ private WebXRControllerButton [ ] buttons ;
55
55
56
56
private bool controllerActive = false ;
57
57
private bool handActive = false ;
@@ -94,6 +94,56 @@ private void Update()
94
94
}
95
95
#endif
96
96
97
+ private void Awake ( )
98
+ {
99
+ InitButtons ( ) ;
100
+ }
101
+
102
+ private void InitButtons ( )
103
+ {
104
+ buttons = new WebXRControllerButton [ 6 ] ;
105
+ buttons [ ( int ) ButtonTypes . Trigger ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
106
+ buttons [ ( int ) ButtonTypes . Grip ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
107
+ buttons [ ( int ) ButtonTypes . Thumbstick ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
108
+ buttons [ ( int ) ButtonTypes . Touchpad ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
109
+ buttons [ ( int ) ButtonTypes . ButtonA ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
110
+ buttons [ ( int ) ButtonTypes . ButtonB ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
111
+ }
112
+
113
+ private void UpdateAllButtons ( )
114
+ {
115
+ buttons [ ( int ) ButtonTypes . Trigger ] . UpdateState ( trigger == 1 , trigger ) ;
116
+ buttons [ ( int ) ButtonTypes . Grip ] . UpdateState ( squeeze == 1 , squeeze ) ;
117
+ buttons [ ( int ) ButtonTypes . Thumbstick ] . UpdateState ( thumbstick == 1 , thumbstick ) ;
118
+ buttons [ ( int ) ButtonTypes . Touchpad ] . UpdateState ( touchpad == 1 , touchpad ) ;
119
+ buttons [ ( int ) ButtonTypes . ButtonA ] . UpdateState ( buttonA == 1 , buttonA ) ;
120
+ buttons [ ( int ) ButtonTypes . ButtonB ] . UpdateState ( buttonB == 1 , buttonB ) ;
121
+ }
122
+
123
+ private void UpdateHandButtons ( )
124
+ {
125
+ buttons [ ( int ) ButtonTypes . Trigger ] . UpdateState ( trigger == 1 , trigger ) ;
126
+ buttons [ ( int ) ButtonTypes . Grip ] . UpdateState ( squeeze == 1 , squeeze ) ;
127
+ }
128
+
129
+ private void ResetAllButtons ( )
130
+ {
131
+ trigger = 0 ;
132
+ squeeze = 0 ;
133
+ thumbstick = 0 ;
134
+ thumbstickX = 0 ;
135
+ thumbstickY = 0 ;
136
+ touchpad = 0 ;
137
+ touchpadX = 0 ;
138
+ touchpadY = 0 ;
139
+ buttonA = 0 ;
140
+ buttonB = 0 ;
141
+ if ( buttons ? . Length == 6 )
142
+ {
143
+ UpdateAllButtons ( ) ;
144
+ }
145
+ }
146
+
97
147
private void TryUpdateButtons ( )
98
148
{
99
149
#if UNITY_EDITOR || ! UNITY_WEBGL
@@ -153,32 +203,22 @@ private void TryUpdateButtons()
153
203
buttonB = buttonPressed ? 1 : 0 ;
154
204
}
155
205
156
- WebXRControllerButton [ ] buttons = new WebXRControllerButton [ 6 ] ;
157
- buttons [ ( int ) ButtonTypes . Trigger ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
158
- buttons [ ( int ) ButtonTypes . Grip ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
159
- buttons [ ( int ) ButtonTypes . Thumbstick ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
160
- buttons [ ( int ) ButtonTypes . Touchpad ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
161
- buttons [ ( int ) ButtonTypes . ButtonA ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
162
- buttons [ ( int ) ButtonTypes . ButtonB ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
163
- UpdateButtons ( buttons ) ;
206
+ if ( buttons ? . Length != 6 )
207
+ {
208
+ InitButtons ( ) ;
209
+ }
210
+ else
211
+ {
212
+ UpdateAllButtons ( ) ;
213
+ }
164
214
}
165
215
#endif
166
216
}
167
217
168
- // Updates button states from Web gamepad API.
169
- private void UpdateButtons ( WebXRControllerButton [ ] buttons )
170
- {
171
- for ( int i = 0 ; i < buttons . Length ; i ++ )
172
- {
173
- WebXRControllerButton button = buttons [ i ] ;
174
- SetButtonState ( ( ButtonTypes ) i , button . pressed , button . value ) ;
175
- }
176
- }
177
-
178
- public float GetAxis ( AxisTypes action )
218
+ public float GetAxis ( AxisTypes axisType )
179
219
{
180
220
TryUpdateButtons ( ) ;
181
- switch ( action )
221
+ switch ( axisType )
182
222
{
183
223
case AxisTypes . Grip :
184
224
return squeeze ;
@@ -188,10 +228,10 @@ public float GetAxis(AxisTypes action)
188
228
return 0 ;
189
229
}
190
230
191
- public Vector2 GetAxis2D ( Axis2DTypes action )
231
+ public Vector2 GetAxis2D ( Axis2DTypes axisType )
192
232
{
193
233
TryUpdateButtons ( ) ;
194
- switch ( action )
234
+ switch ( axisType )
195
235
{
196
236
case Axis2DTypes . Thumbstick :
197
237
return new Vector2 ( thumbstickX , thumbstickY ) ;
@@ -201,44 +241,22 @@ public Vector2 GetAxis2D(Axis2DTypes action)
201
241
return Vector2 . zero ;
202
242
}
203
243
204
- public bool GetButton ( ButtonTypes action )
244
+ public bool GetButton ( ButtonTypes buttonType )
205
245
{
206
246
TryUpdateButtons ( ) ;
207
- if ( ! buttonStates . ContainsKey ( action ) )
208
- {
209
- return false ;
210
- }
211
- return buttonStates [ action ] . pressed ;
212
- }
213
-
214
- private void SetButtonState ( ButtonTypes action , bool isPressed , float value )
215
- {
216
- if ( buttonStates . ContainsKey ( action ) )
217
- {
218
- buttonStates [ action ] . UpdateState ( isPressed , value ) ;
219
- }
220
- else
221
- buttonStates . Add ( action , new WebXRControllerButton ( isPressed , value ) ) ;
247
+ return buttons [ ( int ) buttonType ] . pressed ;
222
248
}
223
249
224
- public bool GetButtonDown ( ButtonTypes action )
250
+ public bool GetButtonDown ( ButtonTypes buttonType )
225
251
{
226
252
TryUpdateButtons ( ) ;
227
- if ( ! buttonStates . ContainsKey ( action ) )
228
- {
229
- return false ;
230
- }
231
- return buttonStates [ action ] . down ;
253
+ return buttons [ ( int ) buttonType ] . down ;
232
254
}
233
255
234
- public bool GetButtonUp ( ButtonTypes action )
256
+ public bool GetButtonUp ( ButtonTypes buttonType )
235
257
{
236
258
TryUpdateButtons ( ) ;
237
- if ( ! buttonStates . ContainsKey ( action ) )
238
- {
239
- return false ;
240
- }
241
- return buttonStates [ action ] . up ;
259
+ return buttons [ ( int ) buttonType ] . up ;
242
260
}
243
261
244
262
public float GetButtonIndexValue ( int index )
@@ -344,14 +362,14 @@ private void OnControllerUpdate(WebXRControllerData controllerData)
344
362
buttonA = controllerData . buttonA ;
345
363
buttonB = controllerData . buttonB ;
346
364
347
- WebXRControllerButton [ ] buttons = new WebXRControllerButton [ 6 ] ;
348
- buttons [ ( int ) ButtonTypes . Trigger ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
349
- buttons [ ( int ) ButtonTypes . Grip ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
350
- buttons [ ( int ) ButtonTypes . Thumbstick ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
351
- buttons [ ( int ) ButtonTypes . Touchpad ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
352
- buttons [ ( int ) ButtonTypes . ButtonA ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
353
- buttons [ ( int ) ButtonTypes . ButtonB ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
354
- UpdateButtons ( buttons ) ;
365
+ if ( buttons ? . Length != 6 )
366
+ {
367
+ InitButtons ( ) ;
368
+ }
369
+ else
370
+ {
371
+ UpdateAllButtons ( ) ;
372
+ }
355
373
356
374
SetControllerActive ( true ) ;
357
375
}
@@ -411,22 +429,31 @@ private void OnHandUpdateInternal(WebXRHandData handData)
411
429
trigger = handData . trigger ;
412
430
squeeze = handData . squeeze ;
413
431
414
- WebXRControllerButton [ ] buttons = new WebXRControllerButton [ 2 ] ;
415
- buttons [ ( int ) ButtonTypes . Trigger ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
416
- buttons [ ( int ) ButtonTypes . Grip ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
417
- UpdateButtons ( buttons ) ;
432
+ if ( buttons ? . Length != 6 )
433
+ {
434
+ InitButtons ( ) ;
435
+ }
436
+ else
437
+ {
438
+ UpdateHandButtons ( ) ;
439
+ }
418
440
419
441
OnHandUpdate ? . Invoke ( handData ) ;
420
442
}
421
443
}
422
444
423
445
private void SetControllerActive ( bool active )
424
446
{
425
- if ( controllerActive != active )
447
+ if ( controllerActive == active )
448
+ {
449
+ return ;
450
+ }
451
+ if ( ! active )
426
452
{
427
- controllerActive = active ;
428
- OnControllerActive ? . Invoke ( controllerActive ) ;
453
+ ResetAllButtons ( ) ;
429
454
}
455
+ controllerActive = active ;
456
+ OnControllerActive ? . Invoke ( controllerActive ) ;
430
457
}
431
458
432
459
private void SetHandActive ( bool active )
@@ -435,6 +462,10 @@ private void SetHandActive(bool active)
435
462
{
436
463
return ;
437
464
}
465
+ if ( ! active )
466
+ {
467
+ ResetAllButtons ( ) ;
468
+ }
438
469
handActive = active ;
439
470
OnHandActive ? . Invoke ( handActive ) ;
440
471
}
0 commit comments