自定义键码变量
Custom KeyCode Variable
我正在创建一个虚拟启动板,我将把这个脚本附加到每个按钮,但我需要它是模块化的,这样我就不需要 64 个不同的脚本来做完全相同的事情.
我的问题是我不确定如何获取 KeyCodes 和 public 变量。因此,如果我声明一个名为 "theKey":
的 public 变量
if (Input.GetKeyDown(KeyCode.theKey))
{ //play sound etc.
}
与此类似的东西,以便在 unity 中我可以使用检查器插入此脚本附加到的按钮的热键。
KeyCode.String 不起作用,我需要像 .Alpha2
这样的完整密钥代码
public class LaunchManager : MonoBehaviour {
// Define public variables
public Button b11;
// Initialization
void Start () {
}
// Update is called once per frame
void Update () {
var pointer = new PointerEventData(EventSystem.current);
// For Single Use (sound emmission)
if (Input.GetKeyDown(KeyCode.Alpha2)) {
// Click Button from keypress
ExecuteEvents.Execute(b11.gameObject, pointer, ExecuteEvents.pointerDownHandler);
// Play Sound
GetComponent<AudioSource>().Play();
}
// Execute while clicking
if (Input.GetKey(KeyCode.Alpha2)) {
//set button colour to pressed state
}
}
}
非常感谢任何帮助。我确实仔细检查过这个问题的答案。如果我仍然缺少相关细节,请告诉我,我会回复。
如果我正确理解你的问题,你想创建一个 public Keycode 变量,它是这样完成的:
public Keycode theKey = Keycode.None;
您可以像这样使用它:
if (Input.GetKeyDown(theKey))
{
//Do code
}
我正在创建一个虚拟启动板,我将把这个脚本附加到每个按钮,但我需要它是模块化的,这样我就不需要 64 个不同的脚本来做完全相同的事情.
我的问题是我不确定如何获取 KeyCodes 和 public 变量。因此,如果我声明一个名为 "theKey":
的 public 变量if (Input.GetKeyDown(KeyCode.theKey))
{ //play sound etc.
}
与此类似的东西,以便在 unity 中我可以使用检查器插入此脚本附加到的按钮的热键。
KeyCode.String 不起作用,我需要像 .Alpha2
这样的完整密钥代码public class LaunchManager : MonoBehaviour {
// Define public variables
public Button b11;
// Initialization
void Start () {
}
// Update is called once per frame
void Update () {
var pointer = new PointerEventData(EventSystem.current);
// For Single Use (sound emmission)
if (Input.GetKeyDown(KeyCode.Alpha2)) {
// Click Button from keypress
ExecuteEvents.Execute(b11.gameObject, pointer, ExecuteEvents.pointerDownHandler);
// Play Sound
GetComponent<AudioSource>().Play();
}
// Execute while clicking
if (Input.GetKey(KeyCode.Alpha2)) {
//set button colour to pressed state
}
}
}
非常感谢任何帮助。我确实仔细检查过这个问题的答案。如果我仍然缺少相关细节,请告诉我,我会回复。
如果我正确理解你的问题,你想创建一个 public Keycode 变量,它是这样完成的:
public Keycode theKey = Keycode.None;
您可以像这样使用它:
if (Input.GetKeyDown(theKey))
{
//Do code
}