美文网首页
Unity之高通vuforia虚拟按钮

Unity之高通vuforia虚拟按钮

作者: HMY轩园 | 来源:发表于2017-05-08 16:12 被阅读0次

高通插件导入Unity后,可以在路径Vuforia/Prefabs/VirtualButton下找到虚拟按钮的预制件,我们直接拿来使用即可,自己编写脚本代码继承IVirtualButtonEventHandler接口即可,同时需要实现接口里的OnButtonPressed和OnButtonRelease方法。

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

public class VurtualButton : MonoBehaviour,IVirtualButtonEventHandler{

    /// <summary>
    /// 虚拟按钮按下
    /// </summary>
    /// <param name="vb"></param>
    public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
    {
        throw new NotImplementedException();
    }
/// <summary>
/// 虚拟按钮没按下
/// </summary>
/// <param name="vb"></param>
    public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
    {
        throw new NotImplementedException();
    }
    // Use this for initialization
    void Start () {
    
    }
    // Update is called once per frame
    void Update () {
    
    }
}

当然还需要将虚拟按钮注册到事件系统中,需要早Start方法里完成。

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

public class VurtualButton : MonoBehaviour,IVirtualButtonEventHandler{
    public GameObject cube;
    public GameObject sphere;
    private Material cube_Material;
    private Material sphere_Material;
    /// <summary>
    /// 虚拟按钮按下
    /// </summary>
    /// <param name="vb"></param>
    public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
    {
        switch (vb.VirtualButtonName)
        {
            case "11":
                cube.SetActive(true);
                cube_Material.color = Color.blue;         
                break;
            case "12":
              
                sphere.SetActive(true);
                sphere_Material.color = Color.Lerp(Color.red, Color.green, Time.time);
                break;
            default:
                break;
        }
    }
/// <summary>
/// 虚拟按钮没按下
/// </summary>
/// <param name="vb"></param>
    public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
    {
        switch (vb.VirtualButtonName)
        {
            case "11":
                cube_Material.color = Color.red;
                cube.SetActive(false);
                break;
            case "12":
                sphere_Material.color = Color.white;
                sphere.SetActive(false);
                break;
            default:
                break;
        }
       
    }
    // Use this for initialization
    void Start () {
        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();

        for (int i = 0; i < vbs.Length; i++)
        {
            //在虚拟按钮中注册TrackableBehaviour事件
            vbs[i].RegisterEventHandler(this);
        }

        cube.SetActive(false);
        sphere.SetActive(false);
        cube_Material = cube.GetComponent<MeshRenderer>().material;
        sphere_Material = sphere.GetComponent<MeshRenderer>().material;
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}


![XVNZQG1)]S_VRMN5WS5H6UU.png](https://img.haomeiwen.com/i3427975/681244f986f25161.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
虚拟按钮名字设置:
![ON$@0YH_]LMB100LCUA5HO.png

相关文章

网友评论

      本文标题:Unity之高通vuforia虚拟按钮

      本文链接:https://www.haomeiwen.com/subject/owamtxtx.html