- Home /
Scaling GUI Components to Screensize
Hello,
I've been working on a GUI for a character screen (2D / PC standalone/ 16:9/ using OnGUI), with the usual collection of components (buttons, labels, boxes - all % of screen dimensions). It looks okay on the game scene in unity, but when I build it, the components scale, but the text remains the same size.
Is there some easy way to scale font size with screen dimensions? Should I start making actual graphics with the text in them? (I'm guessing these would scale). If so how should I go about calculating what size to make them?
EDIT
The following code is throwing this error: Ignoring invalid matrix assigned to GUI.Matrix. The matrix needs to be invertable. Did you scale by 0 on the Z axis?
Trying to figure out how to use GUI.Matrix, as suggested by RazorCut. Added this code into Awake on the script that has the GUI.
void Awake()
{
// Get Screen Dimensions
screenWidth = Screen.width;
screenHeight = Screen.height;
// Get
tScaleX = screenWidth/1366;
tScaleY = screenHeight/768;
}
And this code just after the call to OnGui()
Vector3 tOffset = new Vector3(0.0f, 0.0f, 0.0f);
Quaternion tRotation = Quaternion.Euler(0, 0, 0);
Vector3 tScale = new Vector3(tScaleX, tScaleY, 1);
Matrix4x4 tMatrix = Matrix4x4.TRS(tOffset, tRotation, tScale);
The GUI is keeping aspect ratio, but the font size doesn't increase with resolution. (This is how it worked before adding the matrix, I think) Also, in some smaller resolutions, a scrollview I added disappears.
Thanks for your help!
Answer by RazorCut · Jan 05, 2013 at 06:15 PM
If you are using GUI.matrix to alter the scale of your GUI then the text will also scale. If you aren't using GUI.matrix, then you will have to brute force it for sure.
Thanks, will look into that now. If you don't $$anonymous$$d, I'll leave the question open for a bit, to see what other suggestions come up!
Can you give me a bit more info on what else I need to get this working?
Vector3 tOffset = new Vector3(0.0f, 0.0f, 0.0f);
Quaternion tRotation = Quaternion.Euler(0, 0, 0);
Vector3 tScale = new Vector3(1.0f, 1.0f, 1.0f);
$$anonymous$$atrix4x4 t$$anonymous$$atrix = $$anonymous$$atrix4x4.TRS(tOffset, tRotation, tScale);
GUI.matrix = t$$anonymous$$atrix;
So assu$$anonymous$$g you only want to adjust scale, then your goal will be to supply the tScale vector with x and y values appropriate for the screen dimensions being rendered to versus the screen dimensions that the GUI is designed for. You'll also probably want to keep your aspect raio the same so that you don't get squashing and stuff. For example if you design your GUI elements to fit perfectly on a screen that is 1024x768 but it's being drawn on a screen that is 512x768 (this is fake, but an example) then you would want to set tScale.x to 0.5. Basically you could specify the values of tScale in the Awake() function, comparing some reference screen dimentions to the actual screen dimentions to get the tScale values.
No luck so far. I've updated the question, if you'd like to take a look!
Are you setting the matrix to GUI.matrix in the OnGUI() method?