Scaling gui elements to screen - is this good idea/practice what I'm doing?

Hi there

I am trying to optimize my gui scripts for my iOS game. What I am doing is to check the screen width and height only once, thus in the Start function and then set all my scaling according to that, based on percentage values. For example to set a button’s width to screen.width * 0.1f , but inthe start function, NOT inside GUI.

By doing this I am obviously using the fact that an iOs device screen size can not change. So my question is this. Are there any pitfalls I should be aware of or is this a good way to avoid unneeded float value calculations in my GUI function?

Thanks in advance

you would need alot elements multiplying in your ongui to notice any real difference

but that should be fine

You can do something fancier like

static public class Utility
{
static public Vector2 ViewportToScreen(float x, float y)
{
return new Vector2(x*(float)Screen.width,y*(float)Screen.height);
}
}

Then when declaring these types of variables you can go

Vector2 buttonPos = Utility.ViewportToScreen(.1f,.25f);

and do the whole thing in one line, instead having to then do stuff in the awake function

Ok, but you are still saying that your ViewPortToSreen method should be called once only, correct?

Ya. You would only call it once.

The special thing about that ViewportToScreen function, or setting up any static method like that, is you can declare it outside of a function, so then you can initialize the value when declaring it as a class variable. Saves you a line from having to make a class variable, and then initialize it inside of the Awake function. Its just a cleaner implementation.

for sure, thanks. I will definitely implement this