- Home /
Increase text size over time c#
HI,
I'm trying to increase text size over time, but getting an int - float error with the time part.
public class Opening : MonoBehaviour {
public GUISkin skin;
bool title = true;
int size = 10;
bool closedCapOne = false;
bool closedCapTwo = false;
bool closedCapThree = false;
bool closedCapFour = false;
bool closedCapFive = false;
void Start () {
StartCoroutine(Open());
}
void Update () {
while(title){
size += 4 * Time.deltaTime; // Smooth time?
}
}
IEnumerator Open () {
CameraFade.FadeOutMain(0.1f);
yield return new WaitForSeconds (5);
title = false;
}
void OnGUI () {
GUI.skin = skin;
skin.label.fontSize = size;
if(title)
{
GUILayout.BeginArea(new Rect(0,0,Screen.width,Screen.height));
GUILayout.Label("Game Title");
GUILayout.EndArea();
}
}
}
How would I get around this in unity?
Answer by Lockstep · Mar 18, 2013 at 12:26 AM
You can make size a float and then round it to int to set the fontsize:
float size = 10;
void OnGUI () {
GUI.skin = skin;
skin.label.fontSize = Mathf.RoundToInt(size);
An integer and a floatingpoint number are two fundamentally different things. Intergers are the numbers { -32768 ,... -1, 0, 1, ..., 32767}, floats are saved as significant digits and an exponent. Converting from int to float is simple but you can't make an integer out of 1.5 directly. Thus you need the round to int funktion.
Thanks that worked, But of course.. Thanks to it being int, you cant have the text looking smooth.
I'm trying to do something similar, using a GUIText that just lives in the world. It works great on the desktop and web, but on iOS it causes all the other GUIText on the screen to glitch out. Is this a known issue? (I'm posting here since others who use this trick might hit the same snag.)
Answer by Owen-Reynolds · Mar 18, 2013 at 12:39 AM
Nothing to do with Unity. size (and fontSize) are ints -- can't add fractions to them. Use the rounded value:
float size; // float will make the deltaTime line happy
...fontSize = (int)(size+0.5f); // rounded
The while loop in update isn't needed -- Update runs over and over already. 4*Time.deltaTime
is a trick to increase by 4 points/second. Otherwise you have to guess the increase/frame that comes out to 4/second.
I was using a while so if I needed to open it again, the label wasn't some insanely high number. But then I learnt using a while inside update crashes unity.
Then if(title)
may be what you want.
You can put a while inside Update, but it has to naturally end on its own, such as `while(i>0) { .. i--; }. It can't depend on something that might turn false after a few updates.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Any idea why my int wont display on GUI 1 Answer
buttons don't work if I use int 1 Answer
How do you change the size and color of a GUI Label in C#? 4 Answers
Changing a GUI String to read as a INT 2 Answers