浏览代码

Factorized Player Input for both systems

/feature-new-input-system
Thomas ICHÉ 4 年前
当前提交
cb2b883d
共有 3 个文件被更改,包括 126 次插入12 次删除
  1. 4
      Runtime/Controllers/KeyboardGamepadPlayerInput.cs
  2. 36
      Runtime/Controllers/PlayerInput.cs
  3. 98
      Runtime/Controllers/SimplePlayerInput.cs

4
Runtime/Controllers/KeyboardGamepadPlayerInput.cs


public override void UpdateInput()
{
#if ENABLE_LEGACY_INPUT_MANAGER
if(useGamepad || useKeyboardAndMouse)
{
m_Movement = new Vector2(Input.GetAxisRaw(MovementHorizontalAxis), Input.GetAxisRaw(MovementVerticalAxis));

m_Look += new Vector2(Input.GetAxisRaw(MouseHorizontalAxis), Input.GetAxisRaw(MouseVerticalAxis));
m_Jump = GetButtonState(JumpButton);
#else
Debug.LogWarning("KeyboardGamepadPlayerInput is only compatible with legacy input");
#endif
}
}
}

36
Runtime/Controllers/PlayerInput.cs


using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
#endif
public abstract class PlayerInput : MonoBehaviour
public abstract class PlayerInput : GameplayIngredientsBehaviour
{
public enum ButtonState
{

public abstract ButtonState Jump { get; }
public abstract void UpdateInput();
#if ENABLE_INPUT_SYSTEM
protected static ButtonState GetButtonControlState(ButtonControl bc)
{
if(bc == null)
{
return ButtonState.Released;
}
if (bc.isPressed)
{
if(bc.wasPressedThisFrame)
return ButtonState.JustPressed;
else
return ButtonState.Pressed;
}
else
{
if(bc.wasReleasedThisFrame)
return ButtonState.JustReleased;
else
return ButtonState.Released;
}
}
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
{
{
if (Input.GetButton(Button))
{
if (Input.GetButtonDown(Button))

}
}
#endif
}
}

98
Runtime/Controllers/SimplePlayerInput.cs


using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
#endif
namespace GameplayIngredients.Controllers
{

[Header("Movement")]
#if ENABLE_INPUT_SYSTEM && ENABLE_LEGACY_INPUT_MANAGER
public bool preferLegacyInput;
#endif
#if ENABLE_INPUT_SYSTEM
[Header("Input System Package")]
public InputAction move;
public InputAction look;
public InputAction jump;
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
[Header("Legacy Input")]
[Header("Look")]
[Header("Buttons")]
#endif
public override Vector2 Look => m_Look;
public override Vector2 Movement => m_Movement;

Vector2 m_Look;
ButtonState m_Jump;
ButtonState m_Jump;
private void Reset()
{
#if ENABLE_LEGACY_INPUT_MANAGER
InitializeValuesLegacy();
#endif
#if ENABLE_INPUT_SYSTEM
InitializeValuesNIS();
#endif
}
#if ENABLE_LEGACY_INPUT_MANAGER
[ContextMenu("Initialize Values (Legacy Input)")]
void InitializeValuesLegacy()
{
MovementHorizontalAxis = "Horizontal";
MovementVerticalAxis = "Vertical";
LookHorizontalAxis = "Mouse X";
LookVerticalAxis = "Mouse Y";
JumpButton = "Jump";
}
#endif
#if ENABLE_INPUT_SYSTEM
[ContextMenu("Initialize Values (Input System Package)")]
void InitializeValuesNIS()
{
move = new InputAction("Left Stick", InputActionType.Value, "<Gamepad>/Left Stick");
move.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d");
look = new InputAction("Right Stick", InputActionType.Value, "<Gamepad>/Right Stick");
look.AddBinding("<Pointer>/Delta")
.WithProcessor("ScaleVector2(x=0.1,y=0.1)");
jump = new InputAction("Jump", InputActionType.Button, "<Gamepad>/Button South");
jump.AddBinding("<Keyboard>/space");
}
#endif
m_Movement = new Vector2(Input.GetAxis(MovementHorizontalAxis), Input.GetAxis(MovementVerticalAxis));
m_Look = new Vector2(Input.GetAxis(LookHorizontalAxis), Input.GetAxis(LookVerticalAxis));
m_Jump = GetButtonState(JumpButton);
#if ENABLE_INPUT_SYSTEM && ENABLE_LEGACY_INPUT_MANAGER
if(preferLegacyInput)
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
{
m_Movement = new Vector2(Input.GetAxis(MovementHorizontalAxis), Input.GetAxis(MovementVerticalAxis));
m_Look = new Vector2(Input.GetAxis(LookHorizontalAxis), Input.GetAxis(LookVerticalAxis));
m_Jump = GetButtonState(JumpButton);
}
#endif
#if ENABLE_INPUT_SYSTEM && ENABLE_LEGACY_INPUT_MANAGER
else
#endif
#if ENABLE_INPUT_SYSTEM
{
if (!move.enabled)
move.Enable();
if(!look.enabled)
look.Enable();
if(!jump.enabled)
jump.Enable();
m_Movement = move.ReadValue<Vector2>();
m_Look = look.ReadValue<Vector2>();
m_Jump = GetButtonControlState(jump.activeControl as ButtonControl);
}
#endif
}
}
}
正在加载...
取消
保存