UGUI的源码还是非常清晰的,打开源码可以发现,
从UGUI的源码可知:在EventSystem中调用每一帧函数来实现:

   private void TickModules()
{
for (var i = 0; i < m_SystemInputModules.Count; i++)
{
if (m_SystemInputModules[i] != null)
m_SystemInputModules[i].UpdateModule();
}
}

这段代码可以得到当前输入InputModule的position,接着在GetMousePointerEventData函数中存在这一段代码:

 public void RaycastAll(PointerEventData eventData, List<RaycastResult> raycastResults)
{
raycastResults.Clear();
var modules = RaycasterManager.GetRaycasters();
for (int i = 0; i < modules.Count; ++i)
{
var module = modules[i];
if (module == null || !module.IsActive())
continue;
        module.Raycast(eventData, raycastResults);
} raycastResults.Sort(s_RaycastComparer);
}

你会发现s_RaycastComparer这样一个函数,该函数就是对所有射线射到的物体进行排序,哈哈,终于找到这个函数了,看看它的实现:

 private static int RaycastComparer(RaycastResult lhs, RaycastResult rhs)
{
if (lhs.module != rhs.module)
{
if (lhs.module.eventCamera != null && rhs.module.eventCamera != null && lhs.module.eventCamera.depth != rhs.module.eventCamera.depth)
{
// need to reverse the standard compareTo
if (lhs.module.eventCamera.depth < rhs.module.eventCamera.depth)
return 1;
if (lhs.module.eventCamera.depth == rhs.module.eventCamera.depth)
return 0;
            return -1;
} if (lhs.module.sortOrderPriority != rhs.module.sortOrderPriority)
return rhs.module.sortOrderPriority.CompareTo(lhs.module.sortOrderPriority); if (lhs.module.renderOrderPriority != rhs.module.renderOrderPriority)
return rhs.module.renderOrderPriority.CompareTo(lhs.module.renderOrderPriority);
} if (lhs.sortingLayer != rhs.sortingLayer)
{
// Uses the layer value to properly compare the relative order of the layers.
var rid = SortingLayer.GetLayerValueFromID(rhs.sortingLayer);
var lid = SortingLayer.GetLayerValueFromID(lhs.sortingLayer);
return rid.CompareTo(lid);
} if (lhs.sortingOrder != rhs.sortingOrder)
return rhs.sortingOrder.CompareTo(lhs.sortingOrder); if (lhs.depth != rhs.depth)
return rhs.depth.CompareTo(lhs.depth); if (lhs.distance != rhs.distance)
return lhs.distance.CompareTo(rhs.distance); return lhs.index.CompareTo(rhs.index);
}

来分析下这段代码的实现:

  • 如果Canvas指定了摄像机,就按照摄像机的depth排序。

  • 按照sortOrderPriority的顺序排序。

  • 按照renderOrderPriority排

  • 按depth排(针对GraphicRaycaster),其他两种物理射线(PhysicsRaycaster,Physics2DRaycaster)的depth为0。

  • 按distance排、这里是针对两种物理射线的方式,为射线到物体的距离。

  • 按index排,无论是哪种射线模式,都会将射线碰撞到的UI物体压入到列表中,这里的index就是列表的索引值。

如何找到已经实现了该事件的组件?

在ExecuteEvents的类中有这样一段代码:

 private static void GetEventChain(GameObject root, IList<Transform> eventChain)
{
eventChain.Clear();
if (root == null)
return;
    var t = root.transform;
while (t != null)
{
eventChain.Add(t);
t = t.parent;
}
}

这段代码一直在点击的gameobject上递归寻找它的父类,然后加入到列表当中。只要有一个父类实现了该接口就会触发该接口事件。然后返回该gameObject。

做实验的话,可以在父对象上实现一个接口函数,点击子对象发现父对象的函数触发了。自己亲测可行。

