当前位置:   article > 正文

【Unity_Input System】Input System新输入系统(二)_using unityengine.inputsystem;

using unityengine.inputsystem;

目录

六、Action Phase

七、Input Action Asset文件

1.Bindings Mode 

1>Binding

2>Positive\Negative Binding

3>Up\Down\Left\Right Composite

4>Up\Down\Left\Right\Forward\Backward Composite

5>Binding with one modifier

6>Binding with two modifier

2.Binding Path

3.Action Type

4.Initial State Check

5.Interaction

1>Default Interaction

2>Press Interaction

3>Hold Interaction

4>Tap Interaction

5>Slow Tap Interaction

6>Multi Tap Interaction

6.Processors

1>Clamp

2>Invert

3>Invert Vector 2

4>Invert Vector 3

5>Normalize

6>Normalize Vector 2

7>Normalize Vector 3

8>Scale

9> Scale Vector 2

10>Scale Vector 3

11>Axis deadzone

12>Stick deadzone


六、Action Phase

Action有五个状态:

Disabled  \Rightarrow Waiting  \Rightarrow Started  \Rightarrow Performed   \Rightarrow Canceled

  • Disabled:未被启用
  • Waiting:等待输入
  • Started:接收到输入,但Action尚未被触发
  • Performed:Action被触发
  • Canceled:输入取消

其中Started、Performed和Canceled都在API中有对应的事件

可以通过打印phase查询当前的状态

  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public class Test : MonoBehaviour
  4. {
  5. private MyPlayerAction inputActions;
  6. private void Start()
  7. {
  8. inputActions = new MyPlayerAction();
  9. inputActions.Enable();
  10. }
  11. void Update()
  12. {
  13. Debug.Log(GetComponent<PlayerInput>().actions["New Action"].phase);
  14. Debug.Log(inputActions.Player.NewAction.phase);
  15. }
  16. }

七、Input Action Asset文件

Actions旁边的“+”可以新建Action,双击名称进行重命名。

 点击Action旁边的“+”号可以新建按键绑定

1.Bindings Mode 

1>Binding

可以绑定单个按键或者摇杆

2>Positive\Negative Binding

绑定两个按键来创建一维的虚拟轴,返回float类型,范围为[-1,1]。

需要设置两个按键:Negative和Positive

当Positive被按下 -> 返回1

当Negative被按下 -> 返回-1

否则 -> 返回0

3>Up\Down\Left\Right Composite

绑定四个按键来创建二维的虚拟轴,返回Vector2类型。

在这个模式下,需要绑定四个按键,Up、Down、Left、Right,按键激活时对应的返回值:

Up -> (0,1)

Down -> (0,-1)

Left -> (-1,0)

Right -> (1,0)

无激活 -> (0,0)

4>Up\Down\Left\Right\Forward\Backward Composite

绑定六个按键来创建三维的虚拟轴,返回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)

5>Binding with one modifier

需要绑定两个按键,一个为Modifier,一个为Binding。比如 ctrl + z ,ctrl 是modifier ,z 是binding。

6>Binding with two modifier

与上面的类似,只是多了一个Modifier。

2.Binding Path

选择绑定模式后,点击新建的Binding,在右侧的Binding Properties设置Path,点击下拉三角,然后点击listen监听正在触发的按键可以进行快速绑定。

3.Action Type

 选中新建的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会响应一次向左,响应一次向右,即会响应所有手柄的输入。)

4.Initial State Check

Action只有在Enabled之后才能被触发,但Action的Bindings可能在Enabled之前就处在非默认状态(比如在Action Enabled之前,绑定的按键已经被按下),这时候:

  • 开启了Initial State Check:在Action Enabled后的下一个Update循环中Action被触发
  • 未开启Initial State Check:Action不被触发

Value模式下Initial State Check默认开启,无需手动设置。

Button和Pass Through模式下Initial State Check需要手动勾选。

例子:

