Search Unity

Fade in / Fade out material goes to 100% opacity for a fraction of a second

Discussion in 'AR/VR (XR) Discussion' started by xpansivevr, Mar 13, 2017.

  1. xpansivevr

    xpansivevr

    Joined:
    Jun 22, 2016
    Posts:
    27
    Hi I'm using the following script to fade between level loads on GVR. The fades work fine when triggered manually but if they are triggered by a button click calling "FadeOut()" the material goes to 100% opacity for a fraction of a second then fades as expected from 0 - 100.

    I'm stuck so any help is appreciated...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class ScreenFaderSphere : MonoBehaviour {
    7.  
    8.     public bool fadeInOnStart = true;
    9.     public float fadeTime = 2.0f;
    10.     public Material faderMaterial;
    11.    
    12.     public Color fadeColor = new Color(0.01f, 0.01f, 0.01f, 1.0f);
    13.     private bool isFading = false;
    14.     public string nextSceneName = null;
    15.    
    16.     void Awake()
    17.     {
    18.         if (faderMaterial==null) Debug.LogError("fader material not found");
    19.     }
    20.    
    21.     void OnEnable()
    22.     {
    23.         if (fadeInOnStart) StartCoroutine(FadeInRoutine());
    24.     }
    25.    
    26.     void OnLevelWasLoaded(int level)
    27.     {
    28.         if (fadeInOnStart) StartCoroutine(FadeInRoutine());
    29.     }
    30.    
    31.     public void FadeIn()
    32.     {
    33.         StartCoroutine(FadeInRoutine());
    34.     }
    35.    
    36.    
    37.     IEnumerator FadeInRoutine()
    38.     {
    39.         float elapsedTime = 0.0f;
    40.         faderMaterial.color = fadeColor;
    41.         Color color = fadeColor;
    42.         isFading = true;
    43.         while (elapsedTime < fadeTime)
    44.         {
    45.             yield return null;
    46.             elapsedTime += Time.deltaTime;
    47.             color.a = 1.0f - Mathf.Clamp01(elapsedTime / fadeTime);
    48.             faderMaterial.color = color;
    49.         }
    50.         isFading = false;
    51.     }
    52.  
    53.  
    54.    
    55.     public void FadeOut()
    56.     {
    57.         StartCoroutine(FadeOutRoutine());
    58.     }
    59.    
    60.     IEnumerator FadeOutRoutine()
    61.     {
    62.         float elapsedTime = 0.0f;
    63.         faderMaterial.color = fadeColor;
    64.         Color color = fadeColor;
    65.         isFading = true;
    66.         while (elapsedTime < fadeTime)
    67.         {
    68.             yield return null;
    69.             elapsedTime += Time.deltaTime;
    70.             color.a = Mathf.Clamp01(elapsedTime / fadeTime);
    71.             faderMaterial.color = color;
    72.         }
    73.         isFading = false;
    74.         if(!string.IsNullOrEmpty(nextSceneName))
    75.             SceneManager.LoadScene(nextSceneName);
    76.     }  
    77.  
    78. }