summaryrefslogtreecommitdiff
path: root/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-04-08 18:26:02 +0800
committerchai <chaifix@163.com>2021-04-08 18:26:02 +0800
commit6c0dc6615522a7308fc7457b469521ee82130705 (patch)
treef31a18a4bb614ac6b8902c43324e0577c61cb3af /Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs
parentd69611d66431e28ea35477c6781a00d57ae04fa3 (diff)
*canvas update system
Diffstat (limited to 'Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs')
-rw-r--r--Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs56
1 files changed, 44 insertions, 12 deletions
diff --git a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs
index bc7263b..d03d0e7 100644
--- a/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs
+++ b/Assets/uGUI-2017.1/UnityEngine.UI/UI/Core/CanvasUpdateRegistry.cs
@@ -4,6 +4,8 @@ using UnityEngine.UI.Collections;
namespace UnityEngine.UI
{
+ // canvas的不同阶段,用来设置layout、graphic做不同的事情,具体看CanvasUpdateRegistry
+ // prelayout -> layout -> postlayout -> prerender -> laterender =>=>=> render
public enum CanvasUpdate
{
Prelayout = 0,
@@ -11,13 +13,14 @@ namespace UnityEngine.UI
PostLayout = 2,
PreRender = 3,
LatePreRender = 4,
+
MaxUpdateValue = 5
}
public interface ICanvasElement
{
- void Rebuild(CanvasUpdate executing);
Transform transform { get; }
+ void Rebuild(CanvasUpdate executing);
void LayoutComplete();
void GraphicUpdateComplete();
// due to unity overriding null check
@@ -26,6 +29,7 @@ namespace UnityEngine.UI
bool IsDestroyed();
}
+ // 需要注意整个游戏只有一个单例
public class CanvasUpdateRegistry
{
private static CanvasUpdateRegistry s_Instance;
@@ -38,9 +42,11 @@ namespace UnityEngine.UI
protected CanvasUpdateRegistry()
{
+ // willRenderCanvases在渲染canvas之前调用,执行PerformUpdate来Rebuild处理m_LayoutRebuildQueue和m_GraphicRebuildQueue
Canvas.willRenderCanvases += PerformUpdate;
}
+ // 简单单例
public static CanvasUpdateRegistry instance
{
get
@@ -51,6 +57,7 @@ namespace UnityEngine.UI
}
}
+ // 检查一下实现了ICanvasElement的对象合法性,实现了ICanvasElement接口的对象必须是Unity Object,
private bool ObjectValidForUpdate(ICanvasElement element)
{
var valid = element != null;
@@ -104,6 +111,8 @@ namespace UnityEngine.UI
}
private static readonly Comparison<ICanvasElement> s_SortLayoutFunction = SortLayoutList;
+ // 在渲染canvas之前对canvas下的元素进行rebuild
+ // 进行 prelayout -> layout -> postlayout -> prerender -> LatePreRender 流程
private void PerformUpdate()
{
UISystemProfilerApi.BeginSample(UISystemProfilerApi.SampleType.Layout);
@@ -111,8 +120,12 @@ namespace UnityEngine.UI
m_PerformingLayoutUpdate = true;
+ // 重建layout
+
+ // 根据父节点从少到多排序,先布局父节点少的,由少到多,由内到外
m_LayoutRebuildQueue.Sort(s_SortLayoutFunction);
- for (int i = 0; i <= (int)CanvasUpdate.PostLayout; i++)
+ // prelayout -> layout -> postlayout
+ for (int i = 0; i <= (int)CanvasUpdate.PostLayout; i++) // 不同的阶段
{
for (int j = 0; j < m_LayoutRebuildQueue.Count; j++)
{
@@ -129,16 +142,22 @@ namespace UnityEngine.UI
}
}
+ // 布局完成
for (int i = 0; i < m_LayoutRebuildQueue.Count; ++i)
m_LayoutRebuildQueue[i].LayoutComplete();
+ // 清空队列,重置状态
instance.m_LayoutRebuildQueue.Clear();
m_PerformingLayoutUpdate = false;
+ // 做剔除(裁剪)
// now layout is complete do culling...
ClipperRegistry.instance.Cull();
+ // 重建graphic
+
m_PerformingGraphicUpdate = true;
+ // prerender -> lateprerender
for (var i = (int)CanvasUpdate.PreRender; i < (int)CanvasUpdate.MaxUpdateValue; i++)
{
for (var k = 0; k < instance.m_GraphicRebuildQueue.Count; k++)
@@ -156,14 +175,21 @@ namespace UnityEngine.UI
}
}
+ // graphic设置mesh和材质完成
for (int i = 0; i < m_GraphicRebuildQueue.Count; ++i)
m_GraphicRebuildQueue[i].GraphicUpdateComplete();
+ // 清空队列,重置状态
instance.m_GraphicRebuildQueue.Clear();
m_PerformingGraphicUpdate = false;
+
+ // 到了这里布局和网格、材质都设置完毕,后面canvas会渲染
+
+ // profiler不用管
UISystemProfilerApi.EndSample(UISystemProfilerApi.SampleType.Layout);
}
+ // 统计父节点个数
private static int ParentCount(Transform child)
{
if (child == null)
@@ -179,14 +205,27 @@ namespace UnityEngine.UI
return count;
}
+ // 根据父节点由少到多排序
private static int SortLayoutList(ICanvasElement x, ICanvasElement y)
{
Transform t1 = x.transform;
Transform t2 = y.transform;
return ParentCount(t1) - ParentCount(t2);
+ }
+
+ public static bool IsRebuildingLayout()
+ {
+ return instance.m_PerformingLayoutUpdate;
}
+ public static bool IsRebuildingGraphics()
+ {
+ return instance.m_PerformingGraphicUpdate;
+ }
+
+#region 将canvas elements注册到队列里的方法
+
public static void RegisterCanvasElementForLayoutRebuild(ICanvasElement element)
{
instance.InternalRegisterCanvasElementForLayoutRebuild(element);
@@ -260,16 +299,9 @@ namespace UnityEngine.UI
}
element.GraphicUpdateComplete();
instance.m_GraphicRebuildQueue.Remove(element);
- }
+ }
+
+#endregion
- public static bool IsRebuildingLayout()
- {
- return instance.m_PerformingLayoutUpdate;
- }
-
- public static bool IsRebuildingGraphics()
- {
- return instance.m_PerformingGraphicUpdate;
- }
}
}