OnGui() Causing lag, am I doing it wrong?
Hey guys,
When I added this line of code, my game gets super laggy.
void OnGUI()
{
if (nanoStage1States == NanoStage1States.Level1)
{
pressed = GUILayout.Toggle(pressed, "Toggle me !", "Button");
Debug.Log(pressed);
}
Debug.Log(nanoStage1States);
}
When level1 becomes true, the fps drops from 70-80 to like 20-30. The profiler also pointed to it. I commented it out and there lag went away.
Is there a better way to do this?
Thanks a lot :)
bool tessst = false;
void OnGUI()
{
if (nanoStage1States == NanoStage1States.Level1 && tessst == false)
{
pressed = GUILayout.Toggle(pressed, "Toggle me !", "Button");
Debug.Log(pressed);
Debug.LogError("IF TOGGLE IS NOT THERE, IT $$anonymous$$AY HAVE APPEARED FOR ONLY A SEC");
tessst = true;
}
Debug.Log(nanoStage1States);
}
Hmmm, this has less lag but the toggle button doesnt show, I did get the LogError only once, So I'm assu$$anonymous$$g the toggle button was there for one frame?
Well, either way, the GUI is impressively inefficient in large quantities. If you have a simple text label and duplicate it a few dozen times, your framerate tanks.
That said, I can't say I know exactly why a single GUI object would hit quite so hard, but just as a thought, you may want to try comparing your current use of "GUILayout.Toggle()" with "GUI.Toggle()" to see whether one may be doing significantly worse than the other. If that also doesn't improve it, I would recommend testing a variety of GUI pieces to see whether it's only that one type impacting framerate so much, or if it is even something like having more copies of that GUI object in the same place than you might think.
Then, if all else fails, I believe the new GUI system (well, "new" now that it's been around for a few versions) may provide better performance.
Thank you for the response. I've barely played with UI so this is new for me lol. Is it actually supposed to call the OnGUI every frame like that? Can I not just create it once? I feel like my script is creating a toggle button everyframe?
Yep. In fact, OnGUI() is called twice per frame, in general.
In order to keep something displayed, you'll have to state that you want it displayed every frame (i.e. in OnGUI()), so that aspect of it's pretty much normal.
But, that's not really important or a big deal as far as that's concerned. That's simply the GUI behaving the way it's supposed to. $$anonymous$$ainly, just bear in $$anonymous$$d that for every GUI object you create, you're generally adding one more draw call to your scene. So, if you're looking to display a large amount of information on screen in the GUI, it might not hurt to try and condense it into as few separate objects as is reasonably possible.
I only added the debugs to find when the lag was happening, this was before I found out there was such a thing as profiler lol. The profiler pointed to OnGUI()
Answer by Highwalker · Jan 24, 2016 at 05:15 AM
OnGui is notoriously slow!! I would just avoid using it.
Your answer
Follow this Question
Related Questions
Massive (>1sec) lag spikes in game, within 10 seconds of starting 1 Answer
Game FPS suddenly slow on Android 1 Answer
How to check to see if Switch Case is true. 1 Answer
What is UnityChoreograp? 0 Answers
Garbage Collection Constant Lag Spikes? 0 Answers