[英]How do I deactivate all the virtual buttons in Vuforia after pressing just one?
Good afternoon!下午好!
I'm trying to make a simple AR quiz game that requires virtual buttons to answer.我正在尝试制作一个需要虚拟按钮来回答的简单 AR 问答游戏。 The game goes on if the correct button is pressed.如果按下正确的按钮,游戏就会继续。
The question is: how do I deactivate all the virtual buttons when a virtual button is pressed ?问题是:按下虚拟按钮时如何停用所有虚拟按钮? I've looked at nearly every topic regarding this, but can't figure it out how to make this work.我已经查看了几乎所有与此相关的主题,但无法弄清楚如何进行这项工作。 At this point I can press all the buttons but only once.此时我可以按下所有按钮,但只能按下一次。
Thanks in advance (and sorry for my bad English)!提前致谢(并为我的英语不好而感到抱歉)!
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!");
}
}
}
Thank you for your reply!感谢你的回复! For some weird reason it doesn't just work.出于某种奇怪的原因,它不仅有效。
I added Debug.Log("DeactivateButtons is called!");
我添加了Debug.Log("DeactivateButtons is called!");
and this method wasn't called at all.并且根本没有调用此方法。
Looks like you are disabling only one button when pressed whereas others remain active.看起来您在按下时仅禁用一个按钮,而其他按钮保持活动状态。 You can add a function to check if any of the buttons are pressed so that you can deactivate them.您可以添加一个功能来检查是否按下了任何按钮,以便您可以停用它们。 Or since you are finding the name of the button when pressed, then you can probably create 3 variables such as:或者,由于您在按下按钮时会找到按钮的名称,因此您可以创建 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;
}
Now to integrate the above to your code, perhaps under OnButtonPressed()
function;现在将上述内容集成到您的代码中,也许在OnButtonPressed()
函数下; you can add:你可以加:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.