今天研究下UGUI的源码,先从EventSystem入手。EventSystem是用来处理点击、键盘输入以及触摸等事件的。

1.BaseInputModule

EventSystem开头声明了两个变量,系统的输入模块列表和当前输入模块

     private List<BaseInputModule> m_SystemInputModules = new List<BaseInputModule>();

        private BaseInputModule m_CurrentInputModule;

BaseInputModule是一个抽象类,PointerInputModule继承于BaseInputModule,也是一个抽象类,StandaloneInputModule和TouchInputModule又继承于PointerInputModule。

StandaloneInputModule:基本的键盘和鼠标输入系统,并跟踪鼠标的位置,以及鼠标/键盘所按下的按键,是面向pc平台的输入模块。

TouchInputModule:基本的触摸输入系统,用于处理触摸、拖拽以及位置数据,是面向移动平台的输入模块。

EventSystem的Update函数中会执行TickModules函数,用于更新m_SystemInputModules的每一个输入模块。

然后遍历m_SystemInputModules,判断是否支持当前平台且处于激活状态,若有赋值给m_CurrentInputModule,若无便选择m_SystemInputModules的第一个支持当前平台的输入模块赋值给m_CurrentInputModule。

再判断是否满足条件if (!changedModule && m_CurrentInputModule != null),当前输入模块执行Process函数,m_CurrentInputModule.Process();

StandaloneInputModule的Process函数中,会把各类事件传递给EventSystem的m_CurrentSelected。m_CurrentSelected由SetSelectedGameObject方法进行设置,并且会对上一个被选中的对象执行取消事件,对新设置的对象执行选中事件。

ExecuteEvents.Execute(m_CurrentSelected, pointer, ExecuteEvents.deselectHandler);
m_CurrentSelected = selected;
ExecuteEvents.Execute(m_CurrentSelected, pointer, ExecuteEvents.selectHandler);

TouchInputModule的Process函数中,会根据是否支持触摸,分别执行FakeTouches(用鼠标模拟触摸)和ProcessTouchEvents函数。ProcessTouchEvents会调用GetTouchPointerEventData函数,GetTouchPointerEventData会通过eventSystem.RaycastAll函数找到第一个被射线照射到的对象,存到一个PointerEventData变量中。然后根据这个PointerEventData变量执行相应的触摸、拖拽等事件。

private void ProcessTouchEvents()
{
for (int i = ; i < input.touchCount; ++i)
{
Touch touch = input.GetTouch(i); if (touch.type == TouchType.Indirect)
continue; bool released;
bool pressed;
var pointer = GetTouchPointerEventData(touch, out pressed, out released); ProcessTouchPress(pointer, pressed, released); if (!released)
{
ProcessMove(pointer);
ProcessDrag(pointer);
}
else
RemovePointerData(pointer);
}
}

2.事件接口

上文提到过ExecuteEvents.Execute函数执行事件,ExecuteEvents是一个静态类,里面声明了一个泛型委托EventFunction,对EventInterfaces的大部分接口声明EventFunction类型的委托变量和函数。例如:

        private static readonly EventFunction<ISelectHandler> s_SelectHandler = Execute;

        private static void Execute(ISelectHandler handler, BaseEventData eventData)
{
handler.OnSelect(eventData);
}

在EventSystem里调用:

ExecuteEvents.Execute(m_CurrentSelected, pointer, ExecuteEvents.selectHandler);

在ExecuteEvents的Execute函数中

public static bool Execute<T>(GameObject target, BaseEventData eventData, EventFunction<T> functor) where T : IEventSystemHandler

会执行functor(arg, eventData),也就是执行arg.OnSelect(eventData)。

EventInterfaces的接口如下:

IPointerEnterHandler//指针进入

IPointerExitHandler//指针离开

IPointerDownHandler//指针按下

IPointerUpHandler//指针抬起

IPointerClickHandler//指针点击

IBeginDragHandler//开始拖拽

IInitializePotentialDragHandler//当发现拖动但在开始拖动有效之前由BaseInputModule调用

IDragHandler//拖拽中

IEndDragHandler//结束拖拽

IDropHandler//结束拖拽时所在gameobject调用

IScrollHandler//鼠标滚轮

IUpdateSelectedHandler//选中物体时,持续触发

ISelectHandler//选中物体

