- Home /
Question by
Dylanpolis · Apr 07, 2017 at 03:00 PM ·
scripting problem
How do i fix this problem?
Im trying to fade in and out between scenes. Im having a problem were all i see is pink and there is no fade.
using UnityEngine;
using System.Collections;
public class AutoFade : MonoBehaviour
{
private static AutoFade m_Instance = null;
private Material m_Material = null;
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;
m_Material = new Material("Shader \"Plane/No zTest\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Cull Off Fog { Mode Off } BindChannels { Bind \"Color\",color } } } }");
}
private void DrawQuad(Color aColor,float aAlpha)
{
aColor.a = aAlpha;
m_Material.SetPass(0);
GL.Color(aColor);
GL.PushMatrix();
GL.LoadOrtho();
GL.Begin(GL.QUADS);
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);
}
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);
}
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);
}
}
Comment
pink is the color for meshrenderers completely without or with broken shaders
Why aren't you using the new UI System? This seems very manual for what Unity gives you.
Answer by shadowpuppet · Apr 08, 2017 at 11:54 PM
try this code. I got it online somewhere and it seems to work for me. two scripts actually. one is the fade and the other actually changes the scene
using UnityEngine;
using System.Collections;
public class nextLevel : MonoBehaviour {
public string levelToLoad;
void Start () {
}
void OnTriggerEnter(Collider other) {
if(other.tag == "Player")
{
StartCoroutine(ChangLevel());
}
}
IEnumerator ChangLevel(){
float fadeTime = GetComponent<Fading>().BeginFade(1);
yield return new WaitForSeconds (fadeTime);
Application.LoadLevel(levelToLoad);
}
}
using UnityEngine;
using System.Collections;
public class Fading : MonoBehaviour {
//public string levelToLoad;
public Texture fadeOutTexture;
public float fadeSpeed = .8f;
private int drawDepth = -1000;
private float alpha = 1f;
private int fadeDir = -1;
void Start () {
}
void OnGUI (){
alpha += fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01 (alpha);
GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
GUI.depth = drawDepth;
GUI.DrawTexture ( new Rect (0, 0, Screen.width, Screen.height), fadeOutTexture );
}
public float BeginFade (int direction){
fadeDir = direction;
return (fadeSpeed);
}
void OnLevelWasLoaded (){
BeginFade (-1);
}
}
Your answer