Do Buttons dirty the canvas?
Original Question: I'd like to know if a button that uses Sprite Swap, will mark the canvas as dirty when the user hovers the mouse over the button?
*Edit for clarity: When certain parameters change in a canvas, it causes Unity to "rebuild" the mesh via Canvas.buildBatch. This is cpu intensive and can cause spikes/hiccups.
This article mentions only Transform changes as a cause of a rebuild: https://support.unity3d.com/hc/en-us/articles/115000355466-Split-canvas-for-dynamic-objects
The article goes so far as saying that changing text does NOT involve a batch build. My question seeks certainty regarding other types of changes such as a button sprite-swap on hover or the tint effect on hover. (Assume there are NO animations)
It's a common graphics / game / development term for when something changes in your rendering hierarchy, requiring either a re-draw or complete re-calculation of the "stuff" that your visuals are made of. (Though technically the term also applies to non-visual things).
https://gamedev.stackexchange.com/questions/160433/what-is-the-meaning-of-component-is-dirty
Of course also unity has to repaint everything that has changed so whats the point of your question?
I'm new to game development, so blissfully unaware of that one sorry :(
You'll need much smarter Devs to answer that one.
Not sure it'll help, but there is a LateUpdate() function you can use to ensure what you are doing is at the end of the frame, if ti$$anonymous$$g of a particular piece of code per frame is important.
Answer by LilGames · Mar 08 at 06:06 PM
I keep asking myself the same initial question and when I Google, I find my own question as a top result, so here's what resources I've found to help me through the whole Static vs Dynamic Canvas decisions:
.
A simple tutorial about correctly setting up the UI hierarchy of mixed Static and Dynamic parts of your UI: https://learn.unity.com/tutorial/nested-canvas-optimization-2019-3#
.
A deeper tutorial explaining a 2-canvas setup and using the profiler to see the impact: https://learn.unity.com/tutorial/working-with-static-and-dynamic-canvases#5ffbd711edbc2a002099bd00
Answer by xxmariofer · Apr 22, 2019 at 11:15 AM
anything that changes the canvas will force a canvas repaint, not only images, scroll bars buttons animations, anything. thats why you should separate canvas depending on when their objects get dirty, if you have a button that is animated, it should go in a separate canvas from the static buttons.
I guess this is where I am looking for more specifics. I assume that animation, scaling, rotation cause a canvas rebuild, but would a simple alpha or tint change? What about sprite-swap method if the sprites are all from the same atlas?
Thank you for your time.
it changes always, even if is such a tiny thing as a tint change, unity doesnt "check" what has changed in the canvas, just as an example, if you have an animation of a button that doesnt do anything the first 5 seconds and the last 5 seconds it applies a tint chaange, the canvas will be redrawn all 10 seconds (all the time, since it doesnt know if something has changed so you would repaint the exact same canvas during 5 seconds) here are some performance tips for canvas uses Link
Redraw/repaint is fast compared to rebuilding the mesh: https://support.unity3d.com/hc/en-us/articles/115000355466-Split-canvas-for-dynamic-objects
Answer by Bip901 · Mar 28, 2020 at 07:57 PM
According to Unity 2017 Game Optimization by Chris Dickinson, the only action which doesn't dirty the canvas is changing the Color
property of a UI element.
Hence I assume a color tint transition won't rebuild the canvas, but a sprite swap will.