Search Unity

Graphic redraw callback?

Discussion in 'UGUI & TextMesh Pro' started by Cross22, Mar 13, 2017.

  1. Cross22

    Cross22

    Joined:
    Sep 26, 2014
    Posts:
    22
    I am trying to replace the standard GraphicRaycaster with something that's a bit more efficient. For that I am looking for some kind of callback when the Canvas / Graphics elements have changed (e.g. due to manual repositioning or animations).

    There are some callbacks but they do not seem to get triggered on position changes, e.g. the below callbacks execute when the layout is changed, but animation clips or dragging the UI elements in inspector does nothing.
    Code (CSharp):
    1.         var canvas = GetComponent<Canvas>();
    2.         var graphicsList = GraphicRegistry.GetGraphicsForCanvas(canvas);
    3.        
    4.         for (int i = 0; i < graphicsList.Count; ++i)
    5.         {
    6.             var graphic = graphicsList[i];
    7.             graphic.RegisterDirtyLayoutCallback(OnDirtyLayout);
    8.             graphic.RegisterDirtyMaterialCallback(OnDirtyMat);
    9.             graphic.RegisterDirtyVerticesCallback(OnDirtyVerts);
    10.         }
    There is a Canvas.willRenderCanvases event that seems to get triggered every frame, regardless whether anything has changed.

    The only reliable way I have found to detect changes is to query TransformRect.hasChanged. But that requires polling the entire UI hierarchy and I am hoping Unity provides some kind of notification instead. Does it?