Performance of an empty OnGUI?

I’ve read a number of things about OnGUI that leave me a little unsure of how to proceed with a state machine framework I’ve written. Currently the framework will handle an OnGUI call and just return immediately if that particular state has no requirement for GUI.

It also does the same for FixedUpdate and LateUpdate. Now I could easily write a bunch of different base classes that didn’t do this, but it won’t be very neat any more and will potentially lead to hard to find errors if one someone defines a SomeState_GUI method but the associated behaviour isn’t handling it. My view, if performance is hammered though, forget the neatness :slight_smile:

So what I think I’ve read is this:

  • An empty OnGUI has a significant performance overhead. I can imagine that the presence of an OnGUI in a behaviour might make Unity do some set up and tear down that would cause this to be true.
  • Profiling OnGUI shows a significant overhead which is in fact the Editor doing stuff, this isn’t the performance you would expect running on a device - therefore it is hard to accurately profile the OnGUI code.

So can anyone tell me if it is true that such an effectively empty OnGUI statement could make a significant performance difference? Also any insight into the potential pitfalls of having empty FixedUpdate/LateUpdate would be interesting too.

I was wondering the same so I’ve run some tests. I’ve create a scene with 10k Game Objects, each one with an empty script. Than added an empty OnUpdate to the script and than replaced the OnUpdate with the other functions.

My results were this cost per frame:

Empty: 1.2ms
OnUpdate: 1.4ms
FixedUpdate: 3.2ms
LateUpdate: 4.3ms
OnGUI: 300ms

So (at my machine) roughly an empty method costs per frame:

OnUpdate: Basically nothing.
FixedUpdate: 0.0002ms
LateUpdate: 0.00041ms
OnGUI: 0.03ms

So looks like the unique method to worry is OnGUI.

I hope it helps.