UnityGUI display delay

I'm not sure if I'm doing something with UnityGUI that's not recommended, or if it's common for there to be delays the first time labels are displayed. I have up to 2 seconds of delay sometimes when displaying labels. However, after displayed the first time, it is smooth after that.

I assume this delay is from Unity doing something to create the label for the first time. Is there a way to do all this first-time prep before displaying stuff so it displays immediately the first time?

Could you post some source code? This sounds very strange to me, I've never encountered any such behaviour.

3 Answers

3

OnGUI code is immediate mode, which means it's recreated and drawn every frame. So basically it's impossible for there to be any inherent delay; either it's drawn or not. Something in the way your code works is probably the issue.

Alright, I'll see if I can give you a simplified example after looking at my code.

There is definitely a delay the first time GUI elements are drawn. I assume this is because they have to be created the first time, but are drawn from a buffer after that. I noticed that it only happens on my Macbook, not on my beefier Windows desktop PC. I suppose it may only be noticeable on slower PC's.

There is a 256x256 image on the main menu, and a 16x16 image, but that's it. Everything else is text of various styles. I have 5 GUI modes that display different text. The first time I go to any of the modes, there is the pause. But when I exit the mode and return to it, there is no pause. There's really nothing fancy going on. Here is my entire GUI script code in its entirety: https://gist.github.com/891849

@Gillissie: that's a fairly complex script with several interdependencies with other things you haven't posted, so I can't really say from looking at it and there's no way I can try it myself. Try commenting stuff out until the delay goes away, and figure out what's causing it from there.

Same problem here. A 5sec freeze the first time a GUI.Label is drawn. Seeing it on iPad build, no delay in editor or desktop builds. Using McAden's solution for now.

I had the same issue and i solved it. When i load a level that has some UI Elements, it does a delay. In my case the problem was the font of the UI Text (which is Arial by default). I changed it to Helvetica and the delay is gone. It seems odd but it solved my problem.

Not 100% efficient but maybe this can work:

var startgame = false;
function Update(){
if(startgame == true){
//There go your code
}
}
function OnGUI(){
//Create your label
if(startgame == false){
startgame = true;
}
}

The why of this question I don't know maybe is error of Unity or maybe is yours but this code will make that your game starts when the GUI's are shown

No need at all to go about that business. See the event order. OnGUI is called after Update. I'm sorry but it is completely redundant. http://unity3d.com/support/documentation/Manual/Execution%20Order.html

Also: http://www.unifycommunity.com/wiki/index.php?title=Event_Execution_Order

By the way I am not quite sure what you're trying to do with this snippet. Delay Update until OnGUI has been called once? What is it that you're trying to solve here?

Is because at the final of the question he ask if there is a way to first appear the GUIs and then the do the other stuff so with this as you say update is first but in this case it can not do anything because startgame is false so I GUI after set the label startgame = true so the update function will ask again and it will pass because is true