- Home /
How can we procedurally animate GUIText.fontSize?
The goal is to define procedurally an animation clip on a GUIText component. I tried (C#):
Animation animation = this.GetComponent<Animation> ();
AnimationClip clip = new AnimationClip ();
clip.SetCurve ("", typeof(GUIText), "fontSize", AnimationCurve.Linear (0.0f, 17.0f, 1.0f, 19.0f));
animation.AddClip (clip, "Font Size Test");
but it does not work...
Answer by VoxelPerfect · Dec 14, 2012 at 08:06 AM
For some reason the fontSize is not present in the Animation View of the Unity Editor either. Anyway, we can animate fontSize "manually" as follows (C# MonoBehavior subclass excerpt):
private AnimationCurve curve;
private float t;
void Start ()
{
this.curve = AnimationCurve.Linear (0.0f, 17.0f, 1.0f, 19.0f);
this.t = -1.0f;
}
void OnGUI ()
{
if (t < 0.0f) {
t = 0.0f;
} else {
t += Time.deltaTime;
Keyframe[] keys = this.correctAnswerCurve.keys;
// in case of loop
if (t > keys[keys.Length-1].time) {
t -= keys[keys.Length-1].time;
}
}
this.guiText.fontSize = (int) Mathf.Round(this.curve.Evaluate(t));
}
Since fontSize is of type int, animation is not smooth but it is a good compromise if we have requested "Pixel Correct" rendering. Otherwise we would just use scaling on the Game Object's transform to achieve the same effect...
Your answer
Follow this Question
Related Questions
Animation not looping 1 Answer
Parameterized Animation Clip 0 Answers
Animation Help Needed (Method Not Found) 2 Answers
Animation created via script does not play 1 Answer
Animation Error 0 Answers