我正在创建一个虚拟启动板,并且我将把这个脚本附加到每个按钮,但我需要它是模块化的,这样我就不需要 64 个不同的脚本来完成完全相同的事情。
我的问题是我不确定如何获取 KeyCodes 公共(public)变量。因此,如果我声明一个名为“theKey”的公共(public)变量:
if (Input.GetKeyDown(KeyCode.theKey))
{ //play sound etc.
}
与此类似的东西,以便在统一中我可以使用检查器插入该脚本所附加的按钮的热键。
KeyCode.String 不起作用,我需要完整的 KeyCodes,如 .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
}
关于c# - 自定义键码变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34775015/