源码12:Graphic
Grahic是UGUI的核心组件,负责显示图像,继承了UIBehaviour,ICanvasElement。它是一个抽象类,是MaskableGraphic(可遮罩图像)的基类,而后者是RawImage,Image和Text的基类
GraphicRegistry
GraphicRegistry的作用主要是记录每个Canvas画布上激活的Graphic。
public class GraphicRegistry
{
private static GraphicRegistry s_Instance;
//每个Canvas对应一个IndexedSet,这个数据结构能够保证元素唯一且有序。
private readonly Dictionary<Canvas, IndexedSet<Graphic>> m_Graphics = new Dictionary<Canvas, IndexedSet<Graphic>>();
//可进行射线检测的
private readonly Dictionary<Canvas, IndexedSet<Graphic>> m_RaycastableGraphics = new Dictionary<Canvas, IndexedSet<Graphic>>();
protected GraphicRegistry()
{
// Avoid runtime generation of these types. Some platforms are AOT only and do not support
// JIT. What's more we actually create a instance of the required types instead of
// just declaring an unused variable which may be optimized away by some compilers (Mono vs MS).
// See: 877060
System.GC.KeepAlive(new Dictionary<Graphic, int>());
System.GC.KeepAlive(new Dictionary<ICanvasElement, int>());
System.GC.KeepAlive(new Dictionary<IClipper, int>());
}
...
}
注册移除和移除对应Canvas的 IndexedSet结构上的Graphic
public static void RegisterGraphicForCanvas(Canvas c, Graphic graphic)
{
...
}
public static void UnregisterGraphicForCanvas(Canvas c, Graphic graphic)
{
...
}
获取Canvas对应的所有Graphic元素
public static IList<Graphic> GetGraphicsForCanvas(Canvas canvas)
{
IndexedSet<Graphic> graphics;
if (instance.m_Graphics.TryGetValue(canvas

这篇博客深入解析了Unity UGUI系统中的Graphic组件和GraphicRegistry类。Graphic是UGUI的核心组件,负责显示图像,是多个UI元素如RawImage、Image和Text的基类。GraphicRegistry则用于记录每个Canvas上的激活Graphic,确保高效渲染和射线检测。文章详细介绍了Graphic的脏标志更新机制,以及如何通过CanvasUpdateRegistry和LayoutRebuilder进行布局和材质的重建。同时,讲解了IMeshModifier和ICanvasRaycastFilter接口在实现遮罩效果和射线检测中的作用。

1182

被折叠的 条评论
为什么被折叠?



