《小白學Unity3D》實現處理滑鼠點選GameObject
阿新 • • 發佈:2021-11-14
實現物體點選事件
通過對攝像機掛載Physics Raycaster元件,對滑鼠點選位置進行射線檢測用以獲得物體的點選,同時在Hierarchy中建立EventSystem GameObject用來接收和處理滑鼠事件。過程如下:
- 在Hierarchy檢視中建立EventSystem[Assets/Create/UI/EventSystem]
- 在Camera上掛載Physics Ray Caster元件
- 在要處理滑鼠事件的物體上掛載有如下內容的指令碼
/* * IPointerClick: 這個介面是處理滑鼠點選事件的核心,提供給EventSystem的回撥 */ public class MouseEventHandle : MonoBehanve, IPointerClickHandle { void OnPointerClick(PointerEventData evendate) { // TODO: } }
EventSystem支援的介面列表
介面名 | 描述 |
---|---|
IPointerClickHandler | 接收OnPointerClick的回撥,可以檢測單擊[包括點選和抬起,在抬起時觸發] |
IPointerDownHandler | 接收OnPointerDown回撥, 滑鼠按鍵點選時檢測 |
IPointerUpHandler | 接收OnPointerUp回撥,滑鼠按鍵抬起時檢測 |
IPointerEnterHandler | 接收OnPointerEnter回撥, 滑鼠移入時檢測 |
IPointerExitHandler | 接收OnPointerExit回撥, 滑鼠移出時檢測 |