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

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

  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
不知道为啥横过来了....

发布了57 篇原创文章 · 获赞 22 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Mediary/article/details/102936113