- Home /
Best way to fade in GUI text in Unity 5.x.x?
I've tried to look around but it seems many of the examples I've found are obsolete in Unity 5?
I got a link to the examples here: http://wiki.unity3d.com/index.php?title=Fade#Examples
This looks like something I could use:
Fade.use.Alpha(guiTexture, 0.0, 1.0, 5.0, EaseType.In);
But I'm not sure where to put that in my C# script. Can someone assist?
Thanks in advance!
Answer by Cherno · Feb 10, 2016 at 10:47 PM
If you indeed use the old GUI system, just change the GUITexture's color's alpha value via Coroutine.
If you use the new UI system, give the parent object of the UI objects that should become transparent a Canvas Group component, and change it's alpha to 1 during runtime via CoRoutine.
Thanks so much for your reply!
I don't have to use the old GUI system, let's go for the new.
I found the Corutine has an example of ”fade out” (so I would have to do the opposite) in the Unity Documentation: http://docs.unity3d.com/$$anonymous$$anual/Coroutines.html
The example goes:
IEnumerator Fade() {
for (float f = 1f; f >= 0; f -= 0.1f) {
Color c = renderer.material.color;
c.a = f;
renderer.material.color = c;
yield return null;
}
}
But I get ”parsing error” when I try to put that into a C# script. Guess I'm putting the code in the wrong place and/or missing some other code that's required.
Would it be too much to ask for an example script of that code snippet above in a working C# script? :-| :) Or maybe there's a better way than doing as the code example above?
Answer by staraffinity · Feb 16, 2016 at 12:15 AM
Manged to get it to work by attaching this script to the GUI text object:
using UnityEngine;
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CanvasFadeIn : MonoBehaviour {
CanvasGroup csGrp;
void Awake(){
csGrp = GetComponent<CanvasGroup>();
}
void Start (){
StartCoroutine("FadeIn");
}
IEnumerator FadeIn(){
csGrp.alpha = 0;
float time = 5f;
while(csGrp.alpha < 1){
csGrp.alpha += Time.deltaTime / time;
yield return null;
}
}
}
However, I don't know what that ”csGrp” means? I got the script from elsewhere, not mine from scratch as you could imagine…
Is the ”csGrp” a reference to a specific Canvas Group? Because there can be more than one Canvas Croup in a scene. I don't know of a way to name a Canvas Groups, though – is it possible?
yes, csGrp is a reference to a CanvasGroup component. The Awake function assigns the reference by searching for a CanvasGroup component on the gameObject this script is attached to. It will use the first one it finds, but since CanvasGroups are commonly not used in multiples on the same GameObject, that's ok in this case.
Okay, great. :) One last thing: Is there an easy way to add a certain amount of delay before the start of the fade? I guess it's just to add some lite code snippet somewhere in the above example. I can of course look for it myself…
void Start (){
StartCoroutine("FadeIn", 1.5f);
}
IEnumerator FadeIn(float delay){
float t = 0f;
while(t < delay) {
t += Time.deltaTime;
yield return null;
}
csGrp.alpha = 0;
float time = 5f;
while(csGrp.alpha < 1){
csGrp.alpha += Time.deltaTime / time;
yield return null;
}
}
Your answer

Follow this Question
Related Questions
How to change GUI Text color in a javascript? 1 Answer
can my player collide with more than one objects? 1 Answer
Score wont show 0 Answers