The case: We’re populating the content area of a ScrollRect dynamically when constructing a particular page in the UI - that is, we have a list. The elements of this list have different heights, depending on the amount of content in a Text
component.
Timing considerations become important in two distinct cases:
- When navigating to this view, we render it to a
RenderTexture
used in a transition animation. Here, we need to wait until layout is complete before actually rendering the view. - When adding a new element to the list, we’d like to scroll to the bottom so that the user sees the addition. Again, setting the
normalizedVerticalPosition
to zero before the layout has completed results in it scrolling almost to the end of the list, but not quite.
The following questions arise:
- We can request a relayout using
LayoutRebuilder
and/or registering a canvas element (the scroll rect) with theCanvasUpdateRegistry
. But when should we use which? Are there other mechanisms we should know about? - According to the documentation, canvas layout is performed before rendering, at the end of the frame, and we’ve assumed that waiting in a coroutine with
WaitForEndOfFrame
should be sufficient to ensure a complete layout, but that doesn’t always seem to be the case. Is there a way to get notified when layout has completed?