高通AR简单的虚拟按钮使用

本文介绍如何在Unity中结合Vuforia,通过创建虚拟按钮来控制场景中的对象显示与隐藏。通过代码实现,当点击特定的虚拟按钮时,可以分别显示或隐藏红色的Cube或黄色的Sphere。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

识别图片上一篇已经说过了

  1. 添加虚拟按钮在这里插入图片描述
  2. 改名字的话,如图在这里插入图片描述
  3. 这里设置两个虚拟按钮,如下
    丑是丑了点
  4. 编写代码,把脚本放在ImageTarget上即可
using UnityEngine;
using Vuforia;

public class VirtualButtonTest : MonoBehaviour, IVirtualButtonEventHandler
{
    GameObject cube, sphere;
    void Start()
    {
        //获取VirtualButton Behaviour组件
        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
        for (int i = 0; i < vbs.Length; ++i)
        {
            //遍历所有组件
            vbs[i].RegisterEventHandler(this);    //对该脚本进行注册
            Debug.Log("注册组件的名字" + vbs[i].name);
        }

        cube = transform.Find("Cube").gameObject;
        sphere = transform.Find("Sphere").gameObject;
        // 开始把物体隐藏
        cube.SetActive(false);
        sphere.SetActive(false);
    }

    public void OnButtonPressed(VirtualButtonBehaviour vb)
    {
        switch (vb.VirtualButtonName)
        {
            case "showCube":
                Debug.Log("ShowCube");
                cube.SetActive(true);
                break;
            case "showSphere":
                Debug.Log("ShowSphere");
                sphere.SetActive(true);
                break;
        }
    }

    public void OnButtonReleased(VirtualButtonBehaviour vb)
    {
        switch (vb.VirtualButtonName)
        {
            case "showCube":
                Debug.Log("ShowCube");
                cube.SetActive(false);
                break;
            case "showSphere":
                Debug.Log("ShowSphere");
                sphere.SetActive(false);
                break;
        }
    }
}

效果大概是这样,点左边出现红色的Cube,右边出现黄色的Sphere
不知道为啥横过来了....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值