步骤一:在Package Manager安装Input System
步骤二:右键Create/InputActions,新建一个输入控制器(例如命名为ActionControls)
步骤三:配置映射及绑定,双击刚才新建的InputActions
3.1)添加Action Maps,例如Player或UI等等
3.2)Actions中添加行为,例如Move、Fire等等
3.3)Properties中添加属性,例如对于Move来说Acton Type为Value,Control Type为Vector2
3.4)Actions的行为下面添加绑定,例如Position[Mouse]
步骤四:添加一个PlayerInput组件
Actions选择步骤二创建的输入控制器,例如:ActionControls
Default Map选择默认的映射
Behavior选择响应处理方式
步骤五:处理响应
5.1)Send Messages和Broadcast Messages方式: On{ActionName}()函数,例如我们的ActionName是Move,则函数为OnMove
public void OnMove(InputValue value)
{
Debug.Log("OnMove : " + value.Get<Vector2>());
}
5.2)Invoke Unity Events方式:
Inspector->Player Input->Events->UI->Move(CallbackContext)中,将对象选择为脚本所在对象,函数选择脚本中的自定义输入处理函数:
public void OnMoveTest(InputAction.CallbackContext context)
{
Debug.Log("OnMoveTest : " +context.ReadValue<Vector2>());
}
5.3)Invoke C Sharp Events方式(最灵活常用的方式):
例1:
PlayerInput input;
void Awake()
{
input = GetComponent<PlayerInput>();
}
void OnEnable()
{
input.onActionTriggered+=MyEventFunction;
}
void OnDisable()
{
input.onActionTriggered-=MyEventFunction;
}
void MyEventFunction(InputAction.CallbackContext value)
{
if (callback.action.name == "Move")
{
var move = callback.ReadValue<Vector2>();
Debug.Log(move);
}
}
例2:
public class Character : MonoBehaviour
{
public PlayerInput playerInput;
public InputAction fireAction;
private bool fire;
private void Awake()
{
playerInput = GetComponen<PlayerInput>();
playerInput.onActionTriggered += ReadAction;
fireAction = playerInput.currentActionMap.FindAction("Fire");
}
private void ReadAction(InputAction.CallbackContext context)
{
if (context.action == fireAction)
{
if (context.started) fire = true;
if (context.canceled) fire = false;
}
}
}
例3:
void Start()
{
PlayerInput input = GetComponent<PlayerInput>();
InputAction moveAction = input.currentActionMap.FindAction("Move");
moveAction.performed += OnMoveAction;
}
private void OnMoveAction(InputAction.CallbackContext context)
{
Debug.Log("OnMoveDemo : " + context.ReadValue<Vector2>());
}
例4:
PlayerInput playerInput;
InputAction fireAction;
InputAction moveAction;
private void Awake()
{
playerInput = GetComponent<PlayerInput>();
fireAction = playerInput.currentActionMap.FindAction("Fire");
fireAction.performed += FireAction_performed;
fireAction.canceled += FireAction_canceled;
fireAction.started += FireAction_started;
moveAction = playerInput.currentActionMap.FindAction("move");
}
private void FireAction_started(InputAction.CallbackContext context)
{
throw new System.NotImplementedException();
}
private void FireAction_canceled(InputAction.CallbackContext context)
{
throw new System.NotImplementedException();
}
private void FireAction_performed(InputAction.CallbackContext context)
{
throw new System.NotImplementedException();
}
public void Update()
{
Vector2 m_Look = moveAction.ReadValue<Vector2>();
//...
}
例5:
private void OnMoveAction(InputAction.CallbackContext context)
{
Debug.Log("OnMoveDemo : " + context.ReadValue<Vector2>());
}
void Start()
{
InputAction move = new InputAction(binding: "<Mouse>/Position");
move.performed += OnMoveAction;
move.Enable();
}
例6:
InputAction click = new InputAction(binding: "<Mouse>/leftButton");
click.performed += ctx => {
RaycastHit hit;
Vector3 coor = Mouse.current.position.ReadValue();
if (Physics.Raycast(Camera.main.ScreenPointToRay(coor), out hit))
{
Debug.Log(hit.transform.name);
}
};
click.Enable();
例7:
void Start()
{
PlayerInput input = GetComponent<PlayerInput>();
input.actions.Enable();
input.actions["Move"].performed += OnMoveAction;
}
private void OnMoveAction(InputAction.CallbackContext context)
{
Debug.Log("OnMoveDemo : " + context.ReadValue<Vector2>());
}