前言
UGUI中EventSystem主要负责处理输入、射线以及发送事件,一个场景中
只能有一个EventSystem并且需要 BaseInputModule类型组建的协助才
能工作。
EventSystem负责管理,BaseInputModule负责输 入,BaseRaycaster负责确定目标对象。
UGUI中不仅可以对UI进行监测,还可以对场景中的对象进 行监测,以下会
有详细介绍
需要引入的空间名
using UnityEngine.EventSystems;
using UnityEngine.Events;
一、StandaloneInputModule和TouchInoutModule两个组件会监测一些输入操作,以事件的方式通知目标对象,支持的事件有以下:
- IPointerEnterHandler - OnPointerEnter - Called when a pointer enters the object
- IPointerExitHandler - OnPointerExit - Called when a pointer exits the object
- IPointerDownHandler - OnPointerDown - Called when a pointer is pressed on the object
- IPointerUpHandler - OnPointerUp - Called when a pointer is released (called on the original the pressed object)
- IPointerClickHandler - OnPointerClick - Called when a pointer is pressed and released on the same object
- IInitializePotentialDragHandler - OnInitializePotentialDrag - Called when a drag target is found, can be used to initialise values
使用方式:
1. 引入命名空间 using UnityEngine.EventSystems;
2. 使用某个方法就继承某个接口
3. 根据方法的参数使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// Button a.
/// UGUI处理其他类型事件的方式一:接口
/// </summary>
public class ButtonA : MonoBehaviour,IPointerEnterHand