【问题标题】:How do I deactivate all the virtual buttons in Vuforia after pressing just one?如何在仅按一个后停用 Vuforia 中的所有虚拟按钮?
【发布时间】:2019-11-26 15:19:37
【问题描述】:

下午好!

我正在尝试制作一个需要虚拟按钮来回答的简单 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!");,这个方法根本没有被调用。

【问题讨论】:

    标签: c# unity3d vuforia


    【解决方案1】:

    看起来您在按下时只禁用了一个按钮,而其他按钮仍然处于活动状态。您可以添加一个功能来检查是否按下了任何按钮,以便您可以停用它们。 或者由于您在按下时找到按钮的名称,那么您可能可以创建 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;
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多