Skip to content

Commit c2ef4aa

Browse files
committed
增加手柄右摇杆控制视角功能。
1 parent eb69e4e commit c2ef4aa

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

Assets/System/Scripts/Services/InputManager/KeyListener.cs

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class KeyListener : MonoBehaviour
4646
private Joystick currentJoystick;
4747
private DpadControl currentJoystickDpad;
4848
private ButtonControl[] currentJoystickButtonControls;
49+
private AxisControl currentJoystickRightStickAxisX;
4950
private AxisControl currentJoystickRightStickAxisY;
5051

5152
/// <summary>
@@ -107,9 +108,12 @@ public KeyListener()
107108
currentJoystick = Joystick.current;
108109
if (currentJoystick != null)
109110
{
110-
var joystickChildren = currentJoystick.children;
111+
var joystickChildren = currentJoystick.children.ToArray();
111112
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+
113117
currentJoystickButtonControls = joystickChildren.Where(t => t is ButtonControl).Cast<ButtonControl>().ToArray();
114118
if (currentJoystickButtonControls.Length < 15)
115119
currentJoystickButtonControls = currentJoystickButtonControls.Concat(new ButtonControl[15 - currentJoystickButtonControls.Length]).ToArray();
@@ -290,6 +294,42 @@ private ButtonControl GetGamepadButtonControl(GamepadButton button)
290294
return null;
291295
}
292296

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+
293333
private bool IsKeyOrGamepadButtonsDown(KeyListenerItem item)
294334
{
295335
var isKeydown = false;
@@ -309,24 +349,21 @@ private bool IsKeyOrGamepadButtonsDown(KeyListenerItem item)
309349
}
310350
if (isKeydown)
311351
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+
//最后再判断摇杆操作
322353
if (item.ContainsGamepadButton(GamepadButton.DpadLeft))
323-
isKeydown = leftStickValue.x < -0.4;
354+
isKeydown = GetLeftStickXValue() < -0.4;
324355
else if (item.ContainsGamepadButton(GamepadButton.DpadRight))
325-
isKeydown = leftStickValue.x > 0.4;
356+
isKeydown = GetLeftStickXValue() > 0.4;
326357
else if (item.ContainsGamepadButton(GamepadButton.DpadUp))
327-
isKeydown = leftStickValue.y > 0.4;
358+
isKeydown = GetLeftStickYValue() > 0.4;
328359
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;
330367
return isKeydown;
331368
}
332369

@@ -345,7 +382,7 @@ private void Update()
345382
//排除GUI激活
346383
if (DisableWhenUIFocused && (EventSystem.current.IsPointerOverGameObject() || GUIUtility.hotControl != 0))
347384
return;
348-
385+
349386
//逆序遍历链表。后添加的按键事件最先处理
350387
LinkedListNode<KeyListenerItem> cur = items.Last;
351388
KeyCode lastPressedKey = KeyCode.None;

0 commit comments

Comments
 (0)