- Home /
Scale UI Text
I've been having an issue with my User interface for some time now. When ever i scale the canvas, the text remains the same size and its messes with everything. If i try to scale the text in a script like this :
public Canvas Reference;
public Text WIPTxt, MainMenuTxt;
public float SF, tmp;
void Start () {
if (Reference.scaleFactor != 0)
{
SF = Reference.scaleFactor;
}
else
{
SF = 1;
}
TESTTEXT.text = SF.ToString();
tmp = WIPTxt.fontSize;
tmp *= SF;
WIPTxt.fontSize = (int)tmp;
tmp = MainMenuTxt.fontSize;
tmp *= SF;
MainMenuTxt.fontSize = (int)tmp;
}
The text becomes exponentially smaller with the decrease in resolution. The next thing i tried was Content Size Filter. This just ruined everything so im not going near it again. Which leaves me here. I have no idea what to do so im hoping someone here can point me in the right direction because i am clearly missing something,
Out of curiosity, are you trying to scale it based on screen size? I came up with a script that scales UI elements by screen size, using a ref size.
using UnityEngine;
public class SceenSizeScaler : $$anonymous$$onoBehaviour {
public Vector2 refSize;
void Awake()
{
Vector3 size = transform.localScale * (Screen.width / refSize.x);
transform.localScale = size;
}
}
And to get ref size
[$$anonymous$$enuItem("Tools/Current Screen Size")]
public static void ShowScreenSize()
{
Debug.Log("Screen Dimensions are: X: " + Screen.width + ", Y: " + Screen.height);
}
By default, the resolution within the Canvas Scaler is set to 1920x1080. This scales everything on the UI already so i have no issues with this at all. The issue comes from the Text font size, which is what im trying to modify within the script in my answer. $$anonymous$$y train of thought was, If the screen resolution decreases, then the scale factor would decease one. I could then take this value, and multiple it with the font size to get a scaled font size. But this becomes too small.
Edit: One solution i keep seeing is the "Best Fit" property. I really dont want to use this as it makes all the text on the screen appear different sizes and just looks messy.
In my experience with UI Text, it's best to pick a high font size then scale the transform of the text object.
On your canvas set int the Canvas Scaler Script, set the Ui Scale $$anonymous$$ade to "Scale with screen size" now try. if thats not quite it, adjust you anchor points.
you will notice my buttons do not scale this is because i set them to center pivot for this example. the Full Games Pack Text Component is on an empty game object on the canvas background image that is on the canvas itself. what a mouth full Background expands the whole canvas, and empty object is set to expand width only and only because myself was getting an odd expanding error where the Full Games Pack Txt was moving up and down on screen size change, and i didn't like that so i locked the empty game object to be stuck at the top of the background and expand width only and the text component expand on it in full
I've done this on all the Canvas' in my scene but this doesn't work. The actual text field scale with the screen size, but the font size doesn't change. This leads to text boxes appearing empty because the text doesn't fit.
are you setting the font size in code? try removing it and set the text component to best fit and have a max and $$anonymous$$ size.
Your answer
Follow this Question
Related Questions
Finding all text labels in a scene 1 Answer
UI Text 2 Color Horizontal Fill With Sliding Dividing Line 0 Answers
Instantiating a UI element Screen.height and Screen.width way off 0 Answers
UI text visual glitching 1 Answer
How do you create text that can be the child of a non-canvas object? 1 Answer