So I’ve got a script that change the GUI.text after a time period and I want to make it slowly disappear before the next one appear and not just change but I have no idea how to do that , can anyone help me ?
there is my script :
using UnityEngine;
using System.Collections;
public class untrotext : MonoBehaviour {
void Start () {
StartCoroutine(showStuff());
}
// A predone set of messages over time
IEnumerator showStuff() {
transform.guiText.text="Firing...";
yield return new WaitForSeconds(2);
transform.guiText.text="Missile is fired";
// put some text in second line:
transform.Find("lineTwo").guiText.text="Woe to them";
yield return new WaitForSeconds(2);
transform.guiText.text="";
transform.Find("lineTwo").guiText.text="";
}
// someone else can call this for any 1-time message:
public IEnumerator flashMessage(string msg, float len) {
transform.guiText.text=msg;
yield return new WaitForSeconds(len);
transform.Find("lineTwo").guiText.text="l";
}
}
Thank you for your time.
1 Answer
1
The best way to create a fade effect, is to manipulate the alpha channel of the color of the thing you want to fade. In case of a guitext you need to make sure there is a material attached. The font material can easily be found as a child of the font in the project panel. For the scripting part: You need a function that changes the alpha value of the color over time. Something like this:
public float fadeTime = 0.5f; //How long do we fade
IEnumerator fadeText (bool fadeIn){
float startTime = Time.time;
Color c = guitext.material.color;
Color c_invis = new Color(c.r, c.g, c.b , 0f);
Color c_vis = new Color(c.r, c.g, c.b , 1f);
if(fadeIn){
while(Time.time - startTime < fadeTime){ //Loop for fadeTime seconds
guitext.material.color = Color.Lerp(c_invis, c_vis, (Time.time - startTime) / fadeTime);
yield return WaitForEndOfFrame();
}
} else {
while(Time.time - startTime < fadeTime){ //Loop for fadeTime seconds
guitext.material.color = Color.Lerp(c_vis, c_invis, (Time.time - startTime) / fadeTime);
yield return WaitForEndOfFrame();
}
}
}
Thanks the script is really useful but how do I combine those two script ? when I try to use the fade in function on my first script I get an error that it cant be used in this context ...
– alilograpiIt is a IEnumerator method, so it called using StartCoroutine(fadeText(true)); Make sure the StartCoroutine is in a piece of code that does not get called each frame: if(fadeIn){ StartCoroutine(fadeText(true)); fadeIn = false; }
– fafaseOk , thanks a lot =)
– alilograpi