unity实现3D物体上的事件监听处理

来源:互联网 发布:js在线测试 编辑:程序博客网 时间:2024/05/02 02:01

想要在3D物体上实现全套事件监听处理:

  • OnMouse系列
  • OnTrigger系列
  • OnPointer系列
  • OnDrag系列

1、在相机中添加Physics Raycaster组件

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




0 0
原创粉丝点击