UI elements culling

I’ve scrollable map background images(UI image) and over 300 levels (UI buttons). Unity render all map images and button even if it is not visible. Isn’t Unity 5 Free supports Frustum culling by default? Does it support for UI elements also? If not, what’s the best way to reduce rendering time per frame?

1 Like

Ping, any help please?

As far as I know Frustum culling is not supported with UI. But Unity 5.2 has culling api. There is a new mask component in 5.2 which uses culling api. But it is good to have official answer from unity UI team for this scenario since I also have same thing. @phil-Unity What is the best way to apply for this scenario ?

Yea best way would be to add a 2D Rect Mask to the canvas object. this culls the children which i think is what you want.

2 Likes

Phil, somehow I missed this completely in the 5.2 notes, this is almost too good to be true! Thank you for this.

I’m really hoping I can replace this virtualization container (basically uses an object pool to reduce instantiations) I have with this functionality. Oh man, if this means Unity can support all the data in a scrollrect I want to throw at it, then I’m going to be using this all over the place! I’m so excited!

@phil-Unity I think most people including me did not understand what unity 5.2 brings. For instance, in 5.2, objects with canvas group attached and alpha=0 rendered? I ask because when I set alpha to 0, in stats vert and tris count drops. When I set to 1 they increase! I do same thing in 5.1 it does not affect. I think it always draws. Am I right? Any difference in canvas group between 5.1 and 5.2? If there is any difference, where in document?

not sure what you mean is that not expected? If you set alpha to 0 then its not visible so no point rendering the geometry so verts and tris drop. In 5.1 they would render invisible with is incorrect behaviour. Nothing is different in the canvas group the change is on the rendering side.

@phil-Unity So, if we do not want to render panel, can we simply set canvas group alpha to zero? I am aking because in 5.1 I was disactivating panel when it is not used to not produce extra drawcall. I cannot use 2d rect mask in my case because panels always inside canvas. They fade in and out.

yea you should be able to use the canvas group in that way.

Just wanted to add that the change (very welcomed one) in rendering was added in 5.2.1p1.
And before that to avoid rendering of CanvasGroup with 0 alpha you needed to add Canvas to it.

Is it “safe” performance wise to add 2D Rect Mask to root Canvas with many sub-Canvases / elements so any element which is out of screen isn’t rendered?

why you think it is not? I dont think it affects performance. I hope:)

Thank you @Gokcan for all the help, appreciated.
Tested in latest patch 5.2.1p3
Unfortunately, mask does not work as mentioned by @phil-Unity . Vert/Tris count is still the same with/without mask It does not provide any culling. CanvasGroup alpha=0 does reduce rendering time. Canvas group solution with scrolling is CPU consuming, so I do not see much benefit here.
Any other suggestions?

I tested and it workd. Be careful not “Mask”, you will use “2d rect mask”. Attach it to scrool rect or canvas.

Which unity version did you try? thanks

5.2.1 p3

Do you see any reduction in number of tris/verts in editor by using Mask?
I’ve tried it multiple times, it’s still the same. I am using Unity Free, is it pro only feature?

yes I see. Do not add 2d rect mask while playing. First stop game then add it to canvas and restart.

1 Like

Ortin do you see any performance bugs in 5.2.1p3 to work with it? I am planing to port my project to 5.2 from 5.1. Should I ? Or wait a bit more?

Idk :slight_smile: May be it has some peformance hit on big hierarchies as you need to update new / removed elements to use its scroll rect.

I’m not on 5.2 right now, just using it for experiments, it is (was) too buggy in general. Waiting for 5.2.2 to give it another try.