目录
3>Up\Down\Left\Right Composite
4>Up\Down\Left\Right\Forward\Backward Composite
六、Action Phase
Action有五个状态:
Disabled Waiting Started Performed Canceled
- Disabled:未被启用
- Waiting:等待输入
- Started:接收到输入,但Action尚未被触发
- Performed:Action被触发
- 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);
}
}
七、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勾选
在游戏物体上挂载以下代码
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,所以无法识别出按键已按下
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 Started
- 值改变:Started Performed Started
- 回到默认值,无输入:Started Canceled Waiting
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:
- 检查到有输入:Waiting Started
- 输入超过按键阈值:Started Performed
- 回到默认值,无输入:Performed Canceled Waiting
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:
- 输入改变:Waiting Performed(没有Started状态)
- 回到默认值,无输入:Performed Canceled Waiting
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");
}
}
控制台输出结果:
2>Press Interaction
Press Interaction有三种可选的Behavior:Press Only、Release Only和Press And Release
选择Press Only时:
- 输入达到Press Point:Waiting Started Performed
- 松手低于Press Point:Performed Canceled Waiting
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时:
- 输入达到Press Point:Waiting Started
- 松手低于Press Point:Started Performed Canceled Waiting
修改Test4的Behavior为Release Only,运行后控制台输出结果:
选择Press and Release时:
- 输入达到Press Point:Waiting Started Performed
- 松手低于Press Point:Performed Canceled Waiting(注意在这里Performed事件会再触发一次)
修改Test4的Behavior为Press and Release,运行后控制台输出结果:
Performed事件一共触发两次:按键超过Press Point触发一次,松手低于Press Point再触发一次
3>Hold Interaction
- 输入达到Press Point:Waiting Started
- 持续按键超过设定的时长:Started Performed
- (未达到设定时长就松开按键: Started Canceled Waiting)
- 松手低于Press Point:Performed Canceled Waiting
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):
控制台输出结果(按键未维持足够时长):
4>Tap Interaction
- 输入达到Press Point:Waiting Started
- 在要求的时间间隔内松开: Started Performed Waiting
- (按键时间过长: Started Canceled Waiting)
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):
控制台输出结果(按键时间过长):
5>Slow Tap Interaction
- 输入达到Press Point:Waiting Started
- 长按足够时长后松开: Started Performed Waiting
- (按键时间过短: Started Canceled Waiting)
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):
控制台输出结果(按键时间过短) :
6>Multi Tap Interaction
- 输入达到Press Point:Waiting Started
- 在设定的时间间隔内连续按键达到足够次数: Started Performed Waiting
- (不满足: Started Canceled Waiting)
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):
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类似