https://img-blog.csdnimg.cn/20201229131747791.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Z6dGZ6dGZ6dA==,size_16,color_FFFFFF,t_70

Base class for all visual UI Component When creating visual UI components you should inherit from this class.

继承自ICanvasElement,ICanvasElement接口用在CanvasUpdateRegistry类中,在PerformUpdate(canvas的willRenderCanvases)时负责对注册的该类组件进行rebuild LayoutComplete GraphicUpdateComplete

/// <summary>
/// This is an element that can live on a Canvas.
/// </summary>
public interface ICanvasElement
{
    /// <summary>
    /// Rebuild the element for the given stage.
    /// </summary>
    /// <param name="executing">The current CanvasUpdate stage being rebuild.</param>
    void Rebuild(CanvasUpdate executing);

    /// <summary>
    /// Get the transform associated with the ICanvasElement.
    /// </summary>
    Transform transform { get; }

    /// <summary>
    /// Callback sent when this ICanvasElement has completed layout.
    /// </summary>
    void LayoutComplete();

    /// <summary>
    /// Callback sent when this ICanvasElement has completed Graphic rebuild.
    /// </summary>
    void GraphicUpdateComplete();

    /// <summary>
    /// Used if the native representation has been destroyed.
    /// </summary>
    /// <returns>Return true if the element is considered destroyed.</returns>
    bool IsDestroyed();
}

protected Material m_Material;

默认材质球

private Color m_Color = Color.white;

Base color of the Graphic. The builtin UI Components use this as their vertex color. Use this to fetch or change the Color of visual UI elements, such as an Image.

public virtual Color color { get { return m_Color; } set { if (SetPropertyUtility.SetColor(ref m_Color, value)) SetVerticesDirty(); } }

private bool m_RaycastTarget = true

是否考虑射线检测

 /// <summary>
/// Should this graphic be considered a target for raycasting?
/// </summary>
public virtual bool raycastTarget { get { return m_RaycastTarget; } set { m_RaycastTarget = value; } }

SetAllDirty

设置layout、material、vertivces需要更新

SetLayoutDirty

layout那节有说,找到根节点的ILayout,加入m_LayoutRebuildQueue

        /// <summary>
        /// Mark the layout as dirty and needing rebuilt.
        /// </summary>
        /// <remarks>
        /// Send a OnDirtyLayoutCallback notification if any elements are registered. See RegisterDirtyLayoutCallback
        /// </remarks>
        public virtual void SetLayoutDirty()
        {
            if (!IsActive())
                return;

            LayoutRebuilder.MarkLayoutForRebuild(rectTransform);

            if (m_OnDirtyLayoutCallback != null)
                m_OnDirtyLayoutCallback();
        }

SetMaterialDirty

加入m_GraphicRebuildQueue队列,同在CanvasUpdateRegistry的PerformUpdate中调用,在layout的rebuild和cull之后调用其rebuild。

private bool InternalRegisterCanvasElementForGraphicRebuild(ICanvasElement element)
{
    if (m_PerformingGraphicUpdate)
    {
        Debug.LogError(string.Format("Trying to add {0} for graphic rebuild while we are already inside a graphic rebuild loop. This is not supported.", element));
        return false;
    }

    return m_GraphicRebuildQueue.AddUnique(element);
}