- Home /
Are OnGUI calculations cached?
Currently in my UI scripts I do a lot of calculations for determining the x and y coordinates and the width and length of my component. For example stuff like this:
GUI.Box(new Rect(screenWidth/4,screenHeight/4, screenWidth/2,screenHeight/2), "You must finish your current wave before you can save your game.");
if(GUI.Button(new Rect(screenWidth/2 - (screenWidth/5)/2, screenHeight/2 + screenHeight/16, screenWidth/5,screenHeight/7), "Ok")){
showSaveGameMustFinishWave = false;
}
I'm referring to the new Rect(screenWidth/4,screenHeight/4, screenWidth/2,screenHeight/2 part.
My question is are those calculations calculated twice every frame or are they calculated once and stored in a cache.
The reason why I ask is because if they are calculated every time then it would be smarter to make a bunch of rect variables, define them in the awake or onstart method and then just reference the rects such as:
OnAwake(){
box1 = new Rect(blah,blah,blah,blah);
box2 = new Rect(blah,blahl,blah,blah);
}
OnGUI(){
GUI.Box(box1, "Hi");
if(GUI.Button(box2, "ok")){
//code
}
}
Now I understand I might not see a performance difference in a couple but I have around 300+ of these if you add them all up between my scripts. Some of them as simple as screenWidth/2 and others with a lot of calculations per parameter.
Would it be worth it to define them at the start, or are they cached? If it matters my main platform is Android. I'd also like to port to iPhone eventually. Thanks.
Answer by Eric5h5 · Apr 30, 2013 at 03:29 AM
They are not cached; however it's often the case with modern CPUs that calculating stuff is faster than fetching values from relatively slow RAM.
What would you suggest then? In an example of having a lot of these (over 100 at one time easily) would you suggest I calculate them or store them?
If it makes a difference I already store a lot in memory as it is, in the neighborhood of 100mb+ per level. I know text based things won't take up much but it is already being access frequently is what I'm getting at.
Your answer
Follow this Question
Related Questions
InspectorWindow.DrawEditors high CPU in Editor. 1 Answer
does interpolation full up some kind of cache over time 1 Answer
Performance load of OnGUI 0 Answers
Better performance? GUITextures or Texture2D drawn in function OnGUI? 0 Answers
Which one is better for mobile games many OnGUI function or one complex OnGUI function? 1 Answer