UGUI的事件系统分析的更多相关文章

  1. UGUI穿透3D世界判断&&UGUI全事件监听

    public bool isPointUI(){ PointerEventData eventDataCurrnt = new PointerEventData (EventSystem.curren ...

  2. UGUI动态绑定事件

    using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;using UnityEngine. ...

  3. UGUI双击事件

    经测试在Android.ios平台下无效 using UnityEngine; using UnityEngine.EventSystems; using System.Collections; us ...

  4. UGUI研究院之控件以及按钮的监听事件系统

    继续学习,我相信大家在做NGUI开发的时候处理事件都会用到UIEventListener,那么UGUI中怎么办呢?先看UGUI的事件有那些吧 Supported Events The Eventsys ...

  5. uGUI VS NGUI

    前言 这篇日志的比较是根据自己掌握知识所写的,请各路大神多多指教. 引擎版本: Unity 4.6 beta 两者区别 1.uGUI的Canvas 有世界坐标和屏幕坐标 2.uGUI的Button属性 ...

  6. 用uGUI开发自定义Toggle Slider控件

    一.前言 写完<Unity4.6新UI系统初探>后,我模仿手机上的UI分别用uGui和NGUI做了一个仅用作演示的ToggleSlider,我认为这个小小的控件已能体现自定义控件的开发过程 ...

  7. 转:UGUI与NGUI的区别与优缺点

    1. NGUI与UGUI的区别 1) uGUI的Canvas 有世界坐标和屏幕坐标   2) uGUI的Image可以使用material     3) UGUI通过Mask来裁剪,而NGUI通过Pa ...

  8. Unity3D研究院之将UI的点击事件渗透下去(转)

    转自 http://www.xuanyusong.com/archives/4241 处理UI还有3D模型的点击推荐使用UGUI的这套事件系统,因为使用起来比较简洁,不需要自己用代码来发送射线,并且可 ...

  9. Unity3D 脚本手册

    1.private Ray ray;  --定义射线 ray = Camera.main.ScreenPointToRay(Input.mousePosition);  --摄像机发出的射线投射鼠标到 ...

随机推荐

  1. final注意事项

    final修饰的类为终态类,不能被继承,而 抽象类是必须被继承的才有其意义的,因此,final是不能用来修饰抽象类的. final修饰的方法为终态方法,不能被重写.而继承抽象类,必须重写其方法. 抽象 ...

  2. Umbraco项目发布错误 --More than one type want to be a model for content type authorize

    在开发项目时,解决方案下面包括三个项目 MyUmbracoProject MyUmbracoProject.Core MyUmbracoProject.FrontEnd 第一个项目MyUmbracoP ...

  3. position:fixed;如何居中

    div{ position:fixed; margin:auto; left:; right:; top:; bottom:; width:100px; height:100px; } 如果只需要左右 ...

  4. C#----接口与抽象类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 接口 { ...

  5. 【Android-stdio-appdemo搭建记录】

    1-如何删除存在的工程 2-创建Android项目 next设置app兼容最低版本:api15--android 4.0以上 创建活动页面 设置活动页面的名称 创建成功项目以后会有最基本的layout ...

  6. 求第 i 个素数 Meissel Lehmer Algorithm + 二分 【模板】

    1473: L先生与质数V3 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1348  Solved: 147 [Submit][Status][Web ...

  7. web安全深度剖析pdf

    Web安全深度剖析.pdf_免费高速下载|百度网盘-分享无限制 链接:https://pan.baidu.com/s/1kVwP7SF

  8. PAT甲级——1097 Deduplication on a Linked List (链表)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...

  9. oracle数据库的导入导出命令

    说明:将以下命令复制到cmd命令行中运行即可,file代表文件名数据导入imp zzbweb/zzbweb@orcl file=e:\zzbweb.dmp fromuser=zzbweb touser ...

  10. POJ1027 The Same Game

    题目来源:http://poj.org/problem?id=1027 题目大意: 题目说的就是现在蛮流行的手机小游戏popstar,求用贪心方法能得到多少分. 小球有三种颜色:R/G/B.横向.纵向 ...