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

2023-08-24 15:31:53 浏览数 (2)

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

代码语言:javascript复制
        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

代码语言:javascript复制
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.封装点击按钮带参数

代码语言:javascript复制
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);
    }
}

0 人点赞