【发布时间】:2020-05-09 10:32:53
【问题描述】:
在我正在制作的游戏中,我想制作一个快速时间事件,如果按下了错误的按钮,它可能会失败。但我不知道我怎么能做到,所以每个按钮都会触发它。在 void Update 中,除了为每个按钮制作 Keycode 之外,肯定还有其他选择,对吧?
【问题讨论】:
在我正在制作的游戏中,我想制作一个快速时间事件,如果按下了错误的按钮,它可能会失败。但我不知道我怎么能做到,所以每个按钮都会触发它。在 void Update 中,除了为每个按钮制作 Keycode 之外,肯定还有其他选择,对吧?
【问题讨论】:
所以如果我理解正确的话,你希望每个按钮都以某种方式起作用,除了一个。我们称之为“错误按钮”。然后,您可以通过它的名称或标签来区分这个按钮。
如果错误按钮的标签不同,您可以像这样比较 Update() 中的标签:
private string WrongButtonTag = "wrongButton";
void Update()
{
if(Input.GetButtonDown("yourKey") && !this.CompareTag(WrongButtonTag))
{
//add your code logic for correct buttons here
}
}
您可能需要事先分配标签,例如通过调用此函数:
private void AssignWrongButtonTag()
{
this.tag = WrongButtonTag;
}
编辑:
评论之后,我认为这是要走的路:您必须将所有按钮放入一个列表中,然后在 Update 函数中使用此代码:
public List<GameObject> buttonList = new List<GameObject>();
void Update()
{
if(Input.GetButtonDown(<your input key here>)
{
foreach(GameObject button in buttonList)
{
if(!this.CompareTag(WrongButtonTag))
{
continue;
}
//add your code logic for correct buttons here
}
}
}
【讨论】: