- Home /
Level Fade Help!
Hello world! I've been trying to figure this out but no luck as of yet :( So basically, I want the level to load and fade-in as the character walks into the box collider. This is what I've managed to create so far and do not know how to do the fade transition. Any help would be greatly appreciated! Thank's in advance. (Oh and I am new to scripting)
using UnityEngine;
using System.Collections;
public class LevelTrigger : MonoBehaviour {
void OnTriggerEnter ( Collider cubeTrigger ){
Debug.Log ("OnTriggerEnter : cubeTrigger.tag = " + cubeTrigger.tag); // shows the tag of the trigger
// if tag is player
if (cubeTrigger.tag == "Player")
{
var firstlevel = GameObject.Find("Level1");
Destroy(firstlevel);
Application.LoadLevelAdditiveAsync ("Level2");
}
}
}
Answer by Bunny83 · Mar 03, 2013 at 01:11 AM
So you want something like this:
http://answers.unity3d.com/questions/119918/transitions-between-changing-scenes.html#answer-119951
or do you talk about something else? If you want to fade "the whole scene" the easiest way would be to render both to a texture and fade the fullscreen textures. If you don't have Unity pro this is "almost" impossible. You could try to replace all shaders on all renderers and fade the materials alpha values. That's probably quite slow and might not give the right result.
A bit more detail on what you actually mean by "fading" and if you have Unity pro available or not would be nice-
Oh wow, this is exactly what I want! I happened to pass by this thread a few hours ago and gave it a go but it did not work. I mean when I click play, nothing happens (grey screen). No error though. Oh and I am using Unity Pro 3.5.6f4 by the way. Here's the script...
using UnityEngine;
using System.Collections;
public class LevelTrigger : $$anonymous$$onoBehaviour {
void OnTriggerEnter ( Collider cubeTrigger ){
Debug.Log ("OnTriggerEnter : cubeTrigger.tag = " + cubeTrigger.tag); // shows the tag of the trigger
// if tag is player
if (cubeTrigger.tag == "Player")
{
var firstlevel = GameObject.Find("Level1");
Destroy(firstlevel);
AutoFade.LoadLevelAdditiveAsync ("Level2" ,3,1,Color.black);
}
}
}
// AutoFade.cs
using UnityEngine;
using System.Collections;
public class AutoFade : $$anonymous$$onoBehaviour
{
private static AutoFade m_Instance = null;
private $$anonymous$$aterial m_$$anonymous$$aterial = 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_$$anonymous$$aterial = new $$anonymous$$aterial("Shader \"Plane/No zTest\" { SubShader { Pass { Blend SrcAlpha One$$anonymous$$inusSrcAlpha ZWrite Off Cull Off Fog { $$anonymous$$ode Off } BindChannels { Bind \"Color\",color } } } }");
}
private void DrawQuad(Color aColor,float aAlpha)
{
aColor.a = aAlpha;
m_$$anonymous$$aterial.SetPass(0);
GL.Color(aColor);
GL.Push$$anonymous$$atrix();
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.Pop$$anonymous$$atrix();
}
private IEnumerator Fade(float aFadeOutTime, float aFadeInTime, Color aColor)
{
float t = 0.0f;
while (t<1.0f)
{
yield return new WaitForEndOfFrame();
t = $$anonymous$$athf.Clamp01(t + Time.deltaTime / aFadeOutTime);
DrawQuad(aColor,t);
}
if (m_LevelName != "")
Application.LoadLevelAdditiveAsync(m_LevelName);
else
Application.LoadLevelAdditiveAsync(m_LevelIndex);
while (t>0.0f)
{
yield return new WaitForEndOfFrame();
t = $$anonymous$$athf.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 LoadLevelAdditiveAsync(string aLevelName,float aFadeOutTime, float aFadeInTime, Color aColor)
{
if (Fading) return;
Instance.m_LevelName = aLevelName;
Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
}
public static void LoadLevelAdditiveAsync(int aLevelIndex,float aFadeOutTime, float aFadeInTime, Color aColor)
{
if (Fading) return;
Instance.m_LevelName = "";
Instance.m_LevelIndex = aLevelIndex;
Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
}
}
Answer by Propagant · Sep 15, 2015 at 11:03 AM
Hey! I made easiest method of AutoFade in unity by Unity Game engine, UI elemets etc... You can free download it here... Fully explained and showed. Enjoy and thanks, if any opinions, just write. AutoFadeAlternative by matt