目录
📢 本节效果演示
🟥 虚拟按键实现方式
1、Unity新建ImageTarget,在其下方新建VirtualButton
2、VirtualButton上写上该button的名字“showSphere”
后面我们会用代码统一给这些button注册事件。那如何判断这些button哪个是哪个,就通过自定义的他们的名字的不同。
3、在InageTarget下新建cube和sphere,并摆好这些虚拟button在ImageTarget上的位置
4、新建脚本如下,把这个脚本放在ImageTarget上
using UnityEngine;
using Vuforia;
public class VirtualBtnController : MonoBehaviour, IVirtualButtonEventHandler
{
public GameObject cube;
public GameObject sphere;
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);
}
//继承了IVirtualButtonEventHandler的方法
public void OnButtonPressed(VirtualButtonBehaviour vb)
{
switch (vb.VirtualButtonName)
{
case "showCube":
cube.SetActive(true);
break;
case "showSphere":
sphere.SetActive(true);
break;
}
}
//继承了IVirtualButtonEventHandler的方法
public void OnButtonReleased(VirtualButtonBehaviour vb)
{
switch (vb.VirtualButtonName)
{
case "showCube":
cube.SetActive(false);
break;
case "showSphere":
sphere.SetActive(false);
break;
}
}
}
5、注意:
最好的识别方法是手遮挡的范围越大,识别到点击的能力越好。
button最好不要放在四个角
大家还有什么问题,欢迎在下方留言!
如果你有 技术的问题 或 项目开发
都可以加下方联系方式
和我聊一聊你的故事🧡