- Home /
how to re-scale guiStyle property as per screen resolution?
Hi, I've used guiStyle property to display score board on the screen with background image and some text assigned with font size and padding position. I've designed based on 768x1024 screen resolution. Now I'm testing different resolutions and i did for background image to re-scale to fit as per resolution but i can't able to re-scale the font size and padding position.
GUI.Label(screenRect(0.0f,10.0f,246.0f,146.0f),m_distance.ToString() + " m\n\n" + m_score.ToString(), scoreStyle);
Here screenRect(0.0f,10.0f,246.0f,146.0f) function will re-scale 768x1024 resolution (xPosition,yPosition,objectWidth,objectHeight) to fit current screen resolution.
Answer by aeau2012 · Nov 19, 2013 at 07:49 PM
First, create a Vector3, lets call it Scale where the X will be the screen.width/your_resolution_width_Objective, the Y will be the screen.height/your_resolution_height_objective, and the Z will be 1.
Then multiply all of your GUI sizes width Scale.X , GUI sizes height Scale.Y.
For the font you will create a variable float and lets call it NewFontSize, you will multiply the currentFontSize * Scale.X, and then assign to scoreStyle.fontSize the NewFontSize, in this way the font will be scale.
This is a sample code:
using UnityEngine;
using System.Collections;
public class ScaleAllGUI : MonoBehaviour {
public float originalWidth = 768.0f;
public float originalHeight = 1024.0f;
private Vector3 Scale = new Vector3(0,0,0);
// Use this for initialization
void Start () {
Scale.x = (float)Screen.width/originalWidth;
Scale.y = (float)Screen.height/originalHeight; // calculate vert scale
Scale.z = 1;
}
//ALWAYS MULTIPLY YOUR REAL GUI WIDTH OF EACH OBJECT * SCALE.X AND YOUR REAL GUI HEIGHT OF EACH OBJECT * SCALE.Y
void OnGUI()
{
//Put all your GUI here (button,label,toggle,etc)
if(GUI.Button(new Rect(Your_position_on_x, Your_position_on_y, The_width_of_this_gui * Scale.x, The_height_of_this_gui * Scale.y),Your_texture))
{
}
GUI.Label(new Rect(Your_position_on_x, Your_position_on_y, The_width_of_this_gui * Scale.x, The_height_of_this_gui * Scale.y),Your_texture);
//Now for the font one, we create a variable and we multiply the fontSize of the style * Scale.x
float NewFontSize = Scale.x * current_font_size_of_your_style;
scoreStyle.fontSize = (int)NewFontSize;
GUI.Label(screenRect(0.0f,10.0f,246.0f * Scale.x,146.0f * Scale.y),m_distance.ToString() + " mnn" + m_score.ToString(), scoreStyle);
}
}