UGUI event system four (CanvasUpdateRegistry)

        CanvasUpdateRegistry (canvas update registry) is a singleton. It is the intermediary between UGUI and Canvas. Components that inherit the ICanvasElement interface can be registered to it. It listens to the event that Canvas is about to render and calls Rebuild and other methods of registered components.
        First you can see the ICanvasElement interface:

public interface ICanvasElement
{
    void Rebuild(CanvasUpdate executing);
    Transform transform { get; }
    void LayoutComplete();
    void GraphicUpdateComplete();
    // due to unity overriding null check
    // we need this as something may not be null
    // but may be destroyed
    bool IsDestroyed();
}

        We know that UGUI components inherit from UIBehaviour, and UIBehaviour implements the IsDestroyed method. All components inherit from Component, and Component implements the transform property. Therefore, UGUI components that inherit from ICanvasElement no longer need to implement these two members. The other three Rebuild, LayoutComplete, and GraphicUpdateComplete need to be implemented in the code.
(An exception is LayoutRebuilder. It is not a component, but a class responsible for rebuilding the layout. We will talk about it in subsequent articles)

        ​​​​ CanvasUpdateRegistry maintains two index sets (the same elements will not be stored):

       

private readonly IndexedSet<ICanvasElement> m_LayoutRebuildQueue = new IndexedSet<ICanvasElement>();
private readonly IndexedSet<ICanvasElement> m_GraphicRebuildQueue = new IndexedSet<ICanvasElement>();

        One is the layout reconstruction sequence (m_LayoutRebuildQueue), and the other is the image reconstruction sequence (m_GraphicRebuildQueue).
        m_LayoutRebuildQueue adds elements through the RegisterCanvasElementForLayoutRebuild and TryRegisterCanvasElementForLayoutRebuild methods.

        m_GraphicRebuildQueue adds elements through the RegisterCanvasElementForGraphicRebuild and TryRegisterCanvasElementForGraphicRebuild methods.

        ​​​​​Both remove registered elements through UnRegisterCanvasElementForRebuild.

        The constructor of CanvasUpdateRegistry:

protected CanvasUpdateRegistry()
{
    Canvas.willRenderCanvases += PerformUpdate;
}

        WillRenderCanvases is a static event of the static class Canvas. The event is a special kind of delegation (for delegation, please refer to C# Grammar Tips (4) Delegate delegate). In the constructor of CanvasUpdateRegistry, a listener PerformUpdate is added for the willRenderCanvases event. From the literal meaning, we can know that the willRenderCanvases event will be thrown before rendering (all) Canvas, and then the PerformUpdate method of CanvasUpdateRegistry will be called.

        Before introducing PerformUpdate, let’s scroll the CanvasUpdateRegistry file to the top. We see an enumeration type:

public enum CanvasUpdate
{
    Prelayout = 0,
    Layout = 1,
    PostLayout = 2,
    PreRender = 3,
    LatePreRender = 4,
    MaxUpdateValue = 5
}

        Except for the last enumeration item, the other five items represent the three stages of layout and the two stages of rendering respectively.
        In PerformUpdate, unavailable elements will be deleted from the two sequences (if the element is LayoutRebuilder, LayoutComplete will be called).

        Then sort M_LayoutrebuildQueue according to the number of parent objects. Then call the Rebuild method of each element with Prelayout, Layout and PostLayout as parameters, then call the LayoutComplete method of all elements and clear all elements.

        After completing the layout, call ClipperRegistry.instance.Cull(). (ClipperRegistry is another registry singleton used to call the component's clipping method after layout.)

        Then call the Rebuild method of each element of m_GraphicRebuildQueue with PreRender and LatePreRender as parameters respectively, and then call the LayoutComplete (? It seems to be a BUG) method of all elements and clear all elements.

At this point, a complete update process is completed.

Guess you like

Origin blog.csdn.net/ttod/article/details/135010205