Vuforia+Unity最简单的虚拟按钮实现

《Vuforia+Unity最简单的虚拟按钮实现》

《Vuforia+Unity最简单的虚拟按钮实现》

大致效果如上——用手指在空中交互,点击Show Sphere,出现粉色球体,点击Show Cube显示立方体,当然如果你有更酷炫的模型,可以让它们看起来更高大上一点。

开船

首先将Vuforia插件包和识别图导入Vuforia,
添加ARCarmera、ImageTarget,
删除Camera,
在ARCamera中添加License Key,勾选Load __ Dataset,
在ImageTarget下选中你的识别图(配置DataBase和Image Target),

这些步骤在之前文章中写过,现在就简单回顾一下了。

继续,右击ImageTarget,添加一个立方体和一个球体,将这两个模型摆到合适的位置。
找到Assert – Vuforia – Perfabs里的Virtual Button,拖动到ImageTarget下,改名为showCube及showSphere。

《Vuforia+Unity最简单的虚拟按钮实现》

这时我们可以为它们添加一点材质来区分。
在Assert空白处右击,Create – Material,更改材质名称并选择一个你喜欢的颜色,最后拖动材质到对应的虚拟按钮和模型上,就实现材质的添加啦~

《Vuforia+Unity最简单的虚拟按钮实现》

再给虚拟按钮添加简单的形状和文字——右击添加Plane,调整位置大小及方向,再在Plane下添加3D Text,更改内容及字体大小颜色。

《Vuforia+Unity最简单的虚拟按钮实现》

以上都是简单的图形按钮创建,最后给ImageTarget添加脚本,赋予虚拟按钮点击触发事件。

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

public class VirtualButtonTest : MonoBehaviour, IVirtualButtonEventHandler {  //实现IVirtualButtonEventHandler接口,该接口包含OnButtonPressed和OnButtonReleased两个方法

    private GameObject cube;
    private GameObject sphere;

    // Use this for initialization
    void Start () {

        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour> ();   //获取VirtualButton Behaviour组件
        for (int i = 0; i < vbs.Length; ++i) {     //遍历所有组件
            vbs [i].RegisterEventHandler (this);    //对该脚本进行注册
        }//

        cube = transform.Find("Cube").gameObject;
        sphere = transform.Find ("Sphere").gameObject;

        cube.SetActive (false);
        sphere.SetActive (false);

    }

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

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

虚拟按钮就是一块透明的区域,只要摄像头检测到这个区域一定范围被遮挡,就会触发事件,范围大小主要由虚拟按钮的Sensitivity Setting决定(Virtual Button Behaviour脚本下有设置窗口)。

此外还有一个类似的简单功能,在虚拟按钮未触发时显示的是球体,触发时显示立方体,离开后继续显示球体。这个可以作为练习,具体代码贴在下面了:

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

public class VirtualButtonTest2 : MonoBehaviour, IVirtualButtonEventHandler {

    private GameObject virButton;
    private GameObject cube;
    private GameObject sphere;

    // Use this for initialization
    void Start () {

        virButton = GameObject.Find ("Trans");
        virButton.GetComponent<VirtualButtonBehaviour> ().RegisterEventHandler (this);
        cube = transform.Find ("Cube").gameObject;
        sphere = transform.Find ("Sphere").gameObject;
        sphere.SetActive (true);
        cube.SetActive (false);

    }
    // Update is called once per frame
    public void OnButtonPressed (VirtualButtonAbstractBehaviour vb){
        switch (vb.VirtualButtonName) {
        case "Trans":
            sphere.SetActive (false);
            cube.SetActive (true);
            break;
        default:
            break;
        }
        Debug.Log ("OnButtonPressed");
    }
    public void OnButtonReleased (VirtualButtonAbstractBehaviour vb){
        sphere.SetActive (true);
        cube.SetActive (false);
        Debug.Log ("OnButtonReleased");
    }
}

《Vuforia+Unity最简单的虚拟按钮实现》

《Vuforia+Unity最简单的虚拟按钮实现》

测试时发现,原来图片尺寸对摄像头识别效率影响很大:图片像素越高,越需要摄像头近距离识别;像素越低,需要把摄像头拉远了才能成功识别。hhh……

本人代码基础薄弱,实现的都是简单的功能,希望以后能作出越来越高大上的作品吧!加油加油!

    原文作者:早起的蝉儿有鸟吃_Mum
    原文地址: https://www.jianshu.com/p/1c98d5208c00
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