- Home /
Optimizing OnGUI - Too many gui elements?
Is there such a thing as too many gui elements? I find that when I deactivate the gui completely, my app runs a LOT faster. Is there a maximum number of recommended gui elements?
Answer by Lab013 · Mar 14, 2011 at 08:17 AM
The OnGUI is quite slow. It gets called twice a frame and it will slow down your game. How much it will slow you down is dependent on the computer, the type of computational work your game will force mostly, and the GUI elements/styles you use. Usually on a computer its not too big a deal. If you find it is a big deal, then you should use cheaper alternatives such as GUITexture (which can still become expensive if you use it terribly).
well, the alternative is to use collision on gameobjects for buttons... would that be better than gui?
Answer by swartzcr · Aug 24, 2011 at 05:17 PM
One way to get around this is to have a GUI Manger script which has the arguements from each normal onGUI passed to it, so that you manager is the only thing running the actual onGUI function, while still displaying what any of the other onGUI functions normally would.
So you would do something like if(clicked){ GuiManager.state = tree; GuiManager.treeHeight = this.height; } on your trees whose height pops up in the GUI when clicked and onGUI{ if(state == tree){ if(GUI.Button(rect(x,y,x,y), treeHeight+"\n close?") } } on your GUIManager, with a slot for all of your previous different onGUIs in it. That way only one script is running onGUI multiple times a frame, but you still get the experience of everything creating a GUI texture when clicked.
(I apologize if my poorly written example code is confusing, but I hope the concept is still clear)
Your answer
Follow this Question
Related Questions
How to Focus GUI.TextArea in Unity for Android 0 Answers
iPhone Touch Screen? 2 Answers
How to use GUIStyles in OnInspectorGUI? 1 Answer
GUI system for android? 1 Answer
Unity Android Remote GUI Scaling issues 0 Answers