Splash screen

Greetings,

I created a simple script that runs through some images and then is suppose to load the actual scense with the player on it. However, it does everything but load the final scene. Yes, It has been added to the build setting page. I am not getting any errors. The purpose of this script is to load the first image. Wait 1 second and load the next image. Finally it loads you into the game.

Sean

using UnityEngine;
using System.Collections;

public class SplashScreen : MonoBehaviour 
{
	public float timer = 1f;
	public string levelToLoad = "";
	
	void Start () 
	{
		StartCoroutine("DisplayScene");
	}
	
	IEnumerator DisplayScene()
	{
		yield return new WaitForSeconds(timer);
		Application.LoadLevel( levelToLoad );
		
		// Load level
		yield return new WaitForSeconds(timer);
		Application.LoadLevel("DemoClient");
		
	}
}

Instead of loading multiple levels for each image as you’re doing, you may want to attempt to do everything from one scene instead. Here is a solution that I threw together quite some time back that you can use, I tried to clean up and comment the code a little so you can better understand what is going on.

SplashController.cs:

using UnityEngine;
using System.Collections;

public class SplashController : MonoBehaviour
{
    //-------------------------------------------------------------------
    #region Fields

    [SerializeField]
    private float m_FadeTime = 1.0f;
    [SerializeField]
    private float m_Delay = 1.5f;
    [SerializeField]
    private Texture2D[] m_SplashArray;
    [SerializeField]
    private string m_LevelToLoad = "";
    private int m_StepCount = 0;
    private int m_CurrentStep = 0;
    private Rect m_SplashPosition;

    #endregion


    //-------------------------------------------------------------------
    #region Start class method

    private void Start()
    {
        // Set the total number of steps
        this.m_StepCount = this.m_SplashArray.Length;

        // Call the FadeIn class method
        this.FadeIn();
    }

    #endregion


    //-------------------------------------------------------------------
    #region OnGUI class method

    private void OnGUI()
    {
        // Check if current step excedes the step count
        if (this.m_CurrentStep == this.m_StepCount)
        {
            // Prevent further method processing
            return;
        }

        // Check if there is a valid splash screen texture
        if (this.m_SplashArray[this.m_CurrentStep] != null)
        {
            // Configure the splash screen position
            this.m_SplashPosition.x = (Screen.width * 0.5f) - (this.m_SplashArray[this.m_CurrentStep].width * 0.5f);
            this.m_SplashPosition.y = (Screen.height * 0.5f) - (this.m_SplashArray[this.m_CurrentStep].height * 0.5f);
            this.m_SplashPosition.width = this.m_SplashArray[this.m_CurrentStep].width;
            this.m_SplashPosition.height = this.m_SplashArray[this.m_CurrentStep].height;

            // Draw splash screen
            GUI.DrawTexture(this.m_SplashPosition, this.m_SplashArray[this.m_CurrentStep]);
        }
        else
        {
            // Prevent further method processing
            return;
        }
    }

    #endregion


    //-------------------------------------------------------------------
    #region FadeIn class method

    private void FadeIn()
    {
        // Fade screen in, then call FadeOut class method
        CameraFade.StartAlphaFade(Color.white, true, this.m_FadeTime, 0.5f, this.FadeOut);
    }

    #endregion


    //-------------------------------------------------------------------
    #region FadeOut class method

    private void FadeOut()
    {
        // Fade screen out, then call Step class method
        CameraFade.StartAlphaFade(Color.white, false, this.m_FadeTime, this.m_Delay, this.Step);
    }

    #endregion


    //-------------------------------------------------------------------
    #region Step class method

    private void Step()
    {
        // Check if current step excedes the step count
        if (this.m_CurrentStep >= this.m_StepCount - 1)
        {
            // Load the desired level
            LoadingInfo.m_LevelName = this.m_LevelToLoad.ToString();
            Application.LoadLevel("Loading");

            // Prevent further method processing
            return;
        }

        // Increase current step
        this.m_CurrentStep++;

        // Call FadeIn class method
        this.FadeIn();
    }

    #endregion
}

And below is the camera fade class, which was taken from the Unify Wiki.

CameraFade.cs:

using UnityEngine;
using System;
 
public class CameraFade : MonoBehaviour
{   
	private static CameraFade mInstance = null;
 
	private static CameraFade instance
	{
		get
		{
			if( mInstance == null )
			{
				mInstance = GameObject.FindObjectOfType(typeof(CameraFade)) as CameraFade;
 
				if( mInstance == null )
				{
					mInstance = new GameObject("CameraFade").AddComponent<CameraFade>();
				}
			}
 
			return mInstance;
		}
	}
 
	void Awake()
	{
		if( mInstance == null )
		{
			mInstance = this as CameraFade;
			instance.init();
		}
	}
 
	public GUIStyle m_BackgroundStyle = new GUIStyle();						// Style for background tiling
	public Texture2D m_FadeTexture;											// 1x1 pixel texture used for fading
	public Color m_CurrentScreenOverlayColor = new Color(0,0,0,0);			// default starting color: black and fully transparrent
	public Color m_TargetScreenOverlayColor = new Color(0,0,0,0);			// default target color: black and fully transparrent
	public Color m_DeltaColor = new Color(0,0,0,0);							// the delta-color is basically the "speed / second" at which the current color should change
	public int m_FadeGUIDepth = -1000;										// make sure this texture is drawn on top of everything
 
	public float m_FadeDelay = 0;
	public Action m_OnFadeFinish = null;
 
	// Initialize the texture, background-style and initial color:
	public void init()
	{		
		instance.m_FadeTexture = new Texture2D(1, 1);        
        instance.m_BackgroundStyle.normal.background = instance.m_FadeTexture;
	}
 
