@@ -46,6 +46,7 @@ public class KeyListener : MonoBehaviour
46
46
private Joystick currentJoystick ;
47
47
private DpadControl currentJoystickDpad ;
48
48
private ButtonControl [ ] currentJoystickButtonControls ;
49
+ private AxisControl currentJoystickRightStickAxisX ;
49
50
private AxisControl currentJoystickRightStickAxisY ;
50
51
51
52
/// <summary>
@@ -107,9 +108,12 @@ public KeyListener()
107
108
currentJoystick = Joystick . current ;
108
109
if ( currentJoystick != null )
109
110
{
110
- var joystickChildren = currentJoystick . children ;
111
+ var joystickChildren = currentJoystick . children . ToArray ( ) ;
111
112
currentJoystickDpad = joystickChildren . FirstOrDefault ( t => t is DpadControl ) as DpadControl ;
112
- currentJoystickRightStickAxisY = joystickChildren . FirstOrDefault ( t => t . path . EndsWith ( "rz" ) ) as AxisControl ;
113
+
114
+ currentJoystickRightStickAxisX = joystickChildren . FirstOrDefault ( t => t . path . EndsWith ( "/z" ) ) as AxisControl ;
115
+ currentJoystickRightStickAxisY = joystickChildren . FirstOrDefault ( t => t . path . EndsWith ( "/rz" ) ) as AxisControl ;
116
+
113
117
currentJoystickButtonControls = joystickChildren . Where ( t => t is ButtonControl ) . Cast < ButtonControl > ( ) . ToArray ( ) ;
114
118
if ( currentJoystickButtonControls . Length < 15 )
115
119
currentJoystickButtonControls = currentJoystickButtonControls . Concat ( new ButtonControl [ 15 - currentJoystickButtonControls . Length ] ) . ToArray ( ) ;
@@ -290,6 +294,42 @@ private ButtonControl GetGamepadButtonControl(GamepadButton button)
290
294
return null ;
291
295
}
292
296
297
+ private float GetLeftStickXValue ( )
298
+ {
299
+ if ( currentGamepad != null )
300
+ return currentGamepad . leftStick . x . value ;
301
+ else if ( currentJoystick != null )
302
+ return currentJoystick . stick . x . value ;
303
+ return 0 ;
304
+ }
305
+
306
+ private float GetLeftStickYValue ( )
307
+ {
308
+ if ( currentGamepad != null )
309
+ return currentGamepad . leftStick . y . value ;
310
+ else if ( currentJoystick != null )
311
+ return currentJoystick . stick . y . value ;
312
+ return 0 ;
313
+ }
314
+
315
+ private float GetRightStickXValue ( )
316
+ {
317
+ if ( currentGamepad != null )
318
+ return currentGamepad . rightStick . x . value ;
319
+ else if ( currentJoystick != null )
320
+ return currentJoystickRightStickAxisX . value ;
321
+ return 0 ;
322
+ }
323
+
324
+ private float GetRightStickYValue ( )
325
+ {
326
+ if ( currentGamepad != null )
327
+ return currentGamepad . rightStick . y . value ;
328
+ else if ( currentJoystick != null )
329
+ return currentJoystickRightStickAxisY . value ;
330
+ return 0 ;
331
+ }
332
+
293
333
private bool IsKeyOrGamepadButtonsDown ( KeyListenerItem item )
294
334
{
295
335
var isKeydown = false ;
@@ -309,24 +349,21 @@ private bool IsKeyOrGamepadButtonsDown(KeyListenerItem item)
309
349
}
310
350
if ( isKeydown )
311
351
return isKeydown ;
312
- //最后再判断左摇杆操作
313
- Vector2 leftStickValue = default ;
314
- if ( currentGamepad != null )
315
- {
316
- leftStickValue = currentGamepad . leftStick . value ;
317
- }
318
- else if ( currentJoystick != null )
319
- {
320
- leftStickValue = currentJoystick . stick . value ;
321
- }
352
+ //最后再判断摇杆操作
322
353
if ( item . ContainsGamepadButton ( GamepadButton . DpadLeft ) )
323
- isKeydown = leftStickValue . x < - 0.4 ;
354
+ isKeydown = GetLeftStickXValue ( ) < - 0.4 ;
324
355
else if ( item . ContainsGamepadButton ( GamepadButton . DpadRight ) )
325
- isKeydown = leftStickValue . x > 0.4 ;
356
+ isKeydown = GetLeftStickXValue ( ) > 0.4 ;
326
357
else if ( item . ContainsGamepadButton ( GamepadButton . DpadUp ) )
327
- isKeydown = leftStickValue . y > 0.4 ;
358
+ isKeydown = GetLeftStickYValue ( ) > 0.4 ;
328
359
else if ( item . ContainsGamepadButton ( GamepadButton . DpadDown ) )
329
- isKeydown = leftStickValue . y < - 0.4 ;
360
+ isKeydown = GetLeftStickYValue ( ) < - 0.4 ;
361
+ else if ( item . ContainsGamepadButton ( GamepadButton . Y ) )
362
+ isKeydown = Math . Abs ( GetRightStickYValue ( ) ) >= 0.4 ;
363
+ else if ( item . ContainsGamepadButton ( GamepadButton . LeftShoulder ) )
364
+ isKeydown = GetRightStickXValue ( ) < - 0.4 ;
365
+ else if ( item . ContainsGamepadButton ( GamepadButton . RightShoulder ) )
366
+ isKeydown = GetRightStickXValue ( ) > 0.4 ;
330
367
return isKeydown ;
331
368
}
332
369
@@ -345,7 +382,7 @@ private void Update()
345
382
//排除GUI激活
346
383
if ( DisableWhenUIFocused && ( EventSystem . current . IsPointerOverGameObject ( ) || GUIUtility . hotControl != 0 ) )
347
384
return ;
348
-
385
+
349
386
//逆序遍历链表。后添加的按键事件最先处理
350
387
LinkedListNode < KeyListenerItem > cur = items . Last ;
351
388
KeyCode lastPressedKey = KeyCode . None ;
0 commit comments