Search Unity

Event system performance pretty bad

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by Velo222, Jan 21, 2015.

  1. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,260
    Hello,

    I noticed my game getting "magically" worse and worse performance, so I decided to profile it and see what was up.

    I noticed that in my Profiler, "BehaviorUpdate" was taking up about 30-40% of my CPU usage. Going further, EventSystem.Update() is taking up ~15.8% of my CPU usage. This is rather large and substantial.

    In real performance I see that, without the EventSystem, my game will run at about 58 - 67 fps. When I enable the required EventSystem and scripts for the uGUI to operate, I'm getting 40 - 48 fps. It's almost eating up a clear 10 -15 fps, if not more.

    Since we cannot edit the EventSystem scripts ("Event System" and "Standalone Input Module" scripts) ourselves, is there a way to get better performance from these? I'm assuming something in the Update() of these scripts is running extremely fast to detect any gui clicks or whatever, but perhaps there could be an option to run a little slower on something, so as not to eat up so much cpu usage?

    If this has been addressed already I apologize. Thank you.
     
    Leon700 and ilmario like this.
  2. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,260
    Okay, I figured out my performance problem with the event system, and just figured I share it, in case anyone else is slightly dumb (like me) (j/k of course ;) -- although sometimes I can be a bit dense lol)

    The problem was essentially that I have a ton of world space UI Canvases in my game. Each of these canvases has a "graphics raycaster" script, because I think they're put there automatically when you create a canvas (could be wrong). My fix was that I did NOT need graphic raycasting checks on all of those canvases. The event system was having to try to keep track of all of those objects, and thus the "Eventsystem.Update()" was having a really hard time. Getting rid of those "graphic raycaster" scripts freed up a ton of CPU time in my game.

    The good news is that the "Event System" script and the "Input module" script are not terrible for performance in and of themselves. This is great news for me, because I thought it was on Unity's end, and it turns out not to be (unless you need a lot of graphic raycaster checks).

    Hope that might help someone new to the UI system that uses a lot of world space UI canvases -- or a lot of canvases in general. Of course, everyone's game is different though. I guess I can chalk this one up to my inexperience with the new UI system thankfully :)

    So far, I'm really liking Unity 5.0. Thank you Unity team. Keep up the hard work.


    *I don't think I can change my thread title now, otherwise I would.
     
    Meceka, FardinHaque, MGGDev and 31 others like this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,379
    I'd never thought to remove the raycasters from canvases that don't need them. Thanks for the heads up.
     
    gl33mer likes this.
  4. Shorodei

    Shorodei

    Joined:
    Dec 7, 2013
    Posts:
    8
    It'd be very useful to know what conditions cause memory allocation in the event system. My profiler indicates about 400bytes being allocated each frame by EventSystem.Update, and there is some connection to the number of world space canvases present on screen, but even accounting for it there are some weird sources that are unclear to me.
     
  5. pointcache

    pointcache

    Joined:
    Sep 22, 2012
    Posts:
    510
    omfg the savior, thats trully great observation thank you very much
     
    gl33mer and Peotor like this.
  6. chmodseven

    chmodseven

    Joined:
    Jul 20, 2012
    Posts:
    34
    This is a handy tip, especially for any floating text or similar effects in the world space that don't need to be interacted with. It also works for screen space canvases, although if your canvases have any buttons that need detecting on them, then the raycasters are obviously still going to be needed.
     
    Leon700 likes this.
  7. Peotor

    Peotor

    Joined:
    Jan 26, 2014
    Posts:
    2
    Thank you! That solved my problem too! Now I will always check that. I had 3 canvas-raycasters, so when i got rid of them, perfomance just jumped up to 200 fps!
     
  8. PabloUnityArgentina

    PabloUnityArgentina

    Joined:
    Oct 13, 2014
    Posts:
    7
    Thank you very much Velo222!
     
  9. alteregogi

    alteregogi

    Joined:
    Apr 13, 2015
    Posts:
    8
    LOL! got around 40%ish on my EventSystem.Update() , deleted Graphic Raycaster that didn't interact with users, and got 3% on my EventSystem.Update() !! Thanks!!
     
    MGGDev likes this.
  10. Carlotes247

    Carlotes247

    Joined:
    Jun 12, 2014
    Posts:
    8
    Thank you very much! It is very useful for me too! :D
     
  11. moh05

    moh05

    Joined:
    Nov 26, 2015
    Posts:
    65
    Thanks man! Something I never thought about :)
     
  12. Vitalik_Sev

    Vitalik_Sev

    Joined:
    Mar 2, 2013
    Posts:
    1
    Last edited: Jan 16, 2016
  13. crash664

    crash664

    Joined:
    Nov 22, 2012
    Posts:
    91
    Thanks for answering your own question! :) Saved me some headaches!!
     
  14. DivideByNil

    DivideByNil

    Joined:
    Dec 4, 2015
    Posts:
    15
    I have about a dozen menus at this point, and I meticulously disable the raycasters, animators, and canvases (making sure each parent has a canvas added so that I can disable the rendering), yet there is a small but noticeable EventSystem.Update() / BehaviourUpdate creep subtly affecting my project each time I add a UI element, even if I disable its components.

    It's not critical, but I'd just like to share this is really, really bad :p

    Maybe I need to disable my buttons, text, canvas groups, sliders, panels, and scroll rects when not in use... sigh... and all this because there are horrid spikes from outright disabling UI objects instead of their components.

    Any suggestions for managing several menus outside of tips already shared in this thread?

    P.S. Here's a tip: If you animate your menus with different animations, disable each animator when you aren't using it for a performance boost.
     
  15. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    432
    Do you really need more than 1 canvas per scene? I just use sub-objects to them.
     
  16. Koesys

    Koesys

    Joined:
    Jul 20, 2015
    Posts:
    17
    Thank you man for answering your own question :)
     
  17. The_Mean_Fiddler

    The_Mean_Fiddler

    Joined:
    Nov 27, 2014
    Posts:
    7
    Great info that's really useful.
    I've just realised something that you guys probably know about. I was testing my app in unity editor and I had the EventSystem selected in the scene view. The inspector is displayed which slows it right down
     
  18. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,212
    Judging by this thread, Unity should stop adding that component automatically and instead add a little info text area warning, so users may be aware.

    It's not smart catering for people with zero experience, then ruining their performance. What's smart is just letting them know what's going on in an intuitive way so they can deal with it themselves.
     
    dadude123 likes this.
  19. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,379
    I'm a bit on the fence. On the one hand not adding a GraphicsRaycaster would improve performance. On the other hand not adding a GraphicsRaycaster means none of the buttons would work.

    Considering about half the comments on my YouTube videos on the UI amount to 'Did you put on an event system?', I'm not sure manually adding a Raycaster makes sense for new users.
     
  20. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,212
    Hence the info box thing, they do it for some parts but not this evidently.
     
    Kiwasi likes this.
  21. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,379
    I definitely agree it could be handled better. Even an in editor console warning would do the trick. "You have an intractable element, but no raycaster. Please add one to the canvas". The same way something like two audio listeners is handled.
     
    dadude123 likes this.
  22. malcolmmaima

    malcolmmaima

    Joined:
    Jan 28, 2017
    Posts:
    2
    I've been struggling with this for about a week now, game was running smooth then one day all of a sudden I build the game and serious lag!! SERIOUS!! can barely do anything, tried getting rid of the raycaster in the canvas, setting Vsync on and off, anti-liasing on/off still lag. I'm stuck. No changes to my code from previous smooth running version. I'm almost giving up on my game :-( I've attached a screenshot of my profiler. Any solutions?
     

    Attached Files:

  23. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,379
    Well the profiler says is in your FixedUpdate. Why not click the drop down arrow there and see what is causing the problem?
     
  24. nitroy2k

    nitroy2k

    Joined:
    Jun 27, 2013
    Posts:
    6
    Same problems here Im on my 3d build (same project) game was smooth and then it died....
    We used collaborate...
    Now I am doing everything on my own machine and I daily backup complete folder in case I get in to the problem again..
    Canvas was definitely problem but not only canvas the panel...
    I had 3 panels PauseMenu, DieMenu, LevelEnd and they all had dark panel image with opacity kinda transparent, and my game was killing it...
    Removing elements from canvas didn't solve it until I fully deleted Canvas and Event system, then added it again to scene..
    Adding just 1 panel as a test makes game go laggy.. so problem is in unity..
    @ this moment I'm @ 5.6.0b11 version same S*** happened from 5.5.0f1 on all versions..

    My game always had perfect performance 1ms 1000 FPS (this is smooth now)
    Screen Shot 2017-03-12 at 12.22.45 PM.png
    But nothing much was different before but game was jerking..

    I started building game by adding 1 by 1 element and building it to device and testing :) that is how much in detail I had to go frustrating but finally I made it work
    Any way conclusion of mine is that Unity's UI system is still very bad... Canvas is killing game.. and it needs to be updated..
     
  25. dadude123

    dadude123

    Joined:
    Feb 26, 2014
    Posts:
    784
    You guys seriously need to learn to use and actually read and understand the profiler.

    Just opening it won't help. It's quite simple, open the little arrows to see where the CPU and gpu time is going...
     
    Kiwasi likes this.
  26. shcipwa

    shcipwa

    Joined:
    Jan 18, 2014
    Posts:
    55
    Thanks OP for the tip, just saved us a bunch of time.

    Something else we found that may help performance of EventSystem:

    The graphic raycaster has a property eventCamera that it implements by checking for the Canvas.worldCamera, if null calls Camera.main (and doesn't cache the result).

    Camera.main looks to be doing some really slow operation like FindObjectsOfType<Camera> which starts to get really slow when you have thousands of gameobjects in the scene.

    Making sure you explicitly set Canvas.worldCamera on all canvases will solve this problem.
     
    dadude123, ilmario and gesicht like this.
  27. Untherow

    Untherow

    Joined:
    Jun 19, 2016
    Posts:
    7
    Really helpful observation! Thank you!
     
  28. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    339
    In 2018 we have an option to "Deep Profile". This allows you to expand the stack, and see where these percentages are actually spent. In the end it's probably going to show that EventSystem invokes one of your heavy scripts. Deep profile will help to see which one.
    dp.PNG