Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Unity: How to rescale GUI labels with resolution

Okay so here is my code

Code
void OnGUI()
    {
        if (playerHealth >= 0)
        {
            GUI.Label(new Rect(10, 420, 200, 50), "Health = " + playerHealth.ToString("F0") + " %", healthTextStyle); // "10, 50, 200, 100)" is the location of the health bar
        }
        else
        {
            GUI.Label(new Rect(10, 420, 200, 50), "Health = 0%", healthTextStyle);
        }

        if (playerMass >= 0)
        {
            GUI.Label(new Rect(155, 420, 200, 100), "Mass = " + playerMass * 100+ " %", waterTextStyle);
        }
        else
        {
            GUI.Label(new Rect(155, 420, 200, 100), "Mass = 100%", waterTextStyle);
        }

        if (playerAcid >= 0)
        {
            GUI.Label(new Rect(155, 440, 200, 100), "Acid = " + playerAcid.ToString("F0") + " %", acidTextStyle);
        }
        else
        {
            GUI.Label(new Rect(155, 440, 200, 100), "Acid = 0%", acidTextStyle);
        }

        if (playerFire >= 0)
        {
            GUI.Label(new Rect(155, 460, 200, 100), "Fire = " + playerFire.ToString("F0") + " %", fireTextStyle);
        }
        else
        {
            GUI.Label(new Rect(155, 460, 200, 100), "Fire = 0%", fireTextStyle);
        }

        if (playerEnergy >= 0)
        {
            GUI.Label(new Rect(155, 480, 200, 100), "Energy = " + playerEnergy.ToString("F0") + " %", electricTextStyle);
        }
        else
        {
            GUI.Label(new Rect(155, 480, 200, 100), "Energy = 0%", electricTextStyle);
        }

        if (playerAmmo >= 0)
        {
            GUI.Label(new Rect(10, 440, 200, 100), "Ammo = " + playerAmmo, ammoTextStyle);
        }
        else
        {
            GUI.Label(new Rect(10,440, 200, 100), "Ammo = 0", ammoTextStyle);
        }

        if (coinCount >= 0)
        {
            GUI.Label(new Rect(10, 460, 200, 100), "Coins = " + coinCount, coinsTextStyle);
        }
        else
        {
            GUI.Label(new Rect(10, 460, 200, 100), "Coins = 0 ",coinsTextStyle);
        }

        if (playerScore >= 0)
        {
            GUI.Label(new Rect(10, 480, 200, 200), "Score = " + playerScore, playerScoreTextStyle);
        }
        else
        {
            GUI.Label(new Rect(10, 480, 200, 200), "Score = 0 " + playerScore, playerScoreTextStyle);
        }

        if (gameTime >= 0)
        {
            GUI.Label(new Rect(60, 400, 200, 200), "Game Time = " + gameTime.ToString("F2"), gameTimeTextStyle);
        }
        else
        {
            GUI.Label(new Rect(60, 400, 200, 200), "Game Time = 0:00", gameTimeTextStyle);
        }

In the Unity editor and when playing the text is in the correct place. I have the inspector set to 16:9. But when i compile it and select any resolution the text is in the wrong place and at a different size. Does anybody know how to fix this?

I googled it a bit and seen something to do with scaling with the screen res. But i dont know how to do that.

Thanks
Program it dynamically. Use Screen to get the width and height of your game's current resolution to work out how big and what not your things should be. I also wouldn't recommend programming your GUIs programmatically because, generally, it isn't as modifiable as when you use the built-in GUI builder on Unity. It also can work with scaling and anchors to ensure your components are in the right spot.

Just go to your hierarchy and look for the GUI option, and it'll automatically create the Canvas gameobject and such.
(May 26, 2016 09:19 AM)Yuikoro Wrote:  Program it dynamically. Use Screen to get the width and height of your game's current resolution to work out how big and what not your things should be. I also wouldn't recommend programming your GUIs programmatically because, generally, it isn't as modifiable as when you use the built-in GUI builder on Unity. It also can work with scaling and anchors to ensure your components are in the right spot.

Just go to your hierarchy and look for the GUI option, and it'll automatically create the Canvas gameobject and such.

I regret using GUI labels now but my game is almost finished and i dont want to remake my entire GUI. Hince why i asked how to fix it rather than re-do it
(May 26, 2016 09:26 AM)MrJamieMcC Wrote:  I regret using GUI labels now but my game is almost finished and i dont want to remake my entire GUI. Hince why i asked how to fix it rather than re-do it

When taking into account the UI and player enjoyment it might be worth it. But I won't bother you on that any more

So as I said earlier, just use the Screen class to get the height and width and work out how each component should be placed, along with their size.

The hardest would be figuring out the text font.
-
I suggest programming the resolutions to be dynamic for certain limits. Such as everything from 400x800 and under will have this GUI size and this font size, everything 400-800 x 800-1200 will have this GUI size and this font size, etc.
Whatever sounds easier for you to implement.
(May 26, 2016 09:31 AM)Yuikoro Wrote:  When taking into account the UI and player enjoyment it might be worth it. But I won't bother you on that any more

So as I said earlier, just use the Screen class to get the height and width and work out how each component should be placed, along with their size.

The hardest would be figuring out the text font.
-
I suggest programming the resolutions to be dynamic for certain limits. Such as everything from 400x800 and under will have this GUI size and this font size, everything 400-800 x 800-1200 will have this GUI size and this font size, etc.
Whatever sounds easier for you to implement.

Aaaa yea i understand you now. Thank you very much :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by Lexxi.Dec 8, 2016 02:17 AM 3 195 Dec 8, 2016 02:17 AM
Last Post: Dеvil
[C#] by severosNov 18, 2016 02:11 AM 6 236 Nov 18, 2016 02:11 AM
Last Post: clrlslwi
[.NET] by ChessoMar 11, 2015 08:50 AM 7 289 Mar 11, 2015 08:50 AM
Last Post: ElementalTree