OnGUI gets called multiple times per frame. It gets called at least twice, once with a layout event and once with a repaint event. If you click a GUI element it gets called with mousedown and mouseup (not always in the same frame). It can go higher in some circumstances.
In contrast Update will only get called once per frame.
Based on this you would typically prefer Update over OnGUI, unless you are using the GUI or GUILayout functions. For something that does not change every frame you would typically not do it in either function, but only call it when the value changes.
Maybe. It depends on where the bottlenecks are, which depends on what kind of hardware it is and what you are asking it to do. But the short answer is that, if you re worried about performance you should avoid unnecessary work, especially every frame.
To use the unity GUI and GUILayout drawing functions you must put them in OnGUI. That is its purpose and it ensures that they get drawn on top of everything else. Many of them simply will not function in the context of Update.
In terms of doing other work, both OnGUI and Update get called once per frame so you should be limiting the work you do in either to only what is necessary.
true that
– Jeff-Kesselmanso will the use get drop in fps if i use Update for a lot of codes which dont need to be called every frame?
– ShnayzrMaybe. It depends on where the bottlenecks are, which depends on what kind of hardware it is and what you are asking it to do. But the short answer is that, if you re worried about performance you should avoid unnecessary work, especially every frame.
– Jeff-KesselmanIn general you are better off to build the functioning game first, then optimise later when you can actually find your bottle necks.
– Kiwasi