当前位置:网站首页>UGUI——CanvasUpdateRegistry
UGUI——CanvasUpdateRegistry
2022-07-20 05:34:00 【fztfztfzt】
- Graphic 和Layout 组件都依赖于CanvasUpdateRegistry 类,它不在Unity编辑器的界面中公开。此类跟踪必须更新的Layout 组件和Graphic 组件集,并在其关联的Canvas 调用willRenderCanvases事件时根据需要触发更新。[引用自官方文档]
单例
申明了一个CanvasUpdateRegistry类型的instance单例类
/// <summary>
/// Get the singleton registry instance.
/// </summary>
public static CanvasUpdateRegistry instance
{
get
{
if (s_Instance == null)
s_Instance = new CanvasUpdateRegistry();
return s_Instance;
}
}
跟踪
m_LayoutRebuildQueue
private readonly IndexedSet<ICanvasElement> m_LayoutRebuildQueue = new IndexedSet<ICanvasElement>();
通过
InternalRegisterCanvasElementForLayoutRebuild
接口将 ICanvasElement 加入m_LayoutRebuildQueue,不会重复添加,外界通过调用静态函数RegisterCanvasElementForLayoutRebuild将ICanvasElement加入队列关于 IndexedSet
外部添加途径
- Scrollbar的OnValidate接口, ScrollRect的OnEnable、SetDirtyCaching接口,Slider的OnValidate接口,Toggle的OnValidate接口:调用RegisterCanvasElementForLayoutRebuild注册
- LayoutRebuild的MarkLayoutRootForRebuild静态方法调用TryRegisterCanvasElementForLayoutRebuild注册,MarkLayoutRootForRebuild又只在其MarkLayoutForRebuild接口中调用。而
LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
在需要更新位置、大小的组件里被调用(通常在onenable、setdirty中)
m_GraphicRebuildQueue
private readonly IndexedSet<ICanvasElement> m_GraphicRebuildQueue = new IndexedSet<ICanvasElement>();
- 同1,不同的是:不会根据是否添加过返回false
- 同上2
- 增加了m_PerformingGraphicUpdate参数,控制添加元素,正在执行m_GraphicRebuildQueue中元素的rebuild时,不允许添加/移除新的元素
- 外部添加途径:只有Graphic中几个设置dirty的地方主动调用了:SetVerticesDirty、SetMaterialDirty、OnCullingChanged、MarkGeometryAsDirty
CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
调用
PerformUpdate
注册进委托
在构造函数中,将PerformUpdate添加进Canvas.willRenderCanvases
protected CanvasUpdateRegistry()
{
Canvas.willRenderCanvases += PerformUpdate;
}
执行
- 调用CleanInvalidItems,从上面2个数组中移除null或者IsDestroyed的对象,IsDestroyed的对象会调用一次其LayoutComplete接口
- 设置m_PerformingLayoutUpdate = true,表示在执行layout的update,避免在执行时有layout被从数组中移除
- m_LayoutRebuildQueue.Sort(s_SortLayoutFunction);排序,按照父节点数量从少到多排序。官方文档也解释了排序原因:
- 在GameObject层次结构中靠近根的布局可能会改变嵌套在其中的任何布局的位置和大小,因此必须首先计算。
- 遍历3次(Prelayout、Layout、PostLayout)m_LayoutRebuildQueue,调用其rebuild接口
- 再次遍历m_LayoutRebuildQueue,调用LayoutComplete接口
- 清空m_LayoutRebuildQueue
- m_PerformingLayoutUpdate = false
- ClipperRegistry.instance.Cull();执行裁剪
- m_PerformingGraphicUpdate = true;防止在执行过程中对数组添加和删除
- 遍历2次(PreRender、LatePreRender)m_GraphicRebuildQueue,调用Rebuild接口
- 再次遍历m_GraphicRebuildQueue,调用GraphicUpdateComplete接口
- 清空m_GraphicRebuildQueue
- m_PerformingGraphicUpdate = false;
ClipperRegistry
Registry class to keep track of all IClippers that exist in the scene
This is used during the CanvasUpdate loop to cull clippable elements. The clipping is called after layout, but before Graphic update.
实现和CanvasUpdateRegistry类似,一个m_Clippers的IndexedSet<IClipper>,保存可裁剪对象,不同的是,m_Clippers不会主动清空。目前专门给RectMask2D用的,在OnEnable将自身添加进数组,OnDisable时反注册
执行时机是上文的步骤8,在layout更新后,Graphic更新前执行
边栏推荐
猜你喜欢
[cann training camp] cann training camp_ Shengteng AI interesting application realizes AI interesting application (Part 2) essay
vsCode配置Eslint+Prettier结合使用详细配置步骤,规范化开发
Stm32cubemx reads and writes USB flash disk through FatFs
STM32CubeMX的正交编码器encoder
kettle优化之提高MySQL读写速度
RIoTBoard开发板系列笔记(四)—— 使用VPU硬件解码
DC-2-靶场实践
解决错误:Error: TomEE required to support EAR/EJB deployment
Application practice of shengteng industrial quality inspection
SourceTree对代码版本管理的操作流程及故障处理(不定时更新)
随机推荐
After xshell is installed, an error is reported when starting: mfc110 cannot be found DLL, unable to continue code execution. Reinstalling the program may fix this problem
[système robotique Ros] navigation autonome + détection de cibles Yolo + annonce vocale
STM32CubeMX的CAN总线波特率设置
About encoding (ASCII / Unicode / utf8)
GD32利用CubeMX构建代码的测试
Jupyter中markdown的操作技巧
Intuitive understanding of diluted revolution
【STM32F103RCT6】电机PWM驱动模块思路与代码
输配电及用电工程毕业论文题目
Off chip spiflash embedded in FatFs based on stm32cubemx
Graduation thesis title of power system and automation [selected]
Chapter002 FPGA learning key control LED light and buzzer
回溯解决组合问题
Tools - idea nice style font / size/
Understand wgan and spectral normalization
SQL subquery
makefile中进行宏定义并传递给源代码
A popular understanding of the spectrum obtained by Fourier transform of images
学习记录十二
web---jdbc工具类编写