我们增加两个Action:Test1和Test2,Action Type都为Button,其中Test1不勾选Initial State Check,Test2勾选

 在游戏物体上挂载以下代码

  1. using UnityEngine;
  2. public class InitialCheckTest : MonoBehaviour
  3. {
  4. private MyAction inputActions;
  5. private int i;
  6. void Start()
  7. {
  8. inputActions = new MyAction();
  9. }
  10. void Update()
  11. {
  12. if(i == 250)
  13. {
  14. inputActions.Player.Enable();
  15. Debug.Log("Enabled!");
  16. }
  17. if(i == 251)
  18. {
  19. Debug.Log("Test1:" + inputActions.Player.Test1.triggered);
  20. Debug.Log("Test2:" + inputActions.Player.Test2.triggered);
  21. }
  22. i++;
  23. }
  24. }

点击运行并按着K和L键,5秒后,控制台输出结果如下:

Test1未勾选Initial State Check,所以无法识别出按键已按下

5.Interaction

Action Properties和Binding Properties里面都可以设置Interaction。

在Action Properties里面设置Interaction,会影响到Action下面的所有Bindings,

在Binding Properties里面设置Interaction只会影响到这个Binding自身

请查阅:Interactions | Input System | 1.3.0 (unity3d.com)

1>Default Interaction

当不添加Interactions的时候,会使用Default Interaction

对于Value Type:

  • 检查到有输入:Waiting \Rightarrow Started
  • 值改变:Started \Rightarrow Performed \Rightarrow Started
  • 回到默认值,无输入:Started \Rightarrow Canceled \Rightarrow Waiting

  1. using UnityEngine;
  2. public class InteractionTest : MonoBehaviour
  3. {
  4. private MyAction inputActions;
  5. private void Start()
  6. {
  7. inputActions = new MyAction();
  8. inputActions.Player.Test1.started += Test1_started;
  9. inputActions.Player.Test1.performed += Test1_performed;
  10. inputActions.Player.Test1.canceled += Test1_canceled;
  11. inputActions.Player.Enable();
  12. }
  13. private void Update()
  14. {
  15. Debug.Log(inputActions.Player.Test1.phase);
  16. }
  17. private void Test1_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  18. {
  19. Debug.Log("Test1: canceled");
  20. }
  21. private void Test1_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  22. {
  23. Debug.Log("Test1: performed");
  24. }
  25. private void Test1_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  26. {
  27. Debug.Log("Test1: started");
  28. }
  29. }

控制台输出结果:

对于Button Type:

  • 检查到有输入:Waiting \Rightarrow Started
  • 输入超过按键阈值:Started \Rightarrow Performed
  • 回到默认值,无输入:Performed \Rightarrow Canceled \Rightarrow Waiting

  1. using UnityEngine;
  2. public class InteractionTest : MonoBehaviour
  3. {
  4. private MyAction inputActions;
  5. private void Start()
  6. {
  7. inputActions = new MyAction();
  8. inputActions.Player.Test2.started += Test2_started;
  9. inputActions.Player.Test2.performed += Test2_performed;
  10. inputActions.Player.Test2.canceled += Test2_canceled;
  11. inputActions.Player.Enable();
  12. }
  13. private void Update()
  14. {
  15. Debug.Log(inputActions.Player.Test2.phase);
  16. }
  17. private void Test2_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  18. {
  19. Debug.Log("Test2: canceled");
  20. }
  21. private void Test2_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  22. {
  23. Debug.Log("Test2: performed");
  24. }
  25. private void Test2_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  26. {
  27. Debug.Log("Test2: started");
  28. }
  29. }

控制台输出结果:

 对于Pass Through Type:

  • 输入改变:Waiting \Rightarrow Performed(没有Started状态)
  • 回到默认值,无输入:Performed \Rightarrow Canceled \Rightarrow Waiting

  1. using UnityEngine;
  2. public class InteractionTest : MonoBehaviour
  3. {
  4. private MyAction inputActions;
  5. private void Start()
  6. {
  7. inputActions = new MyAction();
  8. inputActions.Player.Test3.started += Test3_started;
  9. inputActions.Player.Test3.performed += Test3_performed;
  10. inputActions.Player.Test3.canceled += Test3_canceled;
  11. inputActions.Player.Enable();
  12. }
  13. private void Update()
  14. {
  15. Debug.Log(inputActions.Player.Test3.phase);
  16. }
  17. private void Test3_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  18. {
  19. Debug.Log("Test3: canceled");
  20. }
  21. private void Test3_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  22. {
  23. Debug.Log("Test3: performed");
  24. }
  25. private void Test3_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  26. {
  27. Debug.Log("Test3: started");
  28. }
  29. }

