- Home /
How to make GUI Text Scale to different Resolutions?
Hi, I have a GUI text that needs to be scaled to different sizes depending on the screen, the score transforms and adds +1 each time a object is tapped, this is the score system script ( C# )
using UnityEngine;
using System.Collections;
public class Score : MonoBehaviour {
public Transform clicks;
public GUIText score;
public bool clicked = false;
static int click = 69;
public void Start (){
}
public void Update (){
score.text = " " + click;
if (click == 0)
Application.LoadLevel("Superman");
}
void OnMouseDown (){
click += -1;
}
}
Answer by sillanstudios · Jul 12, 2014 at 01:16 PM
Hi There, Just un tick the "pixel correct" box. You should be able to scale it with the transform component. It may look a bit blocky so go the where you have stored your font in your project and increase the font size property. Cheers! :D
Answer by zharik86 · Jul 12, 2014 at 10:21 AM
See my pregoing response for scaling GUIText and GUITexture. Or see my second response
I used your C# Script and placed it onto the GUIText and this returned. /TextScale.cs(4,6): error CS0116: A namespace can only contain types and namespace declarations
$$anonymous$$ost likely you copied, but it isn't clear as well as where. As I understood, you used a script from the second example. I will a little reduce it as GUIText is necessary to you only. (replace all text in your script on written by me):
using UnityEngine;
using System.Collections;
public class TextScale : $$anonymous$$onoBehaviour {
//Base value of resolution screen for which the text is made
public float origW = 1200.0f;
public float origH = 720.0f;
void Start() {
float scaleX = (float)(Screen.width) / origW; //your scale x
float scaleY = (float)(Screen.height) / origH; //your scale y
//Find all GUIText object on your scene
GUIText[] texts = FindObjectsOfType(GUIText) as GUIText[];
foreach(GUIText myText in texts) { //find your element of text
Vector2 pixOff = myText.pixelOffset; //your pixel offset on screen
int origSizeText = myText.fontSize;
myText.pixelOffset = new Vector2(pixOff.x*scaleX, pixOff.y*scaleY); //new position
myText.fontSize = origSizeText * scaleX; //new size font
}
}
}
That's all. Attach this script to any object in scene. For example $$anonymous$$ainCamera.
P.S.: If there are errors, it is possible to receive more detail information.
Your answer
Follow this Question
Related Questions
Why this Error when trying to add a score using the new GUI ? 0 Answers
How do I build a GUI that works on different sized screens? 3 Answers
Score going up every second 1 Answer
GUI Problem 1 Answer
GUI Text Score counter 3 Answers