Screen rescaling Ratio Problem

Hi,

To get my viewer(GUI) always at the right size and position Im using that function:

    void Start()
    {
        //ScreenRatio
        horizRatio = Screen.width / 1920f;
        vertRatio = Screen.height / 1080.0f;
        print(Screen.width);
        print(Screen.height);
        print(horizRatio);
        print(vertRatio);
    }

    //=========================================================================================================

    void Update()
    {
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ScreenRatio

        //---------------------------------------------------------ScreenRatio
        FSW = Screen.width;
        FSH = Screen.height;
        Ratio = FSW / FSH;


if ((Ratio) == (16.0f / 9.0f))
        {
            //Screen Ratio
            SW = 1920f;
            SH = 1080f;

            //Pages
            HelpX = 1084;
            HelpY = 721;
            LoadX = 300;
            LoadY = 59;
            //PhotoAlbum
.....
if ((Ratio) == (16.0f / 10.0f))
        {
            //Screen Ratio
            SW = 1920f;
            SH = 1200f;

            //Pages
            HelpX = 1084;
            HelpY = 721;
            LoadX = 300;
            LoadY = 59;......

        if ((Ratio) == (4.0f / 3.0f))
        {

            //Screen Ratio
            SW = 1280f;
            SH = 1024f;

            //Pages
            HelpX = 1084;
            HelpY = 721;
            LoadX = 300;
            LoadY = 59;.....}

        horizRatio = Screen.width / SW;
        vertRatio = Screen.height / SH;

}

    //=========================================================================================================

    void OnGUI()
    {
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Utilities

        //Aspect Ratio
        GUI.matrix = Matrix4x4.TRS(new Vector3(0f, 0f, 0f), Quaternion.identity, new Vector3(horizRatio, vertRatio, 1));
       ....

It always worked fine, but now I just redo a complete new viewer and some time It seems like it doesn’t rescale it so all my viewer is wrong.

If I press play with Maximize on Play everything is fine, if I didn’t select the Maximize on Play sometime it goes wrong sometime it doesn’t and if I manually scale the game window when it’s good it sometimes goes wrong and sometimes it fixed it.

I really don’t know what’s wrong everything is exactly the same as before…

The only suggestion I have is to put your rescaling code into OnGUI instead of Update. Update is called once per frame and OnGUI is called whenever the GUI is to be redrawn, so it’s probably more appropriate for it to be in OnGUI.

Then again, I don’t know why that would affect it.

nop it doesn’t change anything… :S

it’s random really… and that’s makes it more and more difficult to find where is the problem…

new observation:

if i change the size of the game window in maximize on play = off when the viewer is wrong, it always fix it.

This might not at all be the issue, but often floating-point values are not exactly the value they say they are. For example, 1.2 might actually be 1.200000001.

So what concern me are the 16.0f / 9.0f bits. You could write a function like this:

function ApproxEquals(float a, float b)
{
    return (a <= b + 0.01  a >= b - 0.01);
}

Then you can check your ratios with:

if (ApproxEquals(ratio, 16.0f / 9.0f))

Yes I just did the same observation in fact it doesn’t accept the 4/3 ratio condition(if) even if the screen is in 4/3 ratio

I’ll try your Approx.

How can I return a bool with your function ? in Visual studio it says that It can’t convert function into a bool for the If line
I’m scripting in C#.

Thank’s a lot for your help by the way

Oh, I’m not so good with JS, so I assumed it would work. You could explicitly type the function:

function ApproxEquals(float a, float b) : boolean
{
    return (a <= b + 0.01  a >= b - 0.01);
}

well, C# doesn’t like at all the “) : boolean” thing :smile:

Oh, you’re using C#. Derp, sorry, totally my bad. I just realized what I posted is a disgusting hybrid half-attempt at converting C# in my head to JS anyway. Ignore that.

private bool ApproxEquals(float a, float b)
{
   return (a <= b + 0.01  a >= b - 0.01);
}

ok I was sure I did saw that approx lately:

if (Mathf.Approximately(Ratio, 16.0f / 9.0f))

But it doesn’t seems to work too,

with yours I can change the number for the approx.

Problem solved with your function!!!

Thank’s a lot mate !!

Well there you go, I guess the ratios weren’t being tracked very accurately. Glad to be of help.