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

Why is Canvas.BuildBatch taking so long?

Discussion in 'UGUI & TextMesh Pro' started by DMeville, Jan 6, 2015.

  1. DMeville

    DMeville

    Joined:
    May 5, 2013
    Posts:
    418
    I'm working on an Android project, and everything was silky smooth until I added UI. Now the game is stuttering with just a few UI objects. Is there something obvious I'm doing wrong?

    The game starts with a simple "Menu" Scene, with just a camera and a canvas with a few UI objects (two texts, one button). This scene itself is slow to respond when clicking the button (which loads the "Game" scene)

    This next scene has a dynamic text element that get updates every update tick, and reducing rate at which I'm setting the textElement.text value doesn't help any performance.

    Eventually when the game is over I set the "GameOver" UI element to active, which is just a UI text and button which is disabled during gameplay.

    Any idea what I can do to improve performance?
    I've read that using "BestFit" on UI text is poor, but none of my text elements have best fit checked.

    Screen Shot 2015-01-05 at 10.19.17 PM.png
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    can we see a image of what your menu is like that is causing the slowness
     
  3. DMeville

    DMeville

    Joined:
    May 5, 2013
    Posts:
    418
  4. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    How large is the image in pixels?
     
  5. DMeville

    DMeville

    Joined:
    May 5, 2013
    Posts:
    418
    Which image? The only asset related thing I'm using for the UI is the font file, everything else is the default UI stuff, I've loaded in no image.

    The screen size in pixels varies from device to device as the UI is "fullscreen", the largest is around 960x600
     
  6. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    My mistake I was thinking the background was am image too. I am blind.
     
  7. DMeville

    DMeville

    Joined:
    May 5, 2013
    Posts:
    418
  8. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Hmm i'm not seeing anything that should cause any slow down. could you submit your project as a bug through the bug reporter?
     
  9. ForceVFX

    ForceVFX

    Joined:
    Jan 21, 2011
    Posts:
    611
    A couple of suggestions..I was having the same problem, I did a comparison on duplicate projects/UI 4.5 UIToolkit vs.4.6 UI..and was losing up 30fps.. a huge and scary drop..

    1st). Turn off SpritePacker and use this [TexturePacker]..it is well worth it!
    You have to think what that service(SpritePacker) is trying to do every frame, it is very complicated, especially with dynamic objects/font size variants...OVERHEAD!!

    Do it once with TexturePacker, not every frame..simple

    2nd). Come up with some sort of naming convention, pretty please..

    Patrick---

    I did that and my problem was solved!!
     
    Last edited: Jan 7, 2015
  10. DMeville

    DMeville

    Joined:
    May 5, 2013
    Posts:
    418
    @ForceVFX: Hmm, that's an interesting idea, and I can see how it would be helpful. The thing is I'm not using any custom graphics, just a single font file and that's it. Is there an option to use a texture atlas instead of a font file in the UI Text objects?

    @phil-Unity: I will do that, thanks! I just realized I'm on an older version of 4.6, so I'll update first. Who knows maybe that will do something.
     
  11. DMeville

    DMeville

    Joined:
    May 5, 2013
    Posts:
    418
    I did some more testing and found that removing the line of code that sets a UI Text text value fixes the problem of Canvas.BuildBatch() taking so much time. Previous tests showed that reducing the update rate didn't affect it much, so perhaps I didn't reduce it enough or something, but removing it altogether works. I guess that makes sense, as it needs to rebuild every time the text is changed...?

    Now that I know what the root of the problem is maybe I can find a better way. Is there any existing ways to have dynamic text that is "faster"?

    I'll create a simple project and submit that as the bug report.

    EDIT: The simple project reports Canvas.BuildBatch() taking only 3.5% time. So it must be something with the nesting of UI objects.

    After additional testing it seems removing the Panel UI object speeds everything up, so this is the offending object. Very strange.


    I've removed the panel object from my UI and everything is going smoothly, even while setting text on a textfield every update.
     
    Last edited: Jan 14, 2015