控制台输出结果:

2>Press Interaction

 Press Interaction有三种可选的Behavior:Press Only、Release Only和Press And Release

选择Press Only时:

  • 输入达到Press Point:Waiting \Rightarrow Started  \Rightarrow Performed
  • 松手低于Press Point:Performed \Rightarrow Canceled \Rightarrow Waiting

  1. using UnityEngine;
  2. public class InteractionTest : MonoBehaviour
  3. {
  4. private MyAction inputActions;
  5. private void Start()
  6. {
  7. inputActions = new MyAction();
  8. inputActions.Player.Test4.started += Test4_started;
  9. inputActions.Player.Test4.performed += Test4_performed;
  10. inputActions.Player.Test4.canceled += Test4_canceled;
  11. inputActions.Player.Enable();
  12. }
  13. private void Update()
  14. {
  15. Debug.Log(inputActions.Player.Test4.phase);
  16. }
  17. private void Test4_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  18. {
  19. Debug.Log("Test4: canceled");
  20. }
  21. private void Test4_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  22. {
  23. Debug.Log("Test4: performed");
  24. }
  25. private void Test4_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  26. {
  27. Debug.Log("Test4: started");
  28. }
  29. }

 控制台输出结果:

选择Release Only时:

  • 输入达到Press Point:Waiting \Rightarrow Started
  • 松手低于Press Point:Started \Rightarrow Performed \Rightarrow Canceled \Rightarrow Waiting

 修改Test4的Behavior为Release Only,运行后控制台输出结果:

 

 选择Press and Release时:

  • 输入达到Press Point:Waiting \Rightarrow Started \Rightarrow Performed
  • 松手低于Press Point:Performed \Rightarrow Canceled \Rightarrow Waiting(注意在这里Performed事件会再触发一次)

 修改Test4的Behavior为Press and Release,运行后控制台输出结果:

Performed事件一共触发两次:按键超过Press Point触发一次,松手低于Press Point再触发一次

3>Hold Interaction

  • 输入达到Press Point:Waiting \Rightarrow Started
  • 持续按键超过设定的时长:Started \Rightarrow Performed
  • (未达到设定时长就松开按键: Started \Rightarrow Canceled \Rightarrow Waiting)
  • 松手低于Press Point:Performed \Rightarrow Canceled \Rightarrow Waiting

  1. using UnityEngine;
  2. public class InteractionTest : MonoBehaviour
  3. {
  4. private MyAction inputActions;
  5. private void Start()
  6. {
  7. inputActions = new MyAction();
  8. inputActions.Player.Test5.started += Test5_started;
  9. inputActions.Player.Test5.performed += Test5_performed;
  10. inputActions.Player.Test5.canceled += Test5_canceled;
  11. inputActions.Player.Enable();
  12. }
  13. private void Update()
  14. {
  15. Debug.Log(inputActions.Player.Test5.phase);
  16. }
  17. private void Test5_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  18. {
  19. Debug.Log("Test5: canceled");
  20. }
  21. private void Test5_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  22. {
  23. Debug.Log("Test5: performed");
  24. }
  25. private void Test5_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  26. {
  27. Debug.Log("Test5: started");
  28. }
  29. }

控制台输出结果(成功Hold):

控制台输出结果(按键未维持足够时长):

 

4>Tap Interaction

  • 输入达到Press Point:Waiting \Rightarrow Started
  • 在要求的时间间隔内松开: Started \Rightarrow Performed \Rightarrow Waiting
  • (按键时间过长: Started  \Rightarrow Canceled \Rightarrow Waiting)

  1. using UnityEngine;
  2. public class InteractionTest : MonoBehaviour
  3. {
  4. private MyAction inputActions;
  5. private void Start()
  6. {
  7. inputActions = new MyAction();
  8. inputActions.Player.Test6.started += Test6_started;
  9. inputActions.Player.Test6.performed += Test6_performed;
  10. inputActions.Player.Test6.canceled += Test6_canceled;
  11. inputActions.Player.Enable();
  12. }
  13. private void Update()
  14. {
  15. Debug.Log(inputActions.Player.Test6.phase);
  16. }
  17. private void Test6_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  18. {
  19. Debug.Log("Test6: canceled");
  20. }
  21. private void Test6_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  22. {
  23. Debug.Log("Test6: performed");
  24. }
  25. private void Test6_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  26. {
  27. Debug.Log("Test6: started");
  28. }
  29. }

