- Home /
How to set up Same sized text on all screens?
So i have been looking around a lot, and I can't find a good solution that doesn't require payment. I'm looking for a way to set the font size to a certain size that will be the same on all devices. Ex: My current font size works well on the gs4, but on my friends gs2, it is way too big. I read something about setting the font to Unicode, but I can't figure out how to do that. Please Help!
in script you will set the screen resolution.below method using to Screen.SetResoltion(480,800,true); Like that can u . can u find ur gs4 resolution size to fix in ur script.
But will this work on all devices? I don't want it to work on just one.
Answer by Scribe · Apr 03, 2014 at 12:47 AM
This should work on the devices you mentioned:
private var resolutionIndex : int = 0;
private var screenRes : Resolution;
private var resChanged : boolean = true;
private var displayDPI : float;
private var mmToInch : float = 25.4;
private var fontSize : float;
var textHeight : float = 10; //the desired text height in mm
function ResolutionUpdate(){
Screen.SetResolution(screenRes.width, screenRes.height, true);
if(Screen.dpi == 0){
Debug.Log("Could not find the dpi of this display.");
displayDPI = 300;
}else{
displayDPI = Screen.dpi;
}
resChanged = true;
}
function ActualToPixel(){
return (displayDPI*textHeight)/mmToInch;
}
function Start(){
screenRes = Screen.currentResolution;
ResolutionUpdate();
}
function Update(){
ResolutionUpdate();
}
function OnGUI(){
GUI.skin.label.fontSize = ActualToPixel();
GUI.Label(Rect(0, 0, Screen.width, Screen.height), "AaBbCcDdEeFfGgHhIiJj");
GUI.skin.label.fontSize = 20;
GUI.Label(Rect(0, Screen.height-50, 200, 50), textHeight.ToString());
}
I tested this first on my laptop however my laptop doesn't seem to supply a value for Screen.dpi so it doesn't work there. I tested this on my Android GS3 and it looks like it works in producing a font size to equal a certain number of millimetres in height so I am hoping/assuming it will do the same for any device that it can get a Screen.dpi value from.
Scribe
In the script I change the globally used GUI.skin so if you take out line 38 GUI.skin.label.fontSize = 20;
then anything that uses the default style will be resized accordingly, then you can just stick this wherever you want, the camera or a fontsize$$anonymous$$anager gameobject or something.
If your question has been answered I'd appreciate if you marked it as such, or please explain any problems you are still having and I can try to help!
Scribe
I'm testing it out now, but I have been out of the house for a few days. I will get back to you if I see any problems.
So, right now, just testing it out on my phone, it's too big. Is there any fudge factor to play around with? The text should be half this size
Answer by arkariarn · Apr 02, 2014 at 11:45 PM
hmm, just an idea but maybe you can check for the screen resolution and change the font size of the gui accordingly, or change the gui.skin itself to a gui.skin with a smaller fontsize. your code would be something like this: if(screen.width < 400){ //change gui.skin or fontsize if possible}