using UnityEngine;
using System.Collections;
public class AutoFade : MonoBehaviour
{
private static AutoFade m_Instance = null;
public Material mat;
private Mesh mesh;
private string m_LevelName = "";
private int m_LevelIndex = 0;
private bool m_Fading = false;
private static AutoFade Instance
{
get
{
if (m_Instance == null)
{
m_Instance = (new GameObject("AutoFade")).AddComponent<AutoFade>();
}
return m_Instance;
}
}
public static bool Fading
{
get { return Instance.m_Fading; }
}
private void Awake()
{
DontDestroyOnLoad(this);
m_Instance = this;
mat = new Material(Shader.Find("UI/Default"));
mesh = new Mesh();
}
private void DrawQuad(Color aColor,float aAlpha)
{
if (!mat) {
Debug.LogError("Please Assign a material on the inspector");
return;
}
GL.PushMatrix();
mat.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.QUADS);
GL.Color(new Vector4(aColor.r, aColor.g, aColor.b, aAlpha));
GL.Vertex3(0, 0, -1);
GL.Vertex3(0, 1, -1);
GL.Vertex3(1, 1, -1);
GL.Vertex3(1, 0, -1);
GL.End();
GL.PopMatrix();
}
private IEnumerator Fade(float aFadeOutTime, float aFadeInTime, Color aColor)
{
float t = 0.0f;
while (t<1.0f)
{
yield return new WaitForEndOfFrame();
t = Mathf.Clamp01(t + Time.deltaTime / aFadeOutTime);
DrawQuad(aColor,t);
Debug.Log("fade : " + t);
}
if (m_LevelName != "")
Application.LoadLevel(m_LevelName);
else
Application.LoadLevel(m_LevelIndex);
while (t>0.0f)
{
yield return new WaitForEndOfFrame();
t = Mathf.Clamp01(t - Time.deltaTime / aFadeInTime);
DrawQuad(aColor,t);
Debug.Log("fade : "+t );
}
// OnPostRender();
m_Fading = false;
}
private void StartFade(float aFadeOutTime, float aFadeInTime, Color aColor)
{
m_Fading = true;
StartCoroutine(Fade(aFadeOutTime, aFadeInTime, aColor));
}
public static void LoadLevel(string aLevelName,float aFadeOutTime, float aFadeInTime, Color aColor)
{
if (Fading) return;
Instance.m_LevelName = aLevelName;
Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
}
public static void LoadLevel(int aLevelIndex,float aFadeOutTime, float aFadeInTime, Color aColor)
{
if (Fading) return;
Instance.m_LevelName = "";
Instance.m_LevelIndex = aLevelIndex;
Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
}
}