3、使用事件接口
(1)继承MonoBehavior的脚本继承对应的事件接口,引用命名空间
(2)实现接口中的内容
(3)将该脚本挂载到想要监听自定义事件的UI控件上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.U2D;
using UnityEngine.UI;
public class Lesson : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler,IDragHandler {
// 鼠标进入/离开在移动设备上是不存在的,因为不存在进入/离开的概念
public void OnPointerEnter(PointerEventData eventData) {
print("鼠标进入");
}
public void OnPointerExit(PointerEventData eventData) {
print("鼠标离开");
}
public void OnPointerDown(PointerEventData eventData) {
print("鼠标(触碰)按下");
print(eventData.pointerId);
}
public void OnPointerUp(PointerEventData eventData) {
print("鼠标(触碰)抬起");
}
public void OnDrag(PointerEventData eventData) {
print(eventData.delta);
}
}