ICanvasElement LayoutComplete not actually complete

What event/callback can I use to know that a Canvas has completed its full layout?

Simply registering a ICanvasElement and responding to the LayoutComplete is not enough.
CanvasUpdateRegistry.IsRebuildingLayout is still true.

so… Let’s say I was spawning some objects in that LayoutComplete method and disabling them.

CanvasUpdateRegistry complains:

“Trying to remove element from rebuild list while we are already inside a rebuild loop. This is not supported.”

Ok fine, I don’t want to disable something in the rebuild loop, which is why I’m doing it in LayoutComplete. Maybe we need another method LayoutActuallyCompleteNow :sunglasses:

For now I’m using a custom yield instruction:

public class WaitForLayoutComplete : CustomYieldInstruction {

    public override bool keepWaiting {

        get { return CanvasUpdateRegistry.IsRebuildingLayout();    }
    }
}

public class MyObject : MonoBehaviour {

    public GameObject prefab;

    IEnumerator Start() {

        yield return new WaitForLayoutComplete();

        GameObject instance = Instantiate(prefab);
        instance.SetActive(false);
    }
}
2 Likes

register into it via
CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
CanvasUpdateRegistry.RegisterCanvasElementForLayoutRebuild(this);

1 Like