Search Unity

TextMesh Pro Long spikes from TextMeshProUGUI.Rebuild for no reason

Discussion in 'UGUI & TextMesh Pro' started by dadude123, Jul 21, 2018.

  1. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    789


    There's a container, a VerticalLayoutPanel, that has ~600 child lines.
    Each line has 2 sub objects that each have a TextMeshProUGUI component.

    The layout itself is very optimized, I'm disabling the vertical layout once I know it won't change anymore, and only enable it for one frame to do its thing when I know the children must have changed.
    The list entries themselves are not using any layout components, they're completely driven by anchors.

    Everytime my main-window moves, TMP rebuilds every textmesh object.
    Why can't that be cached or even completely avoided? It doesn't generate the mesh in worldspace, does it?

    Why is Rebuild() even called from the layout system when no local positions have changed.
    It's literally only the gameobject above the container (the window) that changes position.

    Any tips how I can avoid this?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I believe the parent moving would trigger / lead to the UI / Canvas system thinking everything needs to be rebuilt. In this case, the TMP text component would get a call from the UI system to rebuild itself claiming the RectTransformDimensions have changed.

    This is frequent when using Stretch mode anchor positions.
     
  3. petergraham003

    petergraham003

    Joined:
    May 5, 2018
    Posts:
    14
    Each time I enable a UI screen in worldspace, CanvasUpdate.PreRender is getting called from which TextMeshProUGUI.Rebuild is getting called and causing a performance hit. I have tried solutions like removing StretchMode anchor positions, having nested canvas, correcting long point float values but nothing worked out. That performance hit cannot be ignored as it is causing a great jitter to user. Can anyone provide me a workaround or solution for this problem ?
     
    GuirieSanchez likes this.
  4. Rafael_CS

    Rafael_CS

    Joined:
    Sep 16, 2013
    Posts:
    162
    Canvas Layout works invalidating parent of type ILayoutGroup

    So... if you enable an IlayoutElement and the parent of ILayoutElement is a Layout Group, each IlayoutElement child of this group will be recalculate, regardless if only one child need this recalc (And if the parent of layout group is a layout group too, same thing happens to all children's.. until find a parent == null or a parent that is not a LayoutGroup)

    This approach of unity to recalculate layout prevent inconsistencies but generate HUGE overhead when activating and deativating objects.

    Only way to prevent this huge overhead is implementing your own layout group parent (and not inheriting from ILayoutGroup)

    I thing this is a huge mistake of unity layout rebuilder implementation.

    Another thing to prevent the huge lag spikes and random recalcs is to disable Pixel Perfect in root canvas. Pixel perfect call Transform Sync to every object of hierarchy on every layout recalc or transform move.

    Of course, text mesh pro could try implement a custom bool var to check if need recalculate layout when call CalculateLayoutHorizontal. This way will prevent huge overheads while activating and desativating objects
     
    Last edited: Feb 14, 2020