Vuforia--VirtualButton的使用
程序员文章站
2022-04-28 21:27:22
...
一,在官网上创建Key和Target就不说了,将创建的Imarget资源包导入Unity中,设置 Key 值,勾选**程序集,设置图片Type 跟ImageTarget案例一样
二,将prefab ARCamera 和 ImageTarget拖入Hierarchy中 并将VirtualButton拖入ImageTarget作为子物体 修改名字为Button1
三,在Button1上 将Name属性改为Button1
四,在ImageTarget上的基本如图
四,其中可对VirtualButtonEventHandler修改 实现效果(也可以自己创建一个脚本)
using UnityEngine;
using System.Collections.Generic;
using Vuforia;
public class VirtualButtonEventHandler : MonoBehaviour,
IVirtualButtonEventHandler
{
public Material m_VirtualButtonMaterial;
public Transform cube;
void Start()
{
// Register with the virtual buttons TrackableBehaviour and set the material
VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
for (int i = 0; i < vbs.Length; ++i)
{
vbs[i].RegisterEventHandler(this);
if (m_VirtualButtonMaterial != null)
{
vbs[i].GetComponent<MeshRenderer>().sharedMaterial = m_VirtualButtonMaterial;
}
}
}
//当按钮按下时
public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
{
cube.GetComponent<Renderer>().material.color = Color.red;
print(vb.VirtualButtonName);
}
//当按钮释放时
public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
{
cube.GetComponent<Renderer>().material.color = Color.blue;
print(vb.VirtualButtonName);
}
}