识别图片上一篇已经说过了
- 添加虚拟按钮
- 改名字的话,如图
- 这里设置两个虚拟按钮,如下
- 编写代码,把脚本放在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