控制台输出结果(成功Tap):

控制台输出结果(按键时间过长):

5>Slow Tap Interaction

  • 输入达到Press Point:Waiting \Rightarrow Started
  • 长按足够时长后松开: Started \Rightarrow Performed \Rightarrow Waiting
  • (按键时间过短: Started  \Rightarrow Canceled \Rightarrow Waiting)

  1. using UnityEngine;
  2. public class InteractionTest : MonoBehaviour
  3. {
  4. private MyAction inputActions;
  5. private void Start()
  6. {
  7. inputActions = new MyAction();
  8. inputActions.Player.Test7.started += Test7_started;
  9. inputActions.Player.Test7.performed += Test7_performed;
  10. inputActions.Player.Test7.canceled += Test7_canceled;
  11. inputActions.Player.Enable();
  12. }
  13. private void Update()
  14. {
  15. Debug.Log(inputActions.Player.Test7.phase);
  16. }
  17. private void Test7_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  18. {
  19. Debug.Log("Test7: canceled");
  20. }
  21. private void Test7_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  22. {
  23. Debug.Log("Test7: performed");
  24. }
  25. private void Test7_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  26. {
  27. Debug.Log("Test7: started");
  28. }
  29. }

 控制台输出结果(成功Slow Tap):

控制台输出结果(按键时间过短) :

6>Multi Tap Interaction

  •  输入达到Press Point:Waiting \Rightarrow Started
  • 在设定的时间间隔内连续按键达到足够次数: Started \Rightarrow Performed \Rightarrow Waiting
  • (不满足: Started  \Rightarrow Canceled \Rightarrow Waiting)

 

  1. using UnityEngine;
  2. public class InteractionTest : MonoBehaviour
  3. {
  4. private MyAction inputActions;
  5. private void Start()
  6. {
  7. inputActions = new MyAction();
  8. inputActions.Player.Test8.started += Test8_started;
  9. inputActions.Player.Test8.performed += Test8_performed;
  10. inputActions.Player.Test8.canceled += Test8_canceled;
  11. inputActions.Player.Enable();
  12. }
  13. private void Update()
  14. {
  15. Debug.Log(inputActions.Player.Test8.phase);
  16. }
  17. private void Test8_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  18. {
  19. Debug.Log("Test8: canceled");
  20. }
  21. private void Test8_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  22. {
  23. Debug.Log("Test8: performed");
  24. }
  25. private void Test8_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
  26. {
  27. Debug.Log("Test8: started");
  28. }
  29. }

 控制台输出结果(成功Multi Tap):

 控制台输出结果(不满足Multi Tap):

6.Processors

请查阅:Processors | Input System | 1.3.0 (unity3d.com)

1>Clamp

将值限定在Min~Max的范围内

  • 小于min:返回min
  • 处于min~max:返回自身
  • 大于max:返回max 

2>Invert

将值乘以-1

3>Invert Vector 2

勾选了Invert X则向量中的x分量乘以-1

勾选了Invert Y则向量中的y分量乘以-1

4>Invert Vector 3

类似Invert Vector 2

5>Normalize

当设定的Min>=Zero,将在Min~Max范围内的值标准化到0~1

当设定的Min<Zero,将在Min~Max范围内的值标准化到-1~1

6>Normalize Vector 2

维持向量方向不变,将向量的长度标准化为1

7>Normalize Vector 3

类似Normalize Vector 2

8>Scale

乘以设定好的Factor值

9> Scale Vector 2

向量中的X,Y分量分别乘以设定好的值

10>Scale Vector 3

类似Scale Vector 2

11>Axis deadzone

 

设定最小和最大值

当值小于最小的时候,视为0,即视同没有输入

当值的绝对值大于最大的时候,视为-1或1

12>Stick deadzone

对二维向量,与Axis deadzone类似

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/118805
推荐阅读
相关标签
  

闽ICP备14008679号