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.