UIGU源码分析12:Graphic

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

源码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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值