想要在3D物体上实现全套事件监听处理:
- OnMouse系列
- OnTrigger系列
- OnPointer系列
- OnDrag系列
2、3D物体上挂在的脚本根据需要实现以下接口中的部分或全部:
IPointerEnterHandler,
IPointerDownHandler,
IPointerUpHandler,
IPointerExitHandler,
IPointerClickHandler,
IInitializePotentialDragHandler,
IBeginDragHandler,
IEndDragHandler,
IDragHandler,
IDropHandler,
IScrollHandler
https://docs.unity3d.com/ScriptReference/EventSystems.IBeginDragHandler.html
除了默认的事件处理接口之外,也可以使用射线检测
Ray ray;
RaycastHit rayhit;
void Update()
{
if (Input.GetMouseButtonUp(0)) {
ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out rayhit, 10f)) {
Debug.Log(rayhit.collider.gameObject.name);
}
}
}
延伸: http://www.xuanyusong.com/archives/4241