Unity 2D和3D对象的点击
发表于2018-11-09
这篇文章给大家分别介绍下如何判断2D和3D对象的点击,想了解2D和3D对象的点击有何不同就来看看吧。
1、2D对象响应点击
(1)Canvas对象上有Graphic Raycaster
(2)被点击的对象加上Image,并勾选Raycast Target
(3)场景里有唯一对象挂上EventSystem和StandaloneInputModule
(4)被点击的对象加上Button,然后调用:
GetComponent<Button>().onClick.AddListener(() => { Debug.Log("button click"); });
2、3D对象响应点击
(1)看3d对象的camera上挂上Physics Raycaster
(2)被点击对象上加上Box Collider
(3)场景里有唯一对象挂上EventSystem和StandaloneInputModule
(4)挂上点击脚本,内容如下:
public class SceneObjClick : EventTrigger { public override void OnPointerClick(PointerEventData eventData) { base.OnPointerClick(eventData); Debug.Log("Scene Obj Click"); } public override void OnDrag(PointerEventData eventData) { base.OnDrag(eventData); Debug.Log("Scene Obj Drag"); } }
其中EventTrigger.cs是unity自身的脚本,内如如下:
namespace UnityEngine.EventSystems { // // 摘要: // /// // Receives events from the EventSystem and calls registered functions for each // event. // /// [AddComponentMenu("Event/Event Trigger")] public class EventTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IInitializePotentialDragHandler, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler, IScrollHandler, IUpdateSelectedHandler, ISelectHandler, IDeselectHandler, IMoveHandler, ISubmitHandler, ICancelHandler, IEventSystemHandler { // // 摘要: // /// // All the functions registered in this EventTrigger (deprecated). // /// [Obsolete("Please use triggers instead (UnityUpgradable) -> triggers", true)] public List<Entry> delegates; protected EventTrigger(); // // 摘要: // /// // All the functions registered in this EventTrigger. // /// public List<Entry> triggers { get; set; } // // 摘要: // /// // Called before a drag is started. // /// // // 参数: // eventData: // Current event data. public virtual void OnBeginDrag(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when a Cancel event occurs. // /// // // 参数: // eventData: // Current event data. public virtual void OnCancel(BaseEventData eventData); // // 摘要: // /// // Called by the EventSystem when a new object is being selected. // /// // // 参数: // eventData: // Current event data. public virtual void OnDeselect(BaseEventData eventData); // // 摘要: // /// // Called by the EventSystem every time the pointer is moved during dragging. // /// // // 参数: // eventData: // Current event data. public virtual void OnDrag(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when an object accepts a drop. // /// // // 参数: // eventData: // Current event data. public virtual void OnDrop(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem once dragging ends. // /// // // 参数: // eventData: // Current event data. public virtual void OnEndDrag(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when a drag has been found, but before it is valid // to begin the drag. // /// // // 参数: // eventData: // Current event data. public virtual void OnInitializePotentialDrag(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when a Move event occurs. // /// // // 参数: // eventData: // Current event data. public virtual void OnMove(AxisEventData eventData); // // 摘要: // /// // Called by the EventSystem when a Click event occurs. // /// // // 参数: // eventData: // Current event data. public virtual void OnPointerClick(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when a PointerDown event occurs. // /// // // 参数: // eventData: // Current event data. public virtual void OnPointerDown(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when the pointer enters the object associated with // this EventTrigger. // /// // // 参数: // eventData: // Current event data. public virtual void OnPointerEnter(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when the pointer exits the object associated with this // EventTrigger. // /// // // 参数: // eventData: // Current event data. public virtual void OnPointerExit(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when a PointerUp event occurs. // /// // // 参数: // eventData: // Current event data. public virtual void OnPointerUp(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when a Scroll event occurs. // /// // // 参数: // eventData: // Current event data. public virtual void OnScroll(PointerEventData eventData); // // 摘要: // /// // Called by the EventSystem when a Select event occurs. // /// // // 参数: // eventData: // Current event data. public virtual void OnSelect(BaseEventData eventData); // // 摘要: // /// // Called by the EventSystem when a Submit event occurs. // /// // // 参数: // eventData: // Current event data. public virtual void OnSubmit(BaseEventData eventData); // // 摘要: // /// // Called by the EventSystem when the object associated with this EventTrigger is // updated. // /// // // 参数: // eventData: // Current event data. public virtual void OnUpdateSelected(BaseEventData eventData); // // 摘要: // /// // An Entry in the EventSystem delegates list. // /// public class Entry { // // 摘要: // /// // The desired TriggerEvent to be Invoked. // /// public TriggerEvent callback; // // 摘要: // /// // What type of event is the associated callback listening for. // /// public EventTriggerType eventID; public Entry(); } // // 摘要: // /// // UnityEvent class for Triggers. // /// public class TriggerEvent : UnityEvent<BaseEventData> { public TriggerEvent(); } } }
3、点击原理
底层调用Physics.RaycastAll,从摄像机发出一条射线,拿到所有碰撞到的物体,进行排序,获取第一个碰撞物体,然后获取该物体上得事件函数,进行调用。事件函数有EventTrigger继承的那些接口。