简体   繁体   English

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

[英]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.

相关问题 我如何停用阵列中除 ONE 之外的所有游戏对象? - How do I deactivate all game objects in an array except ONE? 如何停用列表中除当前正在使用的相机之外的所有相机? - how do I deactivate all cameras in a list except for the one that is currently in use? 云识别中的Vuforia虚拟按钮 - Vuforia virtual buttons in Cloud Recognition 如何修改文本框控件,以便按退格键删除整个单词,而不是一次删除一个字符? - How do I modify a textbox control so that pressing backspace deletes whole words instead of just one character at a time? 按下按钮后如何删除空间 - how to remove space after pressing buttons 通过在Unity3d中按Vuforia虚拟按钮无法旋转多维数据集 - The cube does not rotate by pressing Vuforia virtual button in Unity3d (C#)如何在按下按钮时显示新表格窗口? 我可以将其他按钮和文本框拖到其中的一个 - (C#)How do I, upon pressing a button, Make a new form window appear? One that i can drag other buttons and text boxes onto 按下提交按钮后,如何重定向到下一页? - How do I redirect to the next page after pressing the submit button? 如何在按下文本框中的Enter键后按下键盘后放下虚拟键盘 - How to drop the virtual keyboard after keydown event on pressing enter in textbox 如何通过按一个来选中所有复选框? - How to check all checkbox by pressing one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM