unity3d:UGUI源码EventSystem输入系统常见问题

本文详细探讨Unity中Button和Text组件的点击响应机制,重点讲解了EventSystem、Raycast、IPointerClickHandler和优化策略,包括不规则按钮处理、穿透UI点击及性能优化。适合了解Unity UI开发者阅读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. button从按下到响应的过程

1.先是EventSystem在Update中调用当前输入模块InputModules的Process方法处理所有的鼠标事件,
2.并且输入模块会调用RaycastAll来得到目标信息,
3.通过子物体未挂载IEventSystemHandler,再找父物体方式找到事件实际接收者并执行点击事件

2. button子物体text也勾选了RaycastTarget,为什么是响应button,而不是text

创建一个Button,那这个Button还包含了Text组件,如果text.RaycastTarget勾上
当鼠标点击的时候会调用GetEventHandler函数,
该函数的root参数其实是Text,发现text无IEventSystemHandler组件
但是会查找到它的父物体Button,发现有,然后调用Button的点击事件
核心问题:text缺少IEventSystemHandler
public class Button : Selectable, IPointerClickHandler, ISubmitHandler
public interface IPointerClickHandler : IEventSystemHandler

点击button,先响应text,再查找到button,handlerCount为0,说明无IPointerClickHandler组件
在这里插入图片描述

text加上EventTrigger,会响应text的点击事件,不会向上响应button
在这里插入图片描述

3. 如何强制让text点击,例如聊天系统点击超链接

text挂脚本实现IPointerClickHandler 接口OnPointerClick

4. 穿透UI点击问题

IsPointerOverGameObject是射线接触到的UI有RaycastTarget

        public override bool IsPointerOverGameObject(int pointerId)
        {
            var lastPointer = GetLastPointerEventData(pointerId);
            if (lastPointer != null)
                return lastPointer.pointerEnter != null;
            return false;
        }

if (Input.GetMouseButtonDown(0))
            {
                if (Application.isMobilePlatform && Input.touchCount > 0)
                {
                    for (int i = 0; i < Input.touchCount; i++)
                    {
                        if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(i).fingerId))
                        {
                            m_isClickUi = true;
                            break;
                        }
                    }
                }
                else if (EventSystem.current.IsPointerOverGameObject())
                {
                    m_isClickUi = true;
                }
            }
            

5. EventSystem功能

EventSystem会在Update中调用输入模块PointerInputModule的Process方法来处理输入消息
PointerInputModule会调用EventSystem中的RaycastAll方法进行射线检测
RaycastAll又会调用BastRaycaster的Raycast方法执行具体的射线检测操作,主要是获取被选中的目标信息。

6. 不规则按钮如何响应点击

Polygon Collider 2D

7. 设计建造系统:如何拖动屏幕不响应建筑点击,如何区分是点击建筑还是拖动建筑

物品点击与拖屏

8. GraphicRaycaster.BlockingObjects

不用的时候不勾选2d,3d,在update有性能消耗。默认是none

if (blockingObjects == BlockingObjects.ThreeD || blockingObjects == BlockingObjects.All)
                {
                    if (ReflectionMethodsCache.Singleton.raycast3D != null)
                    {
                        var hits = ReflectionMethodsCache.Singleton.raycast3DAll(ray, distanceToClipPlane, (int)m_BlockingMask);
                        if (hits.Length > 0)
                            hitDistance = hits[0].distance;
                    }
                }

                if (blockingObjects == BlockingObjects.TwoD || blockingObjects == BlockingObjects.All)
                {
                    if (ReflectionMethodsCache.Singleton.raycast2D != null)
                    {
                        var hits = ReflectionMethodsCache.Singleton.getRayIntersectionAll(ray, distanceToClipPlane, (int)m_BlockingMask);
                        if (hits.Length > 0)
                            hitDistance = hits[0].distance;
                    }
                }

9. 有哪些优化

1.不需要点击事件的可以不勾选RaycastTarget
2.封装点击按钮带参数

using UnityEngine;
using System.Collections;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class ClickListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler,IPointerUpHandler
{
    public delegate void VoidDelegate(GameObject go);
    public VoidDelegate onClick;
    public VoidDelegate onPress;
    public VoidDelegate onNewGuideClick;

    static public ClickListener Get(GameObject go)
    {
        ClickListener listener = go.GetComponent<ClickListener>();
        if (listener == null) listener = go.AddComponent<ClickListener>();
        return listener;
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null)
        {
            onClick(gameObject);
        }
    }
    bool ispress;
    public void OnPointerDown(PointerEventData eventData)
    {
        ispress = true;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        ispress = false;
    }

    void Update() {
        if (onPress != null && ispress)
            onPress(gameObject);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值