赞
踩
目录
3>Up\Down\Left\Right Composite
4>Up\Down\Left\Right\Forward\Backward Composite
Action有五个状态:
Disabled Waiting Started Performed Canceled
其中Started、Performed和Canceled都在API中有对应的事件
可以通过打印phase查询当前的状态
- using UnityEngine;
- using UnityEngine.InputSystem;
-
-
- public class Test : MonoBehaviour
- {
- private MyPlayerAction inputActions;
- private void Start()
- {
- inputActions = new MyPlayerAction();
- inputActions.Enable();
- }
- void Update()
- {
- Debug.Log(GetComponent<PlayerInput>().actions["New Action"].phase);
- Debug.Log(inputActions.Player.NewAction.phase);
- }
- }
Actions旁边的“+”可以新建Action,双击名称进行重命名。
点击Action旁边的“+”号可以新建按键绑定
可以绑定单个按键或者摇杆
绑定两个按键来创建一维的虚拟轴,返回float类型,范围为[-1,1]。
需要设置两个按键:Negative和Positive
当Positive被按下 -> 返回1
当Negative被按下 -> 返回-1
否则 -> 返回0
绑定四个按键来创建二维的虚拟轴,返回Vector2类型。
在这个模式下,需要绑定四个按键,Up、Down、Left、Right,按键激活时对应的返回值:
Up -> (0,1)
Down -> (0,-1)
Left -> (-1,0)
Right -> (1,0)
无激活 -> (0,0)
绑定六个按键来创建三维的虚拟轴,返回Vector3类型。
Up -> (0,1,0)
Down -> (0,-1,0)
Left -> (-1,0,0)
Right -> (1,0,0)
Forward ->(0,0,1)
BackWard ->(0,0,-1)
无激活 -> (0,0,0)
需要绑定两个按键,一个为Modifier,一个为Binding。比如 ctrl + z ,ctrl 是modifier ,z 是binding。
与上面的类似,只是多了一个Modifier。
选择绑定模式后,点击新建的Binding,在右侧的Binding Properties设置Path,点击下拉三角,然后点击listen监听正在触发的按键可以进行快速绑定。
选中新建的Action,右侧可以看到对应的Action Properties
Action Type有三种类型:Value(值)、Button(按钮)、Pass Through
1>Value
监测按键数值的持续变化
2>Button
对按下按键做出响应
3>Pass Through
在Value和Button这两个模式下,当一个Action有多个设备的Bindings同时输入的时候,Action只会被触发程度最高的输入设备触发。(比如Move Action绑定了两个手柄的摇杆,当一个手柄向左,一个手柄向右的时候,Move Action只会响应摇杆偏移更大的手柄,忽略另一个手柄的输入。)
而在Pass Through模式下,当一个Action有多个设备的Bindings同时输入的时候,每个输入设备都会触发一次。(比如Move Action绑定了两个手柄的摇杆,当一个手柄向左,一个手柄向右的时候,Move Action会响应一次向左,响应一次向右,即会响应所有手柄的输入。)
Action只有在Enabled之后才能被触发,但Action的Bindings可能在Enabled之前就处在非默认状态(比如在Action Enabled之前,绑定的按键已经被按下),这时候:
Value模式下Initial State Check默认开启,无需手动设置。
Button和Pass Through模式下Initial State Check需要手动勾选。
例子:
我们增加两个Action:Test1和Test2,Action Type都为Button,其中Test1不勾选Initial State Check,Test2勾选
在游戏物体上挂载以下代码
- using UnityEngine;
-
- public class InitialCheckTest : MonoBehaviour
- {
- private MyAction inputActions;
- private int i;
- void Start()
- {
- inputActions = new MyAction();
- }
-
- void Update()
- {
- if(i == 250)
- {
- inputActions.Player.Enable();
- Debug.Log("Enabled!");
-
- }
- if(i == 251)
- {
- Debug.Log("Test1:" + inputActions.Player.Test1.triggered);
- Debug.Log("Test2:" + inputActions.Player.Test2.triggered);
- }
- i++;
- }
- }
点击运行并按着K和L键,5秒后,控制台输出结果如下:
Test1未勾选Initial State Check,所以无法识别出按键已按下
Action Properties和Binding Properties里面都可以设置Interaction。
在Action Properties里面设置Interaction,会影响到Action下面的所有Bindings,
在Binding Properties里面设置Interaction只会影响到这个Binding自身
请查阅:Interactions | Input System | 1.3.0 (unity3d.com)
当不添加Interactions的时候,会使用Default Interaction
对于Value Type:
- using UnityEngine;
-
- public class InteractionTest : MonoBehaviour
- {
- private MyAction inputActions;
-
- private void Start()
- {
- inputActions = new MyAction();
- inputActions.Player.Test1.started += Test1_started;
- inputActions.Player.Test1.performed += Test1_performed;
- inputActions.Player.Test1.canceled += Test1_canceled;
- inputActions.Player.Enable();
- }
- private void Update()
- {
- Debug.Log(inputActions.Player.Test1.phase);
- }
-
- private void Test1_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test1: canceled");
- }
-
- private void Test1_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test1: performed");
- }
-
- private void Test1_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test1: started");
- }
- }
控制台输出结果:
对于Button Type:
- using UnityEngine;
-
- public class InteractionTest : MonoBehaviour
- {
- private MyAction inputActions;
-
- private void Start()
- {
- inputActions = new MyAction();
- inputActions.Player.Test2.started += Test2_started;
- inputActions.Player.Test2.performed += Test2_performed;
- inputActions.Player.Test2.canceled += Test2_canceled;
- inputActions.Player.Enable();
- }
- private void Update()
- {
- Debug.Log(inputActions.Player.Test2.phase);
- }
-
- private void Test2_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test2: canceled");
- }
-
- private void Test2_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test2: performed");
- }
-
- private void Test2_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test2: started");
- }
- }
控制台输出结果:
对于Pass Through Type:
- using UnityEngine;
-
- public class InteractionTest : MonoBehaviour
- {
- private MyAction inputActions;
- private void Start()
- {
- inputActions = new MyAction();
- inputActions.Player.Test3.started += Test3_started;
- inputActions.Player.Test3.performed += Test3_performed;
- inputActions.Player.Test3.canceled += Test3_canceled;
- inputActions.Player.Enable();
- }
- private void Update()
- {
- Debug.Log(inputActions.Player.Test3.phase);
- }
-
- private void Test3_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test3: canceled");
- }
-
- private void Test3_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test3: performed");
- }
-
- private void Test3_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test3: started");
- }
- }
控制台输出结果:
Press Interaction有三种可选的Behavior:Press Only、Release Only和Press And Release
选择Press Only时:
- using UnityEngine;
-
- public class InteractionTest : MonoBehaviour
- {
- private MyAction inputActions;
- private void Start()
- {
- inputActions = new MyAction();
- inputActions.Player.Test4.started += Test4_started;
- inputActions.Player.Test4.performed += Test4_performed;
- inputActions.Player.Test4.canceled += Test4_canceled;
- inputActions.Player.Enable();
- }
- private void Update()
- {
- Debug.Log(inputActions.Player.Test4.phase);
- }
-
- private void Test4_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test4: canceled");
- }
-
- private void Test4_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test4: performed");
- }
-
- private void Test4_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test4: started");
- }
- }
控制台输出结果:
选择Release Only时:
修改Test4的Behavior为Release Only,运行后控制台输出结果:
选择Press and Release时:
修改Test4的Behavior为Press and Release,运行后控制台输出结果:
Performed事件一共触发两次:按键超过Press Point触发一次,松手低于Press Point再触发一次
- using UnityEngine;
-
- public class InteractionTest : MonoBehaviour
- {
- private MyAction inputActions;
- private void Start()
- {
- inputActions = new MyAction();
- inputActions.Player.Test5.started += Test5_started;
- inputActions.Player.Test5.performed += Test5_performed;
- inputActions.Player.Test5.canceled += Test5_canceled;
- inputActions.Player.Enable();
- }
- private void Update()
- {
- Debug.Log(inputActions.Player.Test5.phase);
- }
-
- private void Test5_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test5: canceled");
- }
-
- private void Test5_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test5: performed");
- }
-
- private void Test5_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test5: started");
- }
- }
控制台输出结果(成功Hold):
控制台输出结果(按键未维持足够时长):
- using UnityEngine;
-
- public class InteractionTest : MonoBehaviour
- {
- private MyAction inputActions;
- private void Start()
- {
- inputActions = new MyAction();
- inputActions.Player.Test6.started += Test6_started;
- inputActions.Player.Test6.performed += Test6_performed;
- inputActions.Player.Test6.canceled += Test6_canceled;
- inputActions.Player.Enable();
- }
- private void Update()
- {
- Debug.Log(inputActions.Player.Test6.phase);
- }
-
- private void Test6_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test6: canceled");
- }
-
- private void Test6_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test6: performed");
- }
-
- private void Test6_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test6: started");
- }
- }
控制台输出结果(成功Tap):
控制台输出结果(按键时间过长):
- using UnityEngine;
-
- public class InteractionTest : MonoBehaviour
- {
- private MyAction inputActions;
- private void Start()
- {
- inputActions = new MyAction();
- inputActions.Player.Test7.started += Test7_started;
- inputActions.Player.Test7.performed += Test7_performed;
- inputActions.Player.Test7.canceled += Test7_canceled;
- inputActions.Player.Enable();
- }
- private void Update()
- {
- Debug.Log(inputActions.Player.Test7.phase);
- }
-
- private void Test7_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test7: canceled");
- }
-
- private void Test7_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test7: performed");
- }
-
- private void Test7_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test7: started");
- }
- }
控制台输出结果(成功Slow Tap):
控制台输出结果(按键时间过短) :
- using UnityEngine;
-
- public class InteractionTest : MonoBehaviour
- {
- private MyAction inputActions;
- private void Start()
- {
- inputActions = new MyAction();
- inputActions.Player.Test8.started += Test8_started;
- inputActions.Player.Test8.performed += Test8_performed;
- inputActions.Player.Test8.canceled += Test8_canceled;
- inputActions.Player.Enable();
- }
- private void Update()
- {
- Debug.Log(inputActions.Player.Test8.phase);
- }
-
- private void Test8_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test8: canceled");
- }
-
- private void Test8_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test8: performed");
- }
-
- private void Test8_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
- {
- Debug.Log("Test8: started");
- }
- }
控制台输出结果(成功Multi Tap):
控制台输出结果(不满足Multi Tap):
请查阅:Processors | Input System | 1.3.0 (unity3d.com)
将值限定在Min~Max的范围内
将值乘以-1
勾选了Invert X则向量中的x分量乘以-1
勾选了Invert Y则向量中的y分量乘以-1
类似Invert Vector 2
当设定的Min>=Zero,将在Min~Max范围内的值标准化到0~1
当设定的Min<Zero,将在Min~Max范围内的值标准化到-1~1
维持向量方向不变,将向量的长度标准化为1
类似Normalize Vector 2
乘以设定好的Factor值
向量中的X,Y分量分别乘以设定好的值
类似Scale Vector 2
设定最小和最大值
当值小于最小的时候,视为0,即视同没有输入
当值的绝对值大于最大的时候,视为-1或1
对二维向量,与Axis deadzone类似
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。