上一章說了一些Unity不規(guī)則圖片點擊息罗。這一次就說一說常用的UI事件。
EventTrigger是一個可以實現(xiàn)很多功能的東東客年。
添加方法有兩種烤黍,第一就是在Inspector中添加EventTrigger
第二種就是寫代碼了。身為一個獨游開發(fā)者寫代碼才是我的特長擅耽。好吧活孩!我不喜歡廢話,下面是干貨秫筏。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Test: EventTrigger
{
? ? public override void OnBeginDrag(PointerEventData data)
? ? {
? ? ? ? Debug.Log("處理開始拖動時要做什么事.");
? ? }
? ? public override void OnCancel(BaseEventData data)
? ? {
? ? ? ? Debug.Log("OnCancel called.");
? ? }
? ? public override void OnDeselect(BaseEventData data)
? ? {
? ? ? ? Debug.Log("OnDeselect called.");
? ? }
? ? public override void OnDrag(PointerEventData data)
? ? {
? ? ? ? Debug.Log("理拖動過程中要做什么事.");
? ? }
? ? public override void OnDrop(PointerEventData data)
? ? {
? ? ? ? Debug.Log("OnDrop called.");
? ? }
? ? public override void OnEndDrag(PointerEventData data)
? ? {
? ? ? ? Debug.Log("理拖動結(jié)束要做什么事.");
? ? }
? ? public override void OnInitializePotentialDrag(PointerEventData data)
? ? {
? ? ? ? Debug.Log("OnInitializePotentialDrag called.");
? ? }
? ? public override void OnMove(AxisEventData data)
? ? {
? ? ? ? Debug.Log("OnMove called.");
? ? }
? ? public override void OnPointerClick(PointerEventData data)
? ? {
? ? ? ? Debug.Log("OnPointerClick called.");
? ? ? ? Debug.Log("在圖片內(nèi)取消滑鼠點擊");
? ? }
? ? public override void OnPointerDown(PointerEventData data)
? ? {
? ? ? ? Debug.Log("OnPointerDown called.");
? ? ? ? Debug.Log("滑鼠點擊");
? ? }
? ? public override void OnPointerEnter(PointerEventData data)
? ? {
? ? ? ? Debug.Log("OnPointerEnter called.");
? ? ? ? Debug.Log("滑鼠移入");
? ? }
? ? public override void OnPointerExit(PointerEventData data)
? ? {
? ? ? ? Debug.Log("OnPointerExit called.");
? ? ? ? Debug.Log("滑鼠移出");
? ? }
? ? public override void OnPointerUp(PointerEventData data)
? ? {
? ? ? ? Debug.Log("OnPointerUp called.");
? ? ? ? Debug.Log("取消滑鼠點擊");
? ? }
? ? public override void OnScroll(PointerEventData data)
? ? {
? ? ? ? Debug.Log("OnScroll called.");
? ? }
? ? public override void OnSelect(BaseEventData data)
? ? {
? ? ? ? Debug.Log("OnSelect called.");
? ? }
? ? public override void OnSubmit(BaseEventData data)
? ? {
? ? ? ? Debug.Log("OnSubmit called.");
? ? }
? ? public override void OnUpdateSelected(BaseEventData data)
? ? {
? ? ? ? Debug.Log("OnUpdateSelected called.");
? ? }
}
代碼不是只有這一種寫發(fā)诱鞠,另一種就是這個
using UnityEngine;
using UnityEngine.EventSystems;
public class ChoiceCart : MonoBehaviour,IPointerEnterHandler,IPointerClickHandler {
? ? // Use this for initialization
? ? void Start () {
}
// Update is called once per frame
void Update () {
}
? ? public void OnPointerEnter(PointerEventData eventData)
? ? {
? ? ? ? //滑鼠進(jìn)入
? ? }
? ? public void OnPointerClick(PointerEventData eventData)
? ? {
? ? ? ? Debug.Log("在圖片內(nèi)取消滑鼠點擊");
? ? }
}
在這里的IPointerEnterHandler和IPointerClickHandler就是和上面的不同,要用哪個就要繼承哪個这敬。另外說一句不要忘了添加using UnityEngine.EventSystems;