[Unity 3D] EventSystem的简单介绍和使用

官方文档:https://docs.unity3d.com/ScriptReference/EventSystems.EventSystem.html

EventSystem

描述

处理输入、射线和发送事件。

在Unity场景中EventSystem主要负责加工和处理事件。一个场景只能含有一个EventSystem。

静态属性

current返回当前的EventSystem。

属性

alreadySelecting如果当前已选中某个GameObject就返回true。
currentInputModule当前激活的BaseInputModule。
currentSelectedGameObject当前选择的GameObject。
firstSelectedGameObject最先被选中的GameObject。
isFocused标志着EventSystem是否认为它应该暂停,或者不基于集中状态。
pixelDragThreshold正在拖拽的软区域像素。
sendNavigationEventsEventSystem是否发送导航事件(move / submit / cancel)。

公开方法

IsPointerOverGameObject给定ID的指示器是否在GameObject之上。
RaycastAll将所有已配置的BaseRaycaster投射入场景中。
SetSelectedGameObject设置当前选择的GameObject。 将触发之前选择的GameObject的OnDeselect和当前选择的OnSelect。
UpdateModules重新计算内部的BaseInputModule列表。

 

简单的使用

1、获取当前点击的UI(按钮等)

GameObject currentSelected = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject;

2、判断 鼠标/手指是否在UI上

需要注意的是:

  1. 需要注意如果不传入参数的话默认为pointerId = -1,即鼠标左键。
  2. IsPointerOverGameObject通常与nMouseDown()Input.GetMouseButtonDown(0)或者Input.GetTouch(0).phase == TouchPhase.Began一起使用。
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class TouchExample : MonoBehaviour
{
    void Update()
    {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            {
                Debug.Log("Touched the UI");
            }
        }
    }
}

 

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值