OnControlsChanged(PlayerInput input):控制器切换,如键盘和手柄都接入,默认设备为键盘,这时候按下手柄,就会触发 给PlayerInput 其中OnMove和OnJump为InputActions资产中的Actions的名称 想要触发OnMove和OnJump还有自带的默认事件,将相应的脚步挂载在PlayerInput组件所在物体即可 public void OnDeviceLost(PlayerInput input...
使用Input System,我们的脚本需要引用using UnityEngine.InputSystem 生成的代码 生成的代码各位可以自行查看,这里就不详讲,要不然篇幅太长了。 新建一个脚本。引入命名空间。 声明自定义的InputActions并实例化,我这边的类名是MyInputSystem。 注意:使用之前一定要执行Enable();否则不起作用。每一个Map以及Action都可以...
PlayerInput playerInput; private void OnEnable() { playerInput = GetComponent<PlayerInput>(); playerInput.currentActionMap.FindAction("Jump").performed += OnJump; playerInput.currentActionMap.FindAction("Move").performed += OnMove; } private void OnDisable() { playerInput.currentActionMap.FindA...
在Unity中,Input System是一个强大且灵活的输入解决方案,它提供了多种方法来处理不同类型的输入设备,如键盘、鼠标和游戏手柄。以下是关于Unity Input System的基本概念、功能和使用方法的详细解答: 1. Unity Input System的基本概念 Input System:Unity的新输入系统,旨在替代旧的Input Manager,提供更丰富、更灵活的功...
通过Package Manager安装好InputSystem之后可以同时导入一些范例资源,阅读其使用代码,InputSystem有几种不同的使用方法,开发时应按需求酌情选择。 需要注意的是,安装好InputSystem之后会有提示询问是否切换到新版输入管理系统,切换后Input Setting面板将失效,游戏中也无法使用Input类获取输入。
public void OnEnable() { rigidbody = GetComponent<Rigidbody>(); if(_inputSystemAsset == null) { _inputSystemAsset = new InputSystemAsset(); _inputSystemAsset.Player.SetCallbacks(new PlayerActionCallback(this)); _inputSystemAsset.Enable(); } } public void OnDisable() { _inputSystemAsset...
1.新建一个MonoBehaviour脚本,获取InputAction,可以是从InputActionAsset查阅获取,直接引用,或手动创建; 2.在Start函数注册回调事件,并启用InputAction: void Start(){//...if(inputAction!=null){inputAction.performed+=OnPerformed;inputAction.Enable();}} ...
voidInputTest(){GameInput inputAction=newGameInput();//GameInput为场景中的InputSystem控制器inputAction.Enable();inputAction.Gameplay.MouseDown.performed+=ctx=>{Debug.Log("按下:"+UnityEngine.InputSystem.Mouse.current.position.ReadValue());};inputAction.Gameplay.MouseDrag.performed+=ctx=>{Debug.Log(...
禁用控件后无法启用控件是指在使用Unity新输入系统时,当我们将一个控件禁用(Disable)后,无法直接通过启用(Enable)来重新激活该控件。这是因为Unity新输入系统中的控件状态是由InputAction来管理的,禁用控件实际上是将其对应的InputAction的Enable状态设置为false,而无法直接通过Enable来重新激活。 要解决这个问题,我们可以...
10. 11. 12. 13. 14. 15. 16. 17. 18. 从Awake方法中可以直接获得所有定义的按键事件组合,然后把对应的处理函数交给performed以及cancelled处理。这样就完成了。 需要注意的是,这种方法需要先Enable对应的InputAction,否则不会工作。 是不是很简单?