- Home /
Info on GUI
When I create a button like this:
void OnGUI()
{
if (GUI.Button(new Rect(screen.with/2, screen.height/5, screen.with/10, screen.height/15), "Click"))
Debug.Log("Clicked the button with text");
}
the device performs recursive calculations within the rect?
it is better to create the Rect inside the Auto Start and then recall it? For example:
private Rect rectPos;
void Start()
{
rectPos = new Rect(screen.with/2, screen.height/5, screen.with/10, screen.height/15);
}
void OnGUI()
{
if (GUI.Button(rectPos, "Click"))
Debug.Log("Clicked the button with text");
}
Comment
If you are not to invested in OnGUI its better to upgrade to the new UI tools available in 4.6. They are more efficient, and make complex tasks in OnGUI trivial.
Answer by YoungDeveloper · Dec 03, 2014 at 04:02 PM
Yes! OnGUI is called every frame or even more often. Creating new Rect that every call can seriously bottleneck your fps in no time. Same goes with unnecessary math calculations, parsing and strings. Everything what can should be created on initialization, calculations or number-> string parsing only on change, and so on.