1
\$\begingroup\$

I am working on a project and now I have a problem . The problem is the font size. The font size is OK inside the Unity but it is really small when I play the game on a real Android device .

My Canvas has 2 text (a and b) and 1 image .

I created an empty game object to hold that text.

Here is the code attached to empty game object :-

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class Text_Script : MonoBehaviour 
{

public Text My_Text_1 ;
public Text My_Text_2 ;

// Use this for initialization
void Start () 
{
    My_Text_1.text = "Hello";
    My_Text_2.text = "World";
}

// Update is called once per frame
void Update () 
{

}
}

Here are the images :-

Inside Unity

enter image description here

Inside a Android Device (here I uploaded the image from BlueStacks , I didn't use my mobile)

enter image description here

So why this is happening ?

I tried this :-

function Start() 
{
     var myText = GetComponent(GUIText);
     // 50 is the fontsize you want to use
     myText.fontSize = Mathf.Min(screen.height,screen.width)/50;
}

but I don't know where to put and on which object should I put those code And I am using Unity 5 and that code is for Unity 4.x .

I don't want to scale the font for a large size as explained in YouTube video (I don't think this is a good method)

So is there any ways to change the font size according to the screen size and resolution .

Please help me

Thanks:)

\$\endgroup\$
1
\$\begingroup\$

GUIText is one of Unity's legacy GUI elements, pre-4.6, so this might feel outdated, but still works.

Define your base resolution height. Say you're working with a base height of 500 pixels:

var uiBaseScreenHeight:float = 500;

Now, what happens if the Android device's screen height is, say, 1500 pixels, which is 3x larger? You'd want to increase the font size:

// Dynamically determine the UI scale to use
var uiScale:float = Screen.height / uiBaseScreenHeight; // eg 1500 / 500 = 3

// Set the base font size
var baseFontSize:int = 16;

// Set the scaled font size
var scaledFontSize:int = Mathf.RoundToInt(baseFontSize * uiScale);

// Apply the scaled font size to the GUIText element
myText.fontSize = scaledFontSize;

And if you start working with GUITextures, you can adjust the pixelInset.width and pixelInset.height properties similarly by multiplying base widths and heights against uiScale.


C# example:

public float uiBaseScreenHeight = 500f;

private int GetScaledFontSize (int baseFontSize) {
    float uiScale = Screen.height / uiBaseScreenHeight;
    int scaledFontSize = Mathf.RoundToInt(baseFontSize * uiScale);
    return scaledFontSize;
}

void Start () {
    My_Text_1.fontSize = GetScaledFontSize(16);
}

The other method, is to use Unity's new UI Canvas and elements instead. Right-click in your Hierarchy and choose UI > Text. This will create a Canvas with a UI.Text element. Check the Canvas' CanvasScaler component and play around with it; make it use "Scale With Screen Size" and choose your base resolution.

\$\endgroup\$
6
  • \$\begingroup\$ Thanks , Can you give me c# code . I am not good at JavaScript \$\endgroup\$ Jan 7 '16 at 19:59
  • \$\begingroup\$ How can I define uiBaseScreenHeight ? And Where should I attach that script ? Regards \$\endgroup\$ Jan 7 '16 at 20:10
  • \$\begingroup\$ I've added a C# example now. I think I was getting confused with your UnityScript example code at the bottom of your post, and overlooked that you were using C# with UI.Text above. If you use just the CanvasScaler with "Scale With Screen Size", you won't actually need to use any code at all to change font sizes. \$\endgroup\$ Jan 7 '16 at 20:48
  • \$\begingroup\$ But it is giving 3 errors . Error 1 :- Assets/Scripts/Text_Script.cs(16,17): error CS0127: Text_Script.GetScaledFontSize(int)': A return keyword must not be followed by any expression when method returns void Error 2 :- Assets/Scripts/Text_Script.cs(16,17): error CS0029: Cannot implicitly convert type int' to void' Error 3 :- Assets/Scripts/Text_Script.cs(25,27): error CS0029: Cannot implicitly convert type void' to `int' \$\endgroup\$ Jan 8 '16 at 8:18
  • 1
    \$\begingroup\$ Thanks :) But I solved it by Canvas Scaler UI Scale Mode . Thanks for your answer . \$\endgroup\$ Jan 8 '16 at 10:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .