如何仅在按下一个按钮后停用 Vuforia 中的所有虚拟按钮?



下午好!

我正在尝试制作一个简单的AR问答游戏,需要虚拟按钮来回答。如果按下正确的按钮,游戏将继续。

问题是:按下虚拟按钮时如何停用所有虚拟按钮? 我几乎看过每个关于这个的主题,但无法弄清楚如何做到这一点。 此时,我可以按下所有按钮,但只能按下一次。

提前感谢(对不起我的英语不好(!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class PushToDebug : MonoBehaviour, IVirtualButtonEventHandler
{
public string vbName;
public AudioSource audi;

void Start()
{
//Register with the virtual buttons TrackableBehaviour
VirtualButtonBehaviour[] vrb = GetComponentsInChildren<VirtualButtonBehaviour>();
audi = GetComponent<AudioSource>();
for (int i = 0; i < vrb.Length; i++)
vrb[i].RegisterEventHandler(this);
}
public void OnButtonPressed(VirtualButtonBehaviour vb)
{
vbName = vb.VirtualButtonName;
if (vbName == "VB1")
{
Debug.Log("Button 1 is pressed!");
audi.Play();
vb.GetComponent<VirtualButtonBehaviour>().enabled = false;
}
else if (vbName == "VB2")
{
Debug.Log("Button 2 is pressed!");
audi.Play();
vb.GetComponent<VirtualButtonBehaviour>().enabled = false;
}
else 
{
Debug.Log("Button 3 is pressed!");
audi.Play();
vb.GetComponent<VirtualButtonBehaviour>().enabled = false;
}
}

public void OnButtonReleased(VirtualButtonBehaviour vb)
{
if (vbName == "VB1")
{
Debug.Log("Button 1 is released!");
}
else if (vbName == "VB2")
{
Debug.Log("Button 2 is released!");
}
else
{
Debug.Log("Button 3 is released!");
}
}
}

感谢您的回复! 出于某种奇怪的原因,它不仅有效。

我添加了Debug.Log("DeactivateButtons is called!");,但根本没有调用此方法。

看起来您在按下时只禁用了一个按钮,而其他按钮仍处于活动状态。您可以添加一个功能来检查是否按下了任何按钮,以便您可以停用它们。 或者,由于您在按下按钮时会找到按钮的名称,因此您可能可以创建 3 个变量,例如:

var VB1 = GameObject.Find("VB1");
var VB2 = GameObject.Find("VB2");
var VB3 = GameObject.Find("VB3");
if(VB1 != null && VB2 != null && VB3 != null){
VB1.GetComponent<VirtualButtonBehavior>().enabled = false;
VB2.GetComponent<VirtualButtonBehavior>().enabled = false;
VB3.GetComponent<VirtualButtonBehavior>().enabled = false;
}

现在要将上述内容集成到您的代码中,也许在OnButtonPressed()函数下;您可以添加:

public void OnButtonPressed(VirtualButtonBehaviour vb)
{
vbName = vb.VirtualButtonName;
if (vbName == "VB1")
{
Debug.Log("Button 1 is pressed!");
audi.Play();
vb.GetComponent<VirtualButtonBehaviour>().enabled = false;
DeactivateButtons();
}
else if (vbName == "VB2")
{
Debug.Log("Button 2 is pressed!");
audi.Play();
vb.GetComponent<VirtualButtonBehaviour>().enabled = false;
DeactivateButtons();
}
else 
{
Debug.Log("Button 3 is pressed!");
audi.Play();
vb.GetComponent<VirtualButtonBehaviour>().enabled = false;
DeactivateButtons();
}
}
public void DeactivateButtons()
{
var VB1 = GameObject.Find("VB1");
var VB2 = GameObject.Find("VB2");
var VB3 = GameObject.Find("VB3");
if(VB1 != null && VB2 != null && VB3 != null){
VB1.GetComponent<VirtualButtonBehavior>().enabled = false;
VB2.GetComponent<VirtualButtonBehavior>().enabled = false;
VB3.GetComponent<VirtualButtonBehavior>().enabled = false;
}
}

最新更新