- Home /
UI Scrollbar movement causes layout rebuild
Hi guys, having an issue with lots of nested layout groups. Whenever I scroll a scrollbar the profiler shows the recursive call of calculating all layout elements sizes. I'm not sure how to stop the recalculation. It should not happen in this scenario because the scroll rect is not changing how any child elements should render. Is there any way for me to not have layout groups auto-recalculate and let me, instead, forcefully call a method on them when I want them to recalculate? It would not be difficult to add on that manual code to the established code base I already have. It would be much preferred, but I am not sure how to make it happen!
Any tips would be appreciated, thanks! I know I layout groups are supposed to be used seldomly, but it's a necessary evil for what I am coding. Thus, manually updating them would solve all my issues; just not sure how to do that
Hi, Not sure if this is exactly what you mean, but you might be able to find useful information in this post, scroll or search for scroll views.
https://learn.unity.com/tutorial/optimizing-unity-ui#5c7f8528edbc2a002053b5a3
"Despite these issues, all approaches can be improved by adding a Rect$$anonymous$$ask2D component to the Scroll View. This component ensures that Scroll View elements that are outside of the Scroll View’s viewport are not included in the list of drawable elements that must have their geometry generated, sorted and analyzed when rebuilding a Canvas."
It's a good read even if it isn't exactly what you're after. All the best.
Thanks you! This does help. I have not yet solved the issue, but it may be possible with this resource. I see that the page walks you through the rebuild process and what layout elements do. It also has links to source code which should definitely answer my questions after enough snooping
Answer by Ossi101 · Jul 28, 2019 at 05:58 PM
If you are not updating your UI layouts during gameplay you can disable all your layout elements prior to testing/building your game. The resulting positions of these layout elements persist even though they are no longer active. This will help reduce recalculations of these elements at runtime. The only issue here is if you add to a dynamic list of UI objects. In that case you would need to enable all appropriate layout elements then disable them again the next frame to have the new objects be consistent with your initial layout values.
I've tried this and it improves performance by about 3x, but its not entirely sufficient because 30fps over 10fps isnt near the amount of total performance I should be getting on my relatively powerful PC. I believe, somehow, Rebuild is still being called in the Profiler timeline. I cannot give a definitive answer there because my editor has just decided to render less than 1 frame per second while the profiler is running...
If you haven't done so already try regrouping the UI elements that are static (ones that don't animate, fade or move) into their own canvases. Also the ones that you do modify in such a way into their own as well. That way recalculations only happen on the UI objects that need it. This is a bit tedious but it's the only way I've found to optimize UI performance issues outside of layout groups.
Another thing to keep in $$anonymous$$d are transparencies. If something like a large panel used for fading in and out, if not disabled properly, can compound issues with your other UI objects. $$anonymous$$ake sure you disable canvases that are fully transparent after they are no longer needed and only enable them again if you are going to do work with them. Overlapping transparent UI objects will still trigger their rebuilds even if you cannot see them.
Thank you for the information. I am currently writing up my own code to layout elements effectively. Unused objects are disabled, and re-enabled when necessary. 'Layout' calculations only happen when I need them to. It's pixel pushing but it works for my scenario and using polymorphism, the work of each object that needs to render is less on me because of code re-use. Thank you for you time :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612205634im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI layout groups rearrange children 0 Answers
Rescalable button with text and image 0 Answers
Importing XAML Files 0 Answers
Heavy use of layout groups and content fitters performance 1 Answer
[UI] Profiler scroll rect 0 Answers