Last active
December 28, 2015 05:20
-
-
Save grimmdev/b73789f144bd5b612f88 to your computer and use it in GitHub Desktop.
Scaling GUI In Unity OLDUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In most games I need a touch of OLD Unity GUI and for that I need a scaling GUI solution, most out there just don't seem to really work for me. | |
// So I made my own with a very simple method, most designs rely on a target device resolution, if you know what that is. | |
// It essentially means everytime I design something I design GUI for ONE resolution and so I want it to scale to every resolution. | |
// So first we define our design resolution. | |
// For the example we chose a common portrait resolution. | |
[SerializeField] | |
private Vector2 DesignResolution = new Vector2(480,800); | |
// Now we need our Scale variable to store the difference between the design resolution and new. | |
[SerializeField] | |
private Vector2 Scale = Vector2.Zero; | |
// After we do that we would need to make sure to do the calculations and store them within an update function, especially if the | |
// Orientation or rotation changes during gameplay. | |
private void Update() | |
{ | |
Scale.x = Screen.width/DesignResolution.x; | |
Scale.y = Screen.height/DesignResolution.y; | |
} | |
// Now that we have the difference added to our scale variable, we make a new custom function for GUI RECT to except our scale function. | |
// Just add this anywhere in the same script. | |
// <Left,Top,Width,Height,Scale.x+Scale.Y> | |
private Rect ScaleRect(float l,float t,float w,float h,Vector2 s) | |
{ | |
return new Rect(l * s.x,t * s.y,w * s.x,h * s.y); | |
} | |
// Now we just simply utilize our function and scale like so. | |
private void OnGUI() | |
{ | |
GUILayout.BeginArea (ScaleRect(15,105,450,590,Scale)); | |
// etc.. | |
// and when you run this function you'll see it works flawlessly | |
// In order for this to work you need to understand your design resolution and use the design resolution as gui cordinates. | |
// For instance I subtracted 30 from the width to make it add 15pixel space on each side (pre-scale), then | |
// when it scales it divies up the difference and upscales the space accordingly or downscales by appropriate amounts mathematically sound. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you need any help using this, just leave a comment. I thought this was worth posting as I use this method a lot and it doesn't seem to be found anywhere else. Hope it helps!