Search Unity

Unity UI How to wait for LayoutRebuild/GraphicRebuild?

Discussion in 'UGUI & TextMesh Pro' started by 5argon, Jun 24, 2017.

  1. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    I have a big tree of UI elements inside a panel. These have many stacked vertical/horizontal layout groups. It causes lag problem on mobile phone when I try to animate the outer panel with everything inside. It opens up wider height-wise from the center which will rapidly cause layout/graphics rebuild along the animation.

    Screenshot 2017-06-24 22.26.37.png
    My solution is to not displaying anything yet while the big panel animation is playing by SetActive(false) the whole game object tree except the outer panel and then enable the tree afterwards in the animation timeline.

    This works, but on setting active, there's one more animation where all the contents inside will scroll-fade in from the right. On setting active the LayoutRebuild and GraphicRebuild will automatically take place as seen in the Profiler. But on mobile, this again causes a lag and I never got to see the scroll-fade in animation because it skips a few frames after the rebuild.

    What I want to do is to separate the scroll in animation and run the animation only after the rebuild is complete. However how can I subscribe to this event? I see GraphicUpdateComplete and LayoutComplete (https://docs.unity3d.com/ScriptReference/UI.LayoutRebuilder.html) that looks promising but how can I access this LayoutRebuilder from my objects? I don't see any such variable in the Vertical/Horizontal LayoutGroup class.

    There is also CanvasUpdateRegistry which could also work (what is the different?) https://docs.unity3d.com/ScriptReference/UI.CanvasUpdateRegistry.html but how can I get ICanvasElement out of my UI to be able to register to RegisterCanvasElementForGraphicRebuild and RegisterCanvasElementForLayoutRebuild? Because currently calling IsRebuildingGraphics or IsRebuildingLayout static function always returns false.
     
  2. roulinade

    roulinade

    Joined:
    Feb 8, 2017
    Posts:
    3
    Hello! I know your question is almost one year old, but I had a similar problem today and found a solution:

    Code (CSharp):
    1. function OnGUI() {
    2.    if (Event.current.type == EventType.Layout) {
    3.       // will be executed only after layout has been re-computed
    4.    }
    5. }
     
    t_tutiya likes this.
  3. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    That looks promising! Thank you!

    (Seeing how old this question is and my game still looks sort of the same as back then (with this problem unfixed) really shows developing a game alone really make time flies..)
     
    t_tutiya likes this.
  4. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    The worst bit here is that Unity still hasn't documented this stuff.

    But it's pretty fricking shameful that they went and deleted all your links to their documentation. Because what's better than being too lazy to document your code? Deleting the docs that you did write! HAHAAHAHA now no-one can use it!