VR和Unity UGUI共用一个事件系统
VR点击按钮通常是通过射线检测的方式实现,但是有时候我们又想既能鼠标点击又能VR射线点击应该怎样做呢,下面就简单的介绍一下Button的操作。
其实我们可以在UGUI 的button上挂上碰撞体,然后检测到射线点击的时候,去执行UGUI的点击事件,这样我们只需要添加碰撞体,事件回调任然用UGUI的事件监听,下面是示例代码能够做到一个抛砖引玉的作用。
PointerEventData data = new PointerEventData(EventSystem.current);
private void Update()
{
Ray ray = new Ray(transform.position,transform.forward);
Debug.DrawLine(transform.position, transform.position+ transform.forward*1000,Color.red);
RaycastHit hit;
if (Physics.Raycast(ray,out hit,1000)) {
//Debug.Log("123");
GameObject currentObj = hit.transform.gameObject;
if (currentObj!=null) {
ExecuteEvents.Execute(currentObj,data, ExecuteEvents.pointerClickHandler);
//Debug.Log("zhixing");
}
}
}