- Home /
GUI Rect and 0.000 floats
Anyone explain to me why this is making my GUI rect not work?
new Rect (0,0,(persistence.sheight / 1.6), persistence.sheight);
The goal is to set up my GUI so that no matter what device it loads on it has some basic screen ratio built in and will stretch the rects to look decent size. The issues seems to be that my (persistence.sheight / 1.6) formula returns a double in the form of a ###.### value that goes out to the 3rd decimal position.
in my persistence which is a data pass between scenes I push screen.height and screen.width to sheight and swidth floats.
Answer by Bunny83 · May 21, 2012 at 06:32 PM
I guess you use C# (guess from the new keyword). You should devide by a float value to get a float value:
new Rect (0,0,(persistence.sheight / 1.6f), persistence.sheight);
However magic numbers inside the code should be avoided. Additionally multiplications are a little bit faster than divisions so i would predefine your factor like this:
// C#
float aspect = 1 / 1.6f;
// [...]
new Rect (0,0,(persistence.sheight * aspect), persistence.sheight);
I'm not sure that thing about division is true anymore on more recent CPUs. I've run benchmarks on my Xeon in Unity and + - / * are all apparently the same speed.
Answer by whydoidoit · May 21, 2012 at 06:28 PM
You need to cast the double return value to a float to use with the rect:
new Rect(0,0,(float)(persistence.sheight/1.6), (float)persistence.sheight);
Can you mark it as answered? I can always use the $$anonymous$$arma ;) And it takes it off the list...