- Home /
Performance load of OnGUI
I was stress testing my scene where I had 1000+ objects with a script attached with an OnGUI() function in the script. The FPS I got jumped between 5fps and 60fps (Vsync off). By removing the OnGUI function which wasn't really needed, the performance jumped to 500fps. Is there an alternative to OnGUI that puts less stress on the CPU?
Thanks!
The new UI system has been a part of Unity for several years now.
However, I'd wager that if you have thousands of objects in your scene, all with scripts attached, you might want to consider keeping that number down.
Alright, I'll give the new UI system a go then :)
I don't actually need 1000+ objects in my scene, I was just pushing the limits to see where I could possibly optimize. Turns out it was the OnGui() function and an un-used Update() function. Thanks!
You should in general remove any of the callback methods that Unity calls for you when you don't need them. The way Unity calls those callbacks is very clever. It remembers which classes implement which callbacks and thus it's very efficient even when you have thousands of objects / script instances.
OnGUI itself isn't that of a problem, however each "control" will have it's own drawcall (sometimes even more than one like a button). For standalone builds a "normal" GUI shouldn't be a big problem, but if you have many controls and / or build for a weaker platform (mobile for example) OnGUI is a no-go.
The new UI system tries to keep as much as possible in a single drawcall.