Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using a few hundred world space canvases is killing performance

Discussion in 'Scripting' started by mrCharli3, Jul 13, 2021.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    I have about 159 AI on screen, each can have different icons over their heads showing things like hunger, happy, unhappy, etc.

    I attach a world space canvas to each AI, and simply activate it when it needs to show something. Meaning if I have 159 hungry AI, Ill have 159 active canvases. Keep in mind there is nothing else than 2 images and a TMP text in these canvases, no custom scripts.

    Without these canvases active my game runs at 130 FPS.
    With them active, it runs at 50 FPS.

    Is there some more performant way to render simple 2d textures as icons without using a canvas?

    upload_2021-7-13_17-59-2.png

    Edit: I realize now this prob is the wrong topic for this, should probably be moved to UI topic.
     
    PutridEx likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    That's already a fair number of additional GameObjects (159 x (canvas + tmpro + image + image)) so it might just be bumping the limits, especially considering canvas and RectTransforms and UI stuff has to rebuild its meshes if things change.

    Assuming each one doesn't change per frame, you could perhaps get a benefit by using a RenderTexture to "strike" each one to a texture and use that on a quad card until it needs to be updated. It's kinda fiddly but that sort of optimization isn't too uncommin.
     
  3. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    207
    Do you have all the sprites rendered in those world canvases in one sprite atlas? Check the FrameDebugger, how many draw calls you are making.
    Other than that, you can try to optimize canvases itself, like remove raycasters if you don't need them, don't raycast TMP. Do you animate those canvases?
    Or you can try Overlay/Screenspace canvas and track the icons from script, but I doubt this will actually help. From my experience, many world space canvases are faster than one big overlay that is constantly dirtied. I didn't have any problem with 90+ worldspace canvases, so I think you are doing something costly here.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Agree with this the more I think about it... check if you're not blasting the same text at the TMPro item each time... it might be naively reconstructing its mesh, not sure how smart they are internally about being handed the same string over and over... but the code is all there to inspect!
     
  5. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    207
    Yeah, check on your TMP if you have "autoSize" on. Autosize + animating/resizing every frame is a performance killer.
     
  6. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    All really good suggestions.

    I dont set the text from anywhere, it's not changed from its prefab.
    I dont have autosize, raycast or any other setting that could be eating performance on the TMP text. Nor do I have raycast on anything else in the canvas.

    I did a deep profile and tbh its quite spread out voer all the "canvas functionality" it seems, so I don't think there is just 1 culprit. Looking at this I feel like low poly 3d icons that are batched would be more performant.

    upload_2021-7-13_18-39-16.png
     

    Attached Files:

  7. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,115
    TMP itself also has a none canvas option (might be more performant). Maybe you can drop the canvases altogether and just render all your stuff in "regular" 3d. What feature of the canvas do you require exactly?
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Do you absolutely have to draw all 150+ of these on screen at once? Seems like more information than a player might reasonably be able to parse, no?
     
    bobisgod234 and RadRedPanda like this.
  9. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    This is a good point, it's worth considering if having all of these Canvases on screen would actually improve your user's experience, or if it would be better to use them sparingly, only when prompted by the user. Two birds with one stone ;)
     
  10. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    No, absolutely not, I intended to merge icons similar to cities skyline. But this was more a question on why canvas seems to perform so poorly.
     
  11. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    I did not know that, will check it out! I use images as background for the icon since just using white icons would not work for when there is snow, but an outline could fix that.
     
  12. swingingtom

    swingingtom

    Joined:
    Feb 15, 2018
    Posts:
    9
    @mrCharli3 At the end, how did you achieved this task and what would you teach us now? Thanks!
     
  13. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    I switched to using billboarded sprite renderers. Using an atlas it is extremely cheap.
     
    swingingtom and Ellwood23 like this.