c# - 自定义键码变量

标签 c# variables unity-game-engine keypress

我正在创建一个虚拟启动板,并且我将把这个脚本附加到每个按钮,但我需要它是模块化的,这样我就不需要 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/

相关文章:

python - Python : Variable name is 'syntax error'

mysql - 构建网络播放器应用程序时如何避免 "dll is not allowed to be included or could not be found"错误?

node.js - 在 HttpsCallableResult 完成之前等待所有回调返回

c# - Silverlight:将子窗口重新定位到中心

我们不能使用与函数相同的变量名吗?

c# - 使用 javascript 创建 .net md5

c - 在循环过程中更改变量时出现问题

c# - 交换 3 个六边形邻居的引用

c# - POST 期间的双重等待操作

c# - 如何更改为另一个 View Controller (解释清楚)