Search Unity

AutoFade doesn't work after building project

Discussion in 'Scripting' started by Pio98, Jun 14, 2014.

  1. Pio98

    Pio98

    Joined:
    Jun 14, 2014
    Posts:
    7
    Hello guys, here's a problem with this AutoFade script: it works in the Editor, but it doen't fade when I build the project, it Load the next level only... Here's the AutoFade.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AutoFade : MonoBehaviour
    5. {
    6.     private static AutoFade m_Instance = null;
    7.     private Material m_Material = null;
    8.     private string m_LevelName = "";
    9.     private int m_LevelIndex = 0;
    10.     private bool m_Fading = false;
    11.    
    12.     private static AutoFade Instance
    13.     {
    14.         get
    15.         {
    16.             if (m_Instance == null)
    17.             {
    18.                 m_Instance = (new GameObject("AutoFade")).AddComponent<AutoFade>();
    19.             }
    20.             return m_Instance;
    21.         }
    22.     }
    23.     public static bool Fading
    24.     {
    25.         get { return Instance.m_Fading; }
    26.     }
    27.    
    28.     private void Awake()
    29.     {
    30.         DontDestroyOnLoad(this);
    31.         m_Instance = this;
    32.         m_Material = new Material("Shader \"Plane/No zTest\" { SubShader { Pass { Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Cull Off Fog { Mode Off } BindChannels { Bind \"Color\",color } } } }");
    33.     }
    34.    
    35.     private void DrawQuad(Color aColor,float aAlpha)
    36.     {
    37.         aColor.a = aAlpha;
    38.         m_Material.SetPass(0);
    39.         GL.Color(aColor);
    40.         GL.PushMatrix();
    41.         GL.LoadOrtho();
    42.         GL.Begin(GL.QUADS);
    43.         GL.Vertex3(0, 0, -1);
    44.         GL.Vertex3(0, 1, -1);
    45.         GL.Vertex3(1, 1, -1);
    46.         GL.Vertex3(1, 0, -1);
    47.         GL.End();
    48.         GL.PopMatrix();
    49.     }
    50.    
    51.     private IEnumerator Fade(float aFadeOutTime, float aFadeInTime, Color aColor)
    52.     {
    53.         float t = 0.0f;
    54.         while (t<1.0f)
    55.         {
    56.             yield return new WaitForEndOfFrame();
    57.             t = Mathf.Clamp01(t + Time.deltaTime / aFadeOutTime);
    58.             DrawQuad(aColor,t);
    59.         }
    60.         if (m_LevelName != "")
    61.             Application.LoadLevel(m_LevelName);
    62.         else
    63.             Application.LoadLevel(m_LevelIndex);
    64.         while (t>0.0f)
    65.         {
    66.             yield return new WaitForEndOfFrame();
    67.             t = Mathf.Clamp01(t - Time.deltaTime / aFadeInTime);
    68.             DrawQuad(aColor,t);
    69.         }
    70.         m_Fading = false;
    71.     }
    72.     private void StartFade(float aFadeOutTime, float aFadeInTime, Color aColor)
    73.     {
    74.         m_Fading = true;
    75.         StartCoroutine(Fade(aFadeOutTime, aFadeInTime, aColor));
    76.     }
    77.    
    78.     public static void LoadLevel(string aLevelName,float aFadeOutTime, float aFadeInTime, Color aColor)
    79.     {
    80.         if (Fading) return;
    81.         Instance.m_LevelName = aLevelName;
    82.         Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
    83.     }
    84.     public static void LoadLevel(int aLevelIndex,float aFadeOutTime, float aFadeInTime, Color aColor)
    85.     {
    86.         if (Fading) return;
    87.         Instance.m_LevelName = "";
    88.         Instance.m_LevelIndex = aLevelIndex;
    89.         Instance.StartFade(aFadeOutTime, aFadeInTime, aColor);
    90.     }
    91. }
    I call the fade function on this way:

    AutoFade.LoadLeve(levelToLoad,fadeOutTime,fadeInTime,FadeColor)
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    have you debug fadein time? to see if it increases slowly or has reached it's limit before loading the scene is done?
     
  3. Pio98

    Pio98

    Joined:
    Jun 14, 2014
    Posts:
    7
    Yeah... it works fine in Debug mode... but in Standalone mode it doesn't work :-\... I'm nearly depressed
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
  5. Pio98

    Pio98

    Joined:
    Jun 14, 2014
    Posts:
    7
    Mhm... Why can't I use AutoFade.cs script? I want to use that script, I can customize as I want...

    PS: Excuse me for my bad english, I'm italian
     
  6. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    well you could start elaborate debugging of you build to see what happens. some possibilities:
    - the line never gets run
    - the fade is going to fast
    - execution order of your scripts
    ...

    try to find out when it is ran in your build and see what happens before and after.

    edit: hmm is your shader included in the build, that would be a party stopper if it was missing in the build...
     
    Last edited: Jun 16, 2014
  7. Pio98

    Pio98

    Joined:
    Jun 14, 2014
    Posts:
    7
    Mhm... the fade line must work because it load the next scene...

    Edit:

    I tried to call the Profiler in the build: it doesn't fade, but the coroutine works
     

    Attached Files:

    Last edited: Jun 16, 2014