Search Unity

delay before fade and load new scene.

Discussion in 'Scripting' started by Arcanegames, Dec 18, 2014.

  1. Arcanegames

    Arcanegames

    Joined:
    Nov 25, 2012
    Posts:
    4
    I have this code from brackeys and I want to add a delay section to it. I am not to good at codding and wanted to know if there was just a basic command line that I could past in to allow me to control how many seconds before the fade and load scene took place.

    using UnityEngine;
    using System.Collections;

    public class Fading : MonoBehaviour {

    public Texture2D fadeOutTexture; // the texture that will overlay the screen. This can be a black image or a loading graphic
    public float fadeSpeed = 0.8f; // the fading speed

    private int drawDepth = -1000; // the texture's order in the draw hierarchy: a low number means it renders on top
    private float alpha = 1.0f; // the texture's alpha value between 0 and 1
    private int fadeDir = -1; // the direction to fade: in = -1 or out = 1

    void OnGUI()
    {
    // fade out/in the alpha value using a direction, a speed and Time.deltaTime to convert the operation to seconds
    alpha += fadeDir * fadeSpeed * Time.deltaTime;
    // force (clamp) the number to be between 0 and 1 because GUI.color uses Alpha values between 0 and 1
    alpha = Mathf.Clamp01(alpha);

    // set color of our GUI (in this case our texture). All color values remain the same & the Alpha is set to the alpha variable
    GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
    GUI.depth = drawDepth; // make the black texture render on top (drawn last)
    GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeOutTexture); // draw the texture to fit the entire screen area
    }

    // sets fadeDir to the direction parameter making the scene fade in if -1 and out if 1
    public float BeginFade (int direction)
    {
    fadeDir = direction;
    return (fadeSpeed);
    }

    // OnLevelWasLoaded is called when a level is loaded. It takes loaded level index (int) as a parameter so you can limit the fade in to certain scenes.
    void OnLevelWasLoaded()
    {
    // alpha = 1; // use this if the alpha is not set to 1 by default
    BeginFade(-1); // call the fade in function
    }
    }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    There's Invoke.
     
  3. StevenJacobs90

    StevenJacobs90

    Joined:
    Nov 15, 2014
    Posts:
    12
    I believe you can do this with Coroutines and IEnumerators, they contain a simple function like WaitForSeconds("int number of how many seconds you want to wait").
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137