1
1
using UnityEngine ;
2
+ #if UNITY_EDITOR
3
+ using UnityEngine . XR ;
4
+ #endif
2
5
using System ;
3
6
using System . Collections . Generic ;
4
7
@@ -43,6 +46,78 @@ public class WebXRController : MonoBehaviour
43
46
private Dictionary < int , Transform > handJoints = new Dictionary < int , Transform > ( ) ;
44
47
private bool handJointsVisible = false ;
45
48
49
+ #if UNITY_EDITOR
50
+ private InputDeviceCharacteristics xrHand = InputDeviceCharacteristics . Controller ;
51
+ private InputDevice ? inputDevice ;
52
+ private HapticCapabilities ? hapticCapabilities ;
53
+ #endif
54
+
55
+ public void TryUpdateButtons ( )
56
+ {
57
+ #if UNITY_EDITOR
58
+ if ( ! WebXRManager . Instance . isSubsystemAvailable && inputDevice != null )
59
+ {
60
+ inputDevice . Value . TryGetFeatureValue ( CommonUsages . trigger , out trigger ) ;
61
+ inputDevice . Value . TryGetFeatureValue ( CommonUsages . grip , out squeeze ) ;
62
+ if ( trigger <= 0.02 )
63
+ {
64
+ trigger = 0 ;
65
+ }
66
+ else if ( trigger >= 0.98 )
67
+ {
68
+ trigger = 1 ;
69
+ }
70
+
71
+ if ( squeeze <= 0.02 )
72
+ {
73
+ squeeze = 0 ;
74
+ }
75
+ else if ( squeeze >= 0.98 )
76
+ {
77
+ squeeze = 1 ;
78
+ }
79
+
80
+ Vector2 axis2D ;
81
+ if ( inputDevice . Value . TryGetFeatureValue ( CommonUsages . primary2DAxis , out axis2D ) )
82
+ {
83
+ thumbstickX = axis2D . x ;
84
+ thumbstickY = axis2D . y ;
85
+ }
86
+ if ( inputDevice . Value . TryGetFeatureValue ( CommonUsages . secondary2DAxis , out axis2D ) )
87
+ {
88
+ touchpadX = axis2D . x ;
89
+ touchpadY = axis2D . y ;
90
+ }
91
+ bool buttonPressed ;
92
+ if ( inputDevice . Value . TryGetFeatureValue ( CommonUsages . primary2DAxisClick , out buttonPressed ) )
93
+ {
94
+ thumbstick = buttonPressed ? 1 : 0 ;
95
+ }
96
+ if ( inputDevice . Value . TryGetFeatureValue ( CommonUsages . secondary2DAxisClick , out buttonPressed ) )
97
+ {
98
+ touchpad = buttonPressed ? 1 : 0 ;
99
+ }
100
+ if ( inputDevice . Value . TryGetFeatureValue ( CommonUsages . primaryButton , out buttonPressed ) )
101
+ {
102
+ buttonA = buttonPressed ? 1 : 0 ;
103
+ }
104
+ if ( inputDevice . Value . TryGetFeatureValue ( CommonUsages . secondaryButton , out buttonPressed ) )
105
+ {
106
+ buttonB = buttonPressed ? 1 : 0 ;
107
+ }
108
+
109
+ WebXRControllerButton [ ] buttons = new WebXRControllerButton [ 6 ] ;
110
+ buttons [ 0 ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
111
+ buttons [ 1 ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
112
+ buttons [ 2 ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
113
+ buttons [ 3 ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
114
+ buttons [ 4 ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
115
+ buttons [ 5 ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
116
+ UpdateButtons ( buttons ) ;
117
+ }
118
+ #endif
119
+ }
120
+
46
121
// Updates button states from Web gamepad API.
47
122
private void UpdateButtons ( WebXRControllerButton [ ] buttons )
48
123
{
@@ -161,12 +236,12 @@ private void OnControllerUpdate(WebXRControllerData controllerData)
161
236
buttonB = controllerData . buttonB ;
162
237
163
238
WebXRControllerButton [ ] buttons = new WebXRControllerButton [ 6 ] ;
164
- buttons [ 0 ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
165
- buttons [ 1 ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
166
- buttons [ 2 ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
167
- buttons [ 3 ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
168
- buttons [ 4 ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
169
- buttons [ 5 ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
239
+ buttons [ 0 ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
240
+ buttons [ 1 ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
241
+ buttons [ 2 ] = new WebXRControllerButton ( thumbstick == 1 , thumbstick ) ;
242
+ buttons [ 3 ] = new WebXRControllerButton ( touchpad == 1 , touchpad ) ;
243
+ buttons [ 4 ] = new WebXRControllerButton ( buttonA == 1 , buttonA ) ;
244
+ buttons [ 5 ] = new WebXRControllerButton ( buttonB == 1 , buttonB ) ;
170
245
UpdateButtons ( buttons ) ;
171
246
}
172
247
}
@@ -188,7 +263,7 @@ private void OnHandUpdate(WebXRHandData handData)
188
263
189
264
Quaternion rotationOffset = Quaternion . Inverse ( handData . joints [ 0 ] . rotation ) ;
190
265
191
- for ( int i = 0 ; i <= WebXRHandData . LITTLE_PHALANX_TIP ; i ++ )
266
+ for ( int i = 0 ; i <= WebXRHandData . LITTLE_PHALANX_TIP ; i ++ )
192
267
{
193
268
if ( handData . joints [ i ] . enabled )
194
269
{
@@ -220,8 +295,8 @@ private void OnHandUpdate(WebXRHandData handData)
220
295
squeeze = handData . squeeze ;
221
296
222
297
WebXRControllerButton [ ] buttons = new WebXRControllerButton [ 2 ] ;
223
- buttons [ 0 ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
224
- buttons [ 1 ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
298
+ buttons [ 0 ] = new WebXRControllerButton ( trigger == 1 , trigger ) ;
299
+ buttons [ 1 ] = new WebXRControllerButton ( squeeze == 1 , squeeze ) ;
225
300
UpdateButtons ( buttons ) ;
226
301
}
227
302
}
@@ -268,7 +343,19 @@ private void SetHandJointsVisible(bool visible)
268
343
// intensity 0 to 1, duration milliseconds
269
344
public void Pulse ( float intensity , float duration )
270
345
{
271
- WebXRManager . Instance . HapticPulse ( hand , intensity , duration ) ;
346
+ if ( WebXRManager . Instance . isSubsystemAvailable )
347
+ {
348
+ WebXRManager . Instance . HapticPulse ( hand , intensity , duration ) ;
349
+ }
350
+ #if UNITY_EDITOR
351
+ else if ( inputDevice ! = null && hapticCapabilities != null
352
+ && hapticCapabilities . Value . supportsImpulse )
353
+ {
354
+ // duration in seconds
355
+ duration = duration * 0.001f ;
356
+ inputDevice . Value . SendHapticImpulse ( 0 , intensity , duration ) ;
357
+ }
358
+ #endif
272
359
}
273
360
274
361
void OnEnable ( )
@@ -282,6 +369,27 @@ void OnEnable()
282
369
WebXRManager . OnHandUpdate += OnHandUpdate ;
283
370
WebXRManager . OnHeadsetUpdate += onHeadsetUpdate ;
284
371
SetVisible ( false ) ;
372
+ #if UNITY_EDITOR
373
+ switch ( hand )
374
+ {
375
+ case WebXRControllerHand . LEFT :
376
+ xrHand = InputDeviceCharacteristics . Controller | InputDeviceCharacteristics . Left ;
377
+ break ;
378
+ case WebXRControllerHand . RIGHT :
379
+ xrHand = InputDeviceCharacteristics . Controller | InputDeviceCharacteristics . Right ;
380
+ break ;
381
+ }
382
+
383
+ List < InputDevice > allDevices = new List < InputDevice > ( ) ;
384
+ InputDevices . GetDevicesWithCharacteristics ( xrHand , allDevices ) ;
385
+ foreach ( InputDevice device in allDevices )
386
+ {
387
+ HandleInputDevicesConnected ( device ) ;
388
+ }
389
+
390
+ InputDevices . deviceConnected += HandleInputDevicesConnected ;
391
+ InputDevices . deviceDisconnected += HandleInputDevicesDisconnected ;
392
+ #endif
285
393
}
286
394
287
395
void OnDisabled ( )
@@ -290,6 +398,37 @@ void OnDisabled()
290
398
WebXRManager . OnHandUpdate -= OnHandUpdate ;
291
399
WebXRManager . OnHeadsetUpdate -= onHeadsetUpdate ;
292
400
SetVisible ( false ) ;
401
+ #if UNITY_EDITOR
402
+ InputDevices . deviceConnected -= HandleInputDevicesConnected ;
403
+ InputDevices . deviceDisconnected -= HandleInputDevicesDisconnected ;
404
+ inputDevice = null ;
405
+ #endif
406
+ }
407
+
408
+ #if UNITY_EDITOR
409
+ private void HandleInputDevicesConnected ( InputDevice device )
410
+ {
411
+ if ( device . characteristics . HasFlag ( xrHand ) )
412
+ {
413
+ inputDevice = device ;
414
+ HapticCapabilities capabilities ;
415
+ if ( device . TryGetHapticCapabilities ( out capabilities ) )
416
+ {
417
+ hapticCapabilities = capabilities ;
418
+ }
419
+ SetVisible ( true ) ;
420
+ }
421
+ }
422
+
423
+ private void HandleInputDevicesDisconnected ( InputDevice device )
424
+ {
425
+ if ( inputDevice != null && inputDevice . Value == device )
426
+ {
427
+ inputDevice = null ;
428
+ hapticCapabilities = null ;
429
+ SetVisible ( false ) ;
430
+ }
293
431
}
432
+ #endif
294
433
}
295
434
}
0 commit comments