Last active
April 11, 2017 03:59
-
-
Save pacoelayudante/c05ce89d88e3b4a100fd056479d2044a to your computer and use it in GitHub Desktop.
Unity Fade Behaviours
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[ExecuteInEditMode] | |
public class FadeColor : MonoBehaviour { | |
public bool autoIniciar; | |
[Range(0f,1f)] | |
public float tiempo; | |
public float duracion = 1; | |
public AnimationCurve curva = AnimationCurve.EaseInOut(0, 0, 1, 1); | |
public Color colorOriginal,colorFinal; | |
Color actual; | |
bool transicionando,subiendo; | |
TextMesh tm; | |
SpriteRenderer sr; | |
void Reset() | |
{ | |
TextMesh temp = GetComponent<TextMesh>(); | |
if (temp) colorOriginal = colorFinal = temp.color; | |
else | |
{ | |
SpriteRenderer tempsr = GetComponent<SpriteRenderer>(); | |
if (tempsr) colorOriginal = colorFinal = tempsr.color; | |
} | |
} | |
void Awake() | |
{ | |
tm = GetComponent<TextMesh>(); | |
sr = GetComponent<SpriteRenderer>(); | |
/*if (tm) colorOriginal = tm.color; | |
if (sr) colorOriginal = sr.color;*/ | |
} | |
void Start() | |
{ | |
#if UNITY_EDITOR | |
if (UnityEditor.EditorApplication.isPlaying) | |
#endif | |
{ | |
Actualizar(); | |
if (autoIniciar) Fade(tiempo < .5f); | |
} | |
} | |
#if UNITY_EDITOR | |
void Update() | |
{ | |
if (!UnityEditor.EditorApplication.isPlaying) | |
{ | |
if (!tm && !sr) | |
{ | |
tm = GetComponent<TextMesh>(); | |
sr = GetComponent<SpriteRenderer>(); | |
} | |
Actualizar(); | |
} | |
} | |
#endif | |
void Actualizar() { | |
actual = Color.Lerp(colorOriginal,colorFinal, curva.Evaluate( tiempo ) ); | |
if (tm) tm.color = actual; | |
if (sr) sr.color = actual; | |
} | |
[ContextMenu("Fade In")] | |
void FadeIn() | |
{ | |
Fade(true); | |
} | |
[ContextMenu("Fade Out")] | |
void FadeOut() | |
{ | |
Fade(false); | |
} | |
public void FadeDesde(float t, bool estado) | |
{ | |
CambiarTiempo(t); | |
Fade(estado); | |
} | |
public void CambiarTiempo(float t) | |
{ | |
tiempo = t; | |
} | |
public void Fade(bool estado){ | |
StartCoroutine(CoFade(estado)); | |
} | |
IEnumerator CoFade(bool estado) | |
{ | |
subiendo = estado; | |
if (transicionando) yield break; | |
else transicionando = true; | |
while( (tiempo > 0 && !subiendo) || (tiempo < 1 && subiendo) ) | |
{ | |
if (duracion <= 0) tiempo = (subiendo ? 1 : 0); | |
else tiempo += (Time.unscaledDeltaTime / duracion) * (subiendo ? 1 : -1); | |
if (tiempo < 0) tiempo = 0; | |
else if (tiempo > 1) tiempo = 1; | |
Actualizar(); | |
yield return null; | |
} | |
transicionando = false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[ExecuteInEditMode] | |
public class FadeEscala : MonoBehaviour | |
{ | |
public bool autoIniciar; | |
[Range(0f, 1f)] | |
public float tiempo; | |
public float duracion = 1; | |
public AnimationCurve curva = AnimationCurve.EaseInOut(0, 0, 1, 1); | |
public Vector3 escalaOriginal,escalaFinal; | |
bool transicionando, subiendo; | |
void Reset() | |
{ | |
escalaOriginal = escalaFinal = transform.localScale; | |
} | |
/*void Awake() | |
{ | |
escalaOriginal = transform.localScale; | |
}*/ | |
void Start() | |
{ | |
#if UNITY_EDITOR | |
if (UnityEditor.EditorApplication.isPlaying) | |
#endif | |
{ | |
Actualizar(); | |
if (autoIniciar) Fade(tiempo < .5f); | |
} | |
} | |
#if UNITY_EDITOR | |
void Update() | |
{ | |
if (!UnityEditor.EditorApplication.isPlaying) | |
{ | |
Actualizar(); | |
} | |
} | |
#endif | |
void Actualizar() | |
{ | |
transform.localScale = Vector3.Lerp(escalaOriginal, escalaFinal, curva.Evaluate(tiempo)); | |
} | |
[ContextMenu("Fade In")] | |
void FadeIn() | |
{ | |
Fade(true); | |
} | |
[ContextMenu("Fade Out")] | |
void FadeOut() | |
{ | |
Fade(false); | |
} | |
public void FadeDesde(float t, bool estado) | |
{ | |
CambiarTiempo(t); | |
Fade(estado); | |
} | |
public void CambiarTiempo(float t) | |
{ | |
tiempo = t; | |
} | |
public void Fade(bool estado) | |
{ | |
StartCoroutine(CoFade(estado)); | |
} | |
IEnumerator CoFade(bool estado) | |
{ | |
subiendo = estado; | |
if (transicionando) yield break; | |
else transicionando = true; | |
while ((tiempo > 0 && !subiendo) || (tiempo < 1 && subiendo)) | |
{ | |
if (duracion <= 0) tiempo = (subiendo ? 1 : 0); | |
else tiempo += (Time.unscaledDeltaTime / duracion) * (subiendo ? 1 : -1); | |
if (tiempo < 0) tiempo = 0; | |
else if (tiempo > 1) tiempo = 1; | |
Actualizar(); | |
yield return null; | |
} | |
transicionando = false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[ExecuteInEditMode] | |
public class FadePosicion : MonoBehaviour { | |
public bool autoIniciar; | |
[Range(0f,1f)] | |
public float tiempo; | |
public float duracion = 1; | |
public AnimationCurve curva = AnimationCurve.EaseInOut(0, 0, 1, 1); | |
public Vector3 posicionOriginal, posicionFinal; | |
bool transicionando,subiendo; | |
void Reset() | |
{ | |
posicionOriginal = posicionFinal = transform.localPosition; | |
} | |
/*void Awake() | |
{ | |
escalaOriginal = transform.localPosition; | |
}*/ | |
void Start() | |
{ | |
#if UNITY_EDITOR | |
if (UnityEditor.EditorApplication.isPlaying) | |
#endif | |
{ | |
Actualizar(); | |
if (autoIniciar) Fade(tiempo < .5f); | |
} | |
} | |
#if UNITY_EDITOR | |
void Update() | |
{ | |
if (!UnityEditor.EditorApplication.isPlaying) | |
{ | |
Actualizar(); | |
} | |
} | |
#endif | |
void Actualizar() | |
{ | |
transform.localPosition = Vector3.Lerp(posicionOriginal, posicionFinal, curva.Evaluate(tiempo)); | |
} | |
[ContextMenu("Fade In")] | |
void FadeIn() | |
{ | |
Fade(true); | |
} | |
[ContextMenu("Fade Out")] | |
void FadeOut() | |
{ | |
Fade(false); | |
} | |
public void FadeDesde(float t, bool estado) | |
{ | |
CambiarTiempo(t); | |
Fade(estado); | |
} | |
public void CambiarTiempo(float t) | |
{ | |
tiempo = t; | |
} | |
public void Fade(bool estado) | |
{ | |
StartCoroutine(CoFade(estado)); | |
} | |
IEnumerator CoFade(bool estado) | |
{ | |
subiendo = estado; | |
if (transicionando) yield break; | |
else transicionando = true; | |
while ((tiempo > 0 && !subiendo) || (tiempo < 1 && subiendo)) | |
{ | |
if (duracion <= 0) tiempo = (subiendo ? 1 : 0); | |
else tiempo += (Time.unscaledDeltaTime / duracion) * (subiendo ? 1 : -1); | |
if (tiempo < 0) tiempo = 0; | |
else if (tiempo > 1) tiempo = 1; | |
Actualizar(); | |
yield return null; | |
} | |
transicionando = false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[ExecuteInEditMode] | |
public class FadeRotacion : MonoBehaviour { | |
public bool autoIniciar; | |
[Range(0f,1f)] | |
public float tiempo; | |
public float duracion = 1; | |
public AnimationCurve curva = AnimationCurve.EaseInOut(0, 0, 1, 1); | |
public Vector3 rotacionOriginal, rotacionFinal; | |
bool transicionando,subiendo; | |
void Reset() | |
{ | |
rotacionOriginal = rotacionFinal = transform.localEulerAngles; | |
} | |
/*void Awake() | |
{ | |
rotacionOriginal = transform.localEulerAngles; | |
}*/ | |
void Start() | |
{ | |
#if UNITY_EDITOR | |
if (UnityEditor.EditorApplication.isPlaying) | |
#endif | |
{ | |
Actualizar(); | |
if (autoIniciar) Fade(tiempo < .5f); | |
} | |
} | |
#if UNITY_EDITOR | |
void Update() | |
{ | |
if (!UnityEditor.EditorApplication.isPlaying) | |
{ | |
Actualizar(); | |
} | |
} | |
#endif | |
void Actualizar() | |
{ | |
transform.localRotation = Quaternion.Slerp(Quaternion.Euler( rotacionOriginal), Quaternion.Euler(rotacionFinal), curva.Evaluate(tiempo)); | |
} | |
[ContextMenu("Fade In")] | |
void FadeIn() | |
{ | |
Fade(true); | |
} | |
[ContextMenu("Fade Out")] | |
void FadeOut() | |
{ | |
Fade(false); | |
} | |
public void FadeDesde(float t, bool estado) | |
{ | |
CambiarTiempo(t); | |
Fade(estado); | |
} | |
public void CambiarTiempo(float t) | |
{ | |
tiempo = t; | |
} | |
public void Fade(bool estado) | |
{ | |
StartCoroutine(CoFade(estado)); | |
} | |
IEnumerator CoFade(bool estado) | |
{ | |
subiendo = estado; | |
if (transicionando) yield break; | |
else transicionando = true; | |
while ((tiempo > 0 && !subiendo) || (tiempo < 1 && subiendo)) | |
{ | |
if (duracion <= 0) tiempo = (subiendo ? 1 : 0); | |
else tiempo += (Time.unscaledDeltaTime / duracion) * (subiendo ? 1 : -1); | |
if (tiempo < 0) tiempo = 0; | |
else if (tiempo > 1) tiempo = 1; | |
Actualizar(); | |
yield return null; | |
} | |
transicionando = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment