Unity场景中如何让3d物体响应拖拽触摸等EventSystem事件

在unity中,有这么一些接口

	IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerMoveHandler, IPointerUpHandler, IPointerDownHandler, IInitializePotentialDragHandler, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler, IScrollHandler, IUpdateSelectedHandler, ISelectHandler, IDeselectHandler, IMoveHandler, ISubmitHandler, ICancelHandler

这些接口可以响应一些触摸事件,一般这些是ui控件挂载一个实现了这些接口的脚本就可以响应的,现在想让场景中的3d物体也能响应这些事件该怎么做呢?

一.创建3d物体,并挂载实现了这些接口的脚本

	public class Supported : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerMoveHandler, IPointerUpHandler, IPointerDownHandler, IInitializePotentialDragHandler, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler, IScrollHandler, IUpdateSelectedHandler, ISelectHandler, IDeselectHandler, IMoveHandler, ISubmitHandler, ICancelHandler
{
public void OnBeginDrag(PointerEventData eventData)
{
    Debug.Log("OnBeginDrag");
}

public void OnCancel(BaseEventData eventData)
{
    Debug.Log("OnCancel");
}

public void OnDeselect(BaseEventData eventData)
{
    Debug.Log("OnDeselect");
}

public void OnDrag(PointerEventData eventData)
{
    Debug.Log("OnDrag");
}

public void OnDrop(PointerEventData eventData)
{
    Debug.Log("OnDrop");
}
}

二.创建EventSystem
Unity场景中如何让3d物体响应拖拽触摸等EventSystem事件_第1张图片
三.camera挂载Graphic Raycaster 和 Physics Raycaster

Unity场景中如何让3d物体响应拖拽触摸等EventSystem事件_第2张图片

运行游戏,鼠标放在物体上就能触发OnPosinterMove等事件

Unity场景中如何让3d物体响应拖拽触摸等EventSystem事件_第3张图片

你可能感兴趣的:(unity,unity,3d,游戏引擎)