- Home /
Simple way to change GUIText font size
Hi guys!
Is there no simple way to change GUIText font size? Currently the text size looks nice in iPhone screen but when it comes to Retina Display, it becomes very small. I've looked for solutions but it seems GUIText.fontSize is only for dynamic fonts. Is there any other way to adjust the font size from the script without having to create custom font? Thanks in advance! =)
Edit: To make things clearer, these all are the steps I've done:
Script part:
private var infoText:GUIText;
private var theFont:Font;
var fonts:Font[];
function Awake(){
infoText = GameObject.Find("Info").GetComponent(GUIText);
infoText.enabled = false;
}
function OnGUI(){
if(iPhoneGeneration.iPhone3GS){
theFont = fonts[0];
}else if(iPhoneGeneration.iPhone4){
theFont = fonts[1];
}
}
function PostScore(){
.
.
if(hs_post.isDone){
infoText.text = "Score uploaded";
infoText.font = theFont;
infoText.enabled = true;
}
}
function CheckUsername(){
if(userName.Length == 0){
infoText.enabled = true;
infoText.text = "Please put your username";
infoText.font = theFont;
}
}
Those are where I access the "infoText" from the script
For the settings:
I hope I didn't miss anything.
Just import a custom font. Chances are it'll look better anyway.
Yeah I have the same problem with various android devices and the best way I have found is to import a custom font for each.
This is what I get for trying to import custom font
"Dynamic Fonts are not supported on this build platform"
Apparently, the documentation says "Dynamic fonts are currently only supported on Desktop platforms ($$anonymous$$ac and Windows)." so I guess it can't be used for mobile device, no?
Ok for whatever font you are going to use, you are going to have to make multiple copies of it for every font size that you need. So say your font is called Arial and you need a version in 16 point for iPhone and in 24 point for Retina Displays. $$anonymous$$ake two copies of that font. Name one something like Arial-iPhone and the other Arial-Retina. Drag both of them into your project folder. Then click on the first one in the project view and you should see the "true type Font Importer" component in the inspector set the font size to 16 or whatever you need there and then change the Character from the default of dynamic to whatever is supported by your platform likely "ASCII default set" Do the same for your other font but set it to the other size that you need. That should take care of that "Dynamic Fonts are not supported on this build platform" error ...that is unless somewhere in your code or one of your GUIStyles is trying to set one of those fonts to a different size.
This was sort of already covered in this answer: http://answers.unity3d.com/questions/20599/how-do-you-increase-gui-text-font-size-.html
And this link to docs could be helpful: http://unity3d.com/support/documentation/Components/class-Font.html
Answer by JDPennock · Mar 15, 2012 at 07:38 PM
Is it working for you now?
OnGUI is called multiple times every frame so its not the best idea to put the check in there. This should work a little better:
private var infoText:GUIText;
private var theFont:Font;
var fonts:Font[];
function Awake(){
infoText = GameObject.Find("Info").GetComponent(GUIText);
infoText.enabled = false;
if(iPhoneGeneration.iPhone4){
infoText.font = fonts[1];
}
else{
infoText.font = fonts[0];
}
}
function PostScore(){
.
.
if(hs_post.isDone){
infoText.text = "Score uploaded";
infoText.enabled = true;
}
}
function CheckUsername(){
if(userName.Length == 0){
infoText.enabled = true;
infoText.text = "Please put your username";
}
}
you could also add an extra variable and an enum to make you code a little more clear and give you the ability to switch between the two fonts in the inspector easily.
private var infoText:GUIText; private var theFont:Font; var fonts:Font[]; enum $$anonymous$$yFonts { Iphone = 0, Retina = 1 } var CurrentFont : $$anonymous$$yFonts;
function Awake(){ infoText = GameObject.Find("Info").GetComponent(GUIText); infoText.enabled = false;
if(iPhoneGeneration.iPhone4){
CurrentFont = fonts[$$anonymous$$yFonts.Retina];
}
else{
CurrentFont = fonts[$$anonymous$$yFonts.Iphone];
}
}
function PostScore(){ . . if(hs_post.isDone){ infoText.text = "Score uploaded"; infoText.font = CurrentFont; infoText.enabled = true; } }
function CheckUsername(){ if(userName.Length == 0){ infoText.font = CurrentFont; infoText.enabled = true; infoText.text = "Please put your username"; } }
sorry that is hard to read, I'm not quite sure how to make code show up correctly in the comments here.
That works, thanks a lot for bearing with me xD I didn't expect that changing text size only applies for Dynamic Fonts and quite a tedious way to resize a text here =(
Answer by stevenp · Mar 16, 2012 at 03:22 AM
Ok, that works. But why I can't mark the answer!? I can't even comment on the answer @_@
Edit: Ok, that was weird, took like 1/2 hour to be able to mark it.
Answer by maspi · Jun 25, 2012 at 07:04 PM
Seems that Unity 4 (probably in August 12) will solve the problem. One little but great feature: "* Dynamic fonts on all platforms with HTML-like markup" see: http://www.marketwire.com/press-release/the-next-generation-of-the-unity-game-engine-unveiled-1670512.htm hopefully …
Answer by maspi · Jun 25, 2012 at 07:06 PM
Seems that Unity 4 (probably in August 12) will solve the problem. Just one little but great feature of all: " Dynamic fonts on all platforms with HTML-like markup" see: http://www.marketwire.com/press-release/the-next-generation-of-the-unity-game-engine-unveiled-1670512.htm hopefully …