- Home /
How do I change GUI elements from outside of the OnGUI function?
I have an array with 200 elements that have to be placed on the GUI layer once. After that, sometimes I will have to delete one or two of these.
Right now I'm using
for(var i=0;i<itemArray.length;i++){
To loop through it all, but it then does this EVERY onGUI call... making it extremely slow. Is there a way to place the elements once and when a change is made in another function only update this single element?
This is the script I'm using inside OnGUI():
for(var i=0;i<AgendaArray.length;i++){ var taskBegin = AgendaArray[i][1]; var taskEnd = AgendaArray[i][2]; var taskName = AgendaArray[i][0]; var taskStatus = AgendaArray[i][5]; var thisStyle : GUIStyle;
if(timer >= taskBegin && timer <= taskEnd && taskStatus != 2){AgendaArray[i][5] = 1;}
else if(timer>taskEnd && taskStatus != 2){AgendaArray[i][5] = 3;}
if(taskStatus == 1){thisStyle = activeStyle;}
else if(taskStatus == 2){thisStyle = passedStyle;}
else if(taskStatus == 3){thisStyle = failedStyle;}
else{thisStyle = inactiveStyle;}
var taskPosition = Screen.width*(taskBegin-beginOfTheDay)/lengthOfDay;
var taskWidth = Screen.width*((taskEnd-taskBegin)/lengthOfDay);
GUI.Box(Rect(taskPosition,Screen.height-35,taskWidth,30),taskName,thisStyle);
}
All of the GUI objects have to be drawn every frame, but you may be able to optimize the script in other ways. Can you give us your entire script?
Answer by Lucas Meijer 1 · Dec 20, 2009 at 09:58 PM
In most cases, you just have to draw your elements every frame. If this is giving you realworld performance problems (measure, not guess!), you could create a texture on the fly, render them into that, and then only render the combined texture at runtime.
Answer by dkoontz · Dec 21, 2009 at 12:56 AM
The Unity GUI system is an "immediate mode" rendering system, which means there's no GUI objects that are created and maintain their own state (although you could create such a system if you wanted to). So each time OnGUI is called you will need to redraw all of your boxes. Now, as others have suggested, you may be able to solve your performance problems in other ways. The most obvious would seem to be moving your logic for determining a style and box width into some sort of Agenda object that can maintain the needed state so you're only recalculating when something changes.
Answer by Peter Alexander · Dec 21, 2009 at 12:54 AM
OnGUI was not designed to be something that you'd actually ship with a product. It's only there so that you can create UI quickly for debugging purposes, or just for placeholders. For a real, shippable UI, you need to roll your own, probably using GuiTexture and GuiText.
I don't know where you got that idea, but it's not correct. Of course OnGUI was designed to be used in actual products. Otherwise it would not have been created. For complex GUI systems, GUITexture/GUIText is a complete nightmare.