【发布时间】:2020-09-05 06:05:23
【问题描述】:
我正在尝试学习新的 Unity 输入系统。在以前的 Unity 输入系统中,
if (Input.GetKeyDown("f"))
//do something when key 'f' is pressed
if (Input.GetKeyDown("g"))
//do something when key 'g' is pressed
if (Input.GetKeyDown("h"))
//do something when key 'h' is pressed
if (Input.GetKeyDown("j"))
//do something when key 'j' is pressed
会沿着 f,g,h,j 的按键执行任何定义的操作。
在新的 Unity 输入系统中,有 InputAction,我可以定义 Action Maps、Actions 和 Properties。然后我可以实现如下功能,可以在播放器对象的检查器下从 PlayerInput 调用。
public void OnKey_F(InputAction.CallbackContext context)
{
if (context.started)
Debug.Log("Pressed F");
}
public void OnKey_G(InputAction.CallbackContext context)
{
if (context.started)
Debug.Log("Pressed G");
}
public void OnKey_H(InputAction.CallbackContext context)
{
if (context.started)
Debug.Log("Pressed H");
}
public void OnKey_J(InputAction.CallbackContext context)
{
if (context.started)
Debug.Log("Pressed J");
}
在新的 Unity 输入系统下,这是执行与上述相同功能的正确方法吗?我找到的教程主要是针对 WASD 运动的,我已经弄清楚它是如何工作的(2d 矢量)。但是,我不确定这种情况是否需要多个键来实现多种功能。
【问题讨论】:
标签: c# unity3d input user-input