unity2019中虚拟按钮的使用

版本:unity2019.4.12f1,Visual Studio2019
1.window栏加入Vuforia Engine AR,此时可以正常使用AR相机了。
在这里插入图片描述
2.利用vuforia码,
在这里插入图片描述
建立一个空物体showcube,
然后在空物体上加入Virtual Button Behaviour(系统自带函数),加入Turn Off Behaviour(系统自带函数)
在这里插入图片描述
接下来是ShowCube设置,即完成显示和不显示cube

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class ShowCube : MonoBehaviour, IVirtualButtonEventHandler
{

    public GameObject cube;
    public GameObject sphere;

    // Use this for initialization
    void Start()
    {
        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
        for (int i = 0; i < vbs.Length; i++)
        {
            vbs[i].RegisterEventHandler(this);
        }

        cube.SetActive(false);
        sphere.SetActive(false);
        print("cube关闭");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void OnButtonPressed(VirtualButtonBehaviour vb)
    {
        /*switch(vb.VirtualButtonName)
        {
            case "showcube":
                cube.SetActive(true);
                break;
            case "showsphere":
                sphere.SetActive(true);
                break;
            default:
                break;
        }*/
        print("daoci");
        switch (vb.VirtualButtonName)
        {
            case "showcube":
                cube.SetActive(true);
                cube.GetComponent<MeshRenderer>().material.color = Color.blue;
                break;
            case "showsphere":
                sphere.SetActive(true);
                sphere.GetComponent<MeshRenderer>().material.color = Color.red;
                break;
            default:
                break;
        }


    }
    public void OnButtonReleased(VirtualButtonBehaviour vb)
    {

    }
}

即可完成设置。
代码解析:using Vuforia;加入此头文件。
public class ShowCube : MonoBehaviour, IVirtualButtonEventHandler
加入IVirtualButtonEventHandler这个。
public void OnButtonPressed(VirtualButtonBehaviour vb)可以在遮挡时自动调用此函数。
在这里插入图片描述
显示出虚拟按钮所在位置。
最终显示结果:
在这里插入图片描述