Search Unity

UI elements culling

Discussion in 'UGUI & TextMesh Pro' started by mog-mog-mog, Oct 11, 2015.

  1. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    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?
     
    awsapps likes this.
  2. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    Ping, any help please?
     
  3. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    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 ?
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    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.
     
    DavidRobichaud and jcredible like this.
  5. jcredible

    jcredible

    Joined:
    Dec 4, 2012
    Posts:
    24
    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!
     
    Last edited: Oct 14, 2015
  6. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    @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?
     
  7. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    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.
     
  8. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    @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.
     
  9. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    yea you should be able to use the canvas group in that way.
     
  10. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    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.
     
  11. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    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?
     
  12. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    why you think it is not? I dont think it affects performance. I hope:)
     
  13. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    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?
     
  14. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    I tested and it workd. Be careful not "Mask", you will use "2d rect mask". Attach it to scrool rect or canvas.
     
  15. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    Which unity version did you try? thanks
     
  16. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    5.2.1 p3
     
  17. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    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?
     
  18. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    yes I see. Do not add 2d rect mask while playing. First stop game then add it to canvas and restart.
     
    spark-man likes this.
  19. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    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?
     
  20. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    Idk :) 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.
     
  21. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Yea right not this is required we have a bug where it doesnt notice the change so adding and removing the mask at run time doesnt work as it should. We are working on it and hope to have a fix for 5.3
     
  22. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    It should be "safe" to do that. What happens is it looks for a mask in its parents and sets a flag on itself saying that its clipped so i dont see why it would be a issue multiple levels deep.
     
  23. mog-mog-mog

    mog-mog-mog

    Joined:
    Feb 12, 2014
    Posts:
    266
    Thank you all. My canvas was in world space originally. I've converted it to screen space now and mask on canvas does work now. Thank you @Gokcan and @phil-Unity
     
  24. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    @phil-Unity
    To continue the topic of rendering fix on alpha = 0:
    don't you think that when UI element is excluded(included) from(into) rendering due to alpha it should trigger onCullStateChanged event?
     
  25. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Hmmm thats a interesting thought, my gut reaction would be no due to it still being within the bounds but i can see what you're getting at...
     
  26. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    So may be new event then onVisibleStateChanged? You can also call it when element is not rendered when it's out of Camera due to its position/Z in Screen Space - Camera (though this case fits onCullStateChanged better :) ).
     
    Last edited: Oct 23, 2015
  27. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    you need 2Dsprite for that btw on the game object, it workes all you need is

    Code (CSharp):
    1. isVisible
     
    Last edited: Oct 23, 2015