IDeselectHandler//取消选中物体

IMoveHandler//物体移动

ISubmitHandler//提交按钮被按下

ICancelHandler//取消按钮被按下

UGUI源码之EventSystem的更多相关文章

  1. 【UGUI源码分析】Unity遮罩之Mask详细解读

    遮罩,顾名思义是一种可以掩盖其它元素的控件.常用于修改其它元素的外观,或限制元素的形状.比如ScrollView或者圆头像效果都有用到遮罩功能.本系列文章希望通过阅读UGUI源码的方式,来探究遮罩的实 ...

  2. 【UGUI源码分析】Unity遮罩之RectMask2D详细解读

    遮罩,顾名思义是一种可以掩盖其它元素的控件.常用于修改其它元素的外观,或限制元素的形状.比如ScrollView或者圆头像效果都有用到遮罩功能.本系列文章希望通过阅读UGUI源码的方式,来探究遮罩的实 ...

  3. UGUI源码之Selectable

    Selectable是Button.InputField.Toggle.ScrollBar.Slider.Dropdown的基类. Selectable的继承的类与接口如下: public class ...

  4. uGUI源码调试

    uGUI源代码地址:https://bitbucket.org/Unity-Technologies/ui 工具编译后转换位置{Unity3D_Vserion}\Editor\Data\UnityEx ...

  5. UGUI源码之Graphic

    Graphic是用来显示图像的一个抽象类,是MaskableGraphic的父类,而MaskableGraphic是Image.RawImage.Text的父类. Graphic继承于UIBehavi ...

  6. Unity UGUI图文混排源码(一)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

  7. [UGUI]图文混排(二):Text源码分析

    UGUI源码: https://bitbucket.org/Unity-Technologies/ui/downloads/?tab=tags 首先下载一份UGUI源码,这里我下载的版本是5.3.2f ...

  8. Unity UGUI图文混排源码(三) -- 动态表情

    这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...

  9. Unity UGUI图文混排源码(二)

    Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304 Unity UGUI图文混排源码(二):ht ...

随机推荐

  1. Element-ui学习笔记3--Form表单(一)

    Radio单选框 要使用 Radio 组件,只需要设置v-model绑定变量,选中意味着变量的值为相应 Radio label属性的值,label可以是String.Number或Boolean. & ...

  2. WOE:信用评分卡模型中的变量离散化方法(生存分析)

    WOE:信用评分卡模型中的变量离散化方法 2016-03-21 生存分析 在做回归模型时,因临床需要常常需要对连续性的变量离散化,诸如年龄,分为老.中.青三组,一般的做法是ROC或者X-tile等等. ...

  3. 关于 FormData 和 URLSearchParams

    一.FormData FormData 接口提供了一种表示表单数据的键值对的构造方式,经过它的数据可以使用 XMLHttpRequest.send() 方法送出,本接口和此方法都相当简单直接.如果送出 ...

  4. HDU 5971"Wrestling Match"(二分图染色)

    传送门 •题意 给出 n 个人,m 场比赛: 这 m 场比赛,每一场比赛中的对决的两人,一个属于 "good player" 另一个属于 "bad player" ...

  5. SpringBoot2.X 项目使用外置绝对路径的配置文件

    spring-boot-absolute-config 前言 该工程是为解决应用部署应用时指定配置文件存放位置的问题. SpringBoot项目默认加载以下位置的配置文件: ? 1 2 3 4 cla ...

  6. 【75.28%】【codeforces 764B】Decoding

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. router-link-active的作用

    如上图所示,创建了3个路由跳转选项,css实现后的效果如下 ↓↓↓ 当我切换“电影” “影院” “我的” 三个路由选项时,文字由黑色变成红色 此时可用vue自带的 router-link-active ...

  8. Js中没有方法的重载

    <script type="text/javascript"> //方法名相同,后面的方法覆盖了前面的方法.后面的方法需要一个name,但没给传值,所以是undefin ...

  9. dll中全局变量在外部进行引用

    在Windows中实际导出全局变量,您必须使用类似于export / import语法的语法,例如: #ifdef COMPILING_THE_DLL #define MY_DLL_EXPORT ex ...

  10. XSS攻击及防范

    1.什么是XSS攻击 跨站脚本攻击(Cross Site Scripting),攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Script代码会被执行,从而达到 ...