- Home /
Unity2D Sprite Fade In and Out
I'm trying to make a script using the new Unity2D sprites to have a level title fade out OR have my tutorial UI sprites fade in. I have gotten far enough to edit the Alpha via the code, but I can only change the alpha to a specific amount instead of having it gradually edit the alpha up and down over time. Can anyone help me out. Heres my code.
using UnityEngine;
using System.Collections;
public class AlphaFade : MonoBehaviour {
public float fadeSpeed = 1f;
public bool fadeIn = true;
public SpriteRenderer text;
// Use this for initialization
// Invisible on Awake
void Awake() {
text.color = new Color(1f,1f,1f,0f);
}
// Update is called once per frame
void Update () {
float Fade = 0f;
if (fadeIn) {
text.color = new Color(1f,1f,1f,Fade=+.1f);
}
}
}
Answer by fonko · May 11, 2018 at 03:34 AM
you should use coroutines for this, something like
void Update ()
{
if(Input.GetKeyUp(KeyCode.T))
{
StartCoroutine(FadeTo(0.0f, 1.0f));
}
if(Input.GetKeyUp(KeyCode.F))
{
StartCoroutine(FadeTo(1.0f, 1.0f));
}
}
IEnumerator FadeTo(float aValue, float aTime)
{
float alpha = transform.renderer.material.color.a;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha,aValue,t));
transform.renderer.material.color = newColor;
yield return null;
}
}
sorry it should be
IEnumerator FadeTo(float aValue, float aTime)
{
float alpha = transform.GetComponent<SpriteRenderer>().material.color.a;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
Color newColor = new Color(1, 1, 1, $$anonymous$$athf.Lerp(alpha, aValue, t));
transform.GetComponent<SpriteRenderer>().material.color = newColor;
yield return null;
}
}
}
Answer by treasgu · Jul 28, 2014 at 09:12 AM
I fix the code adding ref before fadeSpeed
float Fade = Mathf.SmoothDamp(0f,1f,ref fadeSpeed,fadeTime);
But I recommend use SmoothStep for fade effect.
public float minimum = 0.0f;
public float maximum = 1f;
public float duration = 5.0f;
private float startTime;
public SpriteRenderer sprite;
void Start() {
startTime = Time.time;
}
void Update() {
float t = (Time.time - startTime) / duration;
sprite.color = new Color(1f,1f,1f,Mathf.SmoothStep(minimum, maximum, t)); }
Sorry for my english too.
Thx treasgu!!!
I didn't know that ...
You have solved my problem!! :D
so can you tell me that why do recommend using the SmoothStep???
This does not work for me though. I have an if statement that wraps around the fading code like so: if (boolean == true) { float t = (Time.time - startTime) / duration; sprite.color = new Color(1f,1f,1f,$$anonymous$$athf.SmoothStep($$anonymous$$imum, maximum, t)); }
Answer by amilo1003 · Mar 03, 2014 at 10:56 PM
hi, try using Mathf.SmoothDamp. http://docs.unity3d.com/Documentation/ScriptReference/Mathf.SmoothDamp.html
$$anonymous$$y new code now has some errors.
using UnityEngine;
using System.Collections;
public class AlphaFade : $$anonymous$$onoBehaviour {
public float fadeSpeed = 1f;
public float fadeTime = 1f;
public bool fadeIn = true;
public SpriteRenderer text;
// Update is called once per frame
void Update () {
if (fadeIn) {
float Fade = $$anonymous$$athf.SmoothDamp(0f,1f,fadeSpeed,fadeTime);
text.color = new Color(1f,1f,1f,Fade);
}
if (!fadeIn) {
float Fade = $$anonymous$$athf.SmoothDamp(1f,0f,fadeSpeed,fadeTime);
text.color = new Color(1f,1f,1f,Fade);
}
}
}
I need a ref float for my third argument in both SmoothDamp statements, what is it referring to?
you have to declare "fade" float :)
public float fade;
sorry about my english