Event事件回调:
一个场景当中只能有一个EventSystem,并且需要BaseInputModule组件
才能协助工作。
UI中触发事件回调可以在组件中存在,也有的单拿出来处理回调事件.在处理回调的脚本中,要引入项目应的命名空间和相关的接口,使用什么接口就引入那个,并实现其方法.
命名空间引入: using UnityEngine.EventSystems;
常用的回调事件如下所示:
Demo1:使用UGUI的回调函数实现物体随着鼠标的拖拽效果
Demo2:实现鼠标移动到按钮上,按钮放大的效果
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.EventSystems;
- public class UI_Event_Drag :MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
- ,IDragHandler
- {
- GameObject button;
- GameObject image;
- private void Awake()
- {
- button = GameObject.Find("Button");
- image = GameObject.Find("Image");
- }
- public void OnDrag(PointerEventData eventData)
- {
- image.transform.position = eventData.position;
- }
- public void OnPointerEnter(PointerEventData eventData)
- {
- button.transform.localScale *= 1.5f;
- }
- public void OnPointerExit(PointerEventData eventData)
- {
- button.transform.localScale /= 1.5f;
- }
- }