	// Draw the texture and perform the fade:
	void OnGUI()
    {   
		// If delay is over...
		if( Time.time > instance.m_FadeDelay )
		{
			// If the current color of the screen is not equal to the desired color: keep fading!
			if (instance.m_CurrentScreenOverlayColor != instance.m_TargetScreenOverlayColor)
			{			
				// If the difference between the current alpha and the desired alpha is smaller than delta-alpha * deltaTime, then we're pretty much done fading:
				if (Mathf.Abs(instance.m_CurrentScreenOverlayColor.a - instance.m_TargetScreenOverlayColor.a) < Mathf.Abs(instance.m_DeltaColor.a) * Time.deltaTime)
				{
					instance.m_CurrentScreenOverlayColor = instance.m_TargetScreenOverlayColor;
					SetScreenOverlayColor(instance.m_CurrentScreenOverlayColor);
					instance.m_DeltaColor = new Color( 0,0,0,0 );
 
					if( instance.m_OnFadeFinish != null )
						instance.m_OnFadeFinish();
 
					Die();
				}
				else
				{
					// Fade!
					SetScreenOverlayColor(instance.m_CurrentScreenOverlayColor + instance.m_DeltaColor * Time.deltaTime);
				}
			}
		}
		// Only draw the texture when the alpha value is greater than 0:
		if (m_CurrentScreenOverlayColor.a > 0)
		{			
    		GUI.depth = instance.m_FadeGUIDepth;
    		GUI.Label(new Rect(-10, -10, Screen.width + 10, Screen.height + 10), instance.m_FadeTexture, instance.m_BackgroundStyle);
		}
    }
 
 
	/// <summary>
	/// Sets the color of the screen overlay instantly.  Useful to start a fade.
	/// </summary>
	/// <param name='newScreenOverlayColor'>
	/// New screen overlay color.
	/// </param>
	private static void SetScreenOverlayColor(Color newScreenOverlayColor)
	{
		instance.m_CurrentScreenOverlayColor = newScreenOverlayColor;
		instance.m_FadeTexture.SetPixel(0, 0, instance.m_CurrentScreenOverlayColor);
		instance.m_FadeTexture.Apply();
	}
 
 	/// <summary>
 	/// Starts the fade from color newScreenOverlayColor. If isFadeIn, start fully opaque, else start transparent.
 	/// </summary>
 	/// <param name='newScreenOverlayColor'>
 	/// Target screen overlay Color.
 	/// </param>
 	/// <param name='fadeDuration'>
 	/// Fade duration.
 	/// </param>
	public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration )
	{
		if (fadeDuration <= 0.0f)		
		{
			SetScreenOverlayColor(newScreenOverlayColor);
		}
		else					
		{
			if( isFadeIn )
			{
				instance.m_TargetScreenOverlayColor = new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 );
				SetScreenOverlayColor( newScreenOverlayColor );
			} else {
				instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
				SetScreenOverlayColor( new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 ) );
			}
 
			instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;	
		}
	}
 
	/// <summary>
	/// Starts the fade from color newScreenOverlayColor. If isFadeIn, start fully opaque, else start transparent, after a delay.
	/// </summary>
	/// <param name='newScreenOverlayColor'>
	/// New screen overlay color.
	/// </param>
	/// <param name='fadeDuration'>
	/// Fade duration.
	/// </param>
	/// <param name='fadeDelay'>
	/// Fade delay.
	/// </param>
	public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration, float fadeDelay )
	{
		if (fadeDuration <= 0.0f)		
		{
			SetScreenOverlayColor(newScreenOverlayColor);
		}
		else					
		{
			instance.m_FadeDelay = Time.time + fadeDelay;			
 
			if( isFadeIn )
			{
				instance.m_TargetScreenOverlayColor = new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 );
				SetScreenOverlayColor( newScreenOverlayColor );
			} else {
				instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
				SetScreenOverlayColor( new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 ) );
			}
 
			instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;
		}
	}
 
	/// <summary>
	/// Starts the fade from color newScreenOverlayColor. If isFadeIn, start fully opaque, else start transparent, after a delay, with Action OnFadeFinish.
	/// </summary>
	/// <param name='newScreenOverlayColor'>
	/// New screen overlay color.
	/// </param>
	/// <param name='fadeDuration'>
	/// Fade duration.
	/// </param>
	/// <param name='fadeDelay'>
	/// Fade delay.
	/// </param>
	/// <param name='OnFadeFinish'>
	/// On fade finish, doWork().
	/// </param>
	public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration, float fadeDelay, Action OnFadeFinish )
	{
		if (fadeDuration <= 0.0f)		
		{
			SetScreenOverlayColor(newScreenOverlayColor);
		}
		else					
		{
			instance.m_OnFadeFinish = OnFadeFinish;
			instance.m_FadeDelay = Time.time + fadeDelay;
 
			if( isFadeIn )
			{
				instance.m_TargetScreenOverlayColor = new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 );
				SetScreenOverlayColor( newScreenOverlayColor );
			} else {
				instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
				SetScreenOverlayColor( new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 ) );
			}
			instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;
		}
	}
 
	void Die()
	{
		mInstance = null;
		Destroy(gameObject);
	}
 
	void OnApplicationQuit()
	{
		mInstance = null;
	}
}

wow, ok when I get home from work tonight I will take a closer look at this. What I was hoping to do was have a very brief splash screen that ultimately dumps you to a login prompt. Then you would hit the login server and authenticate present you with a list of worlds.

Sean