Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Splash screen

Discussion in 'Scripting' started by Fuzzylr, Sep 4, 2013.

  1. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    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

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SplashScreen : MonoBehaviour
    6. {
    7.     public float timer = 1f;
    8.     public string levelToLoad = "";
    9.    
    10.     void Start ()
    11.     {
    12.         StartCoroutine("DisplayScene");
    13.     }
    14.    
    15.     IEnumerator DisplayScene()
    16.     {
    17.         yield return new WaitForSeconds(timer);
    18.         Application.LoadLevel( levelToLoad );
    19.        
    20.         // Load level
    21.         yield return new WaitForSeconds(timer);
    22.         Application.LoadLevel("DemoClient");
    23.        
    24.     }
    25. }
     
  2. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    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:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class SplashController : MonoBehaviour
    6. {
    7.     //-------------------------------------------------------------------
    8.     #region Fields
    9.  
    10.     [SerializeField]
    11.     private float m_FadeTime = 1.0f;
    12.     [SerializeField]
    13.     private float m_Delay = 1.5f;
    14.     [SerializeField]
    15.     private Texture2D[] m_SplashArray;
    16.     [SerializeField]
    17.     private string m_LevelToLoad = "";
    18.     private int m_StepCount = 0;
    19.     private int m_CurrentStep = 0;
    20.     private Rect m_SplashPosition;
    21.  
    22.     #endregion
    23.  
    24.  
    25.     //-------------------------------------------------------------------
    26.     #region Start class method
    27.  
    28.     private void Start()
    29.     {
    30.         // Set the total number of steps
    31.         this.m_StepCount = this.m_SplashArray.Length;
    32.  
    33.         // Call the FadeIn class method
    34.         this.FadeIn();
    35.     }
    36.  
    37.     #endregion
    38.  
    39.  
    40.     //-------------------------------------------------------------------
    41.     #region OnGUI class method
    42.  
    43.     private void OnGUI()
    44.     {
    45.         // Check if current step excedes the step count
    46.         if (this.m_CurrentStep == this.m_StepCount)
    47.         {
    48.             // Prevent further method processing
    49.             return;
    50.         }
    51.  
    52.         // Check if there is a valid splash screen texture
    53.         if (this.m_SplashArray[this.m_CurrentStep] != null)
    54.         {
    55.             // Configure the splash screen position
    56.             this.m_SplashPosition.x = (Screen.width * 0.5f) - (this.m_SplashArray[this.m_CurrentStep].width * 0.5f);
    57.             this.m_SplashPosition.y = (Screen.height * 0.5f) - (this.m_SplashArray[this.m_CurrentStep].height * 0.5f);
    58.             this.m_SplashPosition.width = this.m_SplashArray[this.m_CurrentStep].width;
    59.             this.m_SplashPosition.height = this.m_SplashArray[this.m_CurrentStep].height;
    60.  
    61.             // Draw splash screen
    62.             GUI.DrawTexture(this.m_SplashPosition, this.m_SplashArray[this.m_CurrentStep]);
    63.         }
    64.         else
    65.         {
    66.             // Prevent further method processing
    67.             return;
    68.         }
    69.     }
    70.  
    71.     #endregion
    72.  
    73.  
    74.     //-------------------------------------------------------------------
    75.     #region FadeIn class method
    76.  
    77.     private void FadeIn()
    78.     {
    79.         // Fade screen in, then call FadeOut class method
    80.         CameraFade.StartAlphaFade(Color.white, true, this.m_FadeTime, 0.5f, this.FadeOut);
    81.     }
    82.  
    83.     #endregion
    84.  
    85.  
    86.     //-------------------------------------------------------------------
    87.     #region FadeOut class method
    88.  
    89.     private void FadeOut()
    90.     {
    91.         // Fade screen out, then call Step class method
    92.         CameraFade.StartAlphaFade(Color.white, false, this.m_FadeTime, this.m_Delay, this.Step);
    93.     }
    94.  
    95.     #endregion
    96.  
    97.  
    98.     //-------------------------------------------------------------------
    99.     #region Step class method
    100.  
    101.     private void Step()
    102.     {
    103.         // Check if current step excedes the step count
    104.         if (this.m_CurrentStep >= this.m_StepCount - 1)
    105.         {
    106.             // Load the desired level
    107.             LoadingInfo.m_LevelName = this.m_LevelToLoad.ToString();
    108.             Application.LoadLevel("Loading");
    109.  
    110.             // Prevent further method processing
    111.             return;
    112.         }
    113.  
    114.         // Increase current step
    115.         this.m_CurrentStep++;
    116.  
    117.         // Call FadeIn class method
    118.         this.FadeIn();
    119.     }
    120.  
    121.     #endregion
    122. }
    123.  
    124.  
    And below is the camera fade class, which was taken from the Unify Wiki.

    CameraFade.cs:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4.  
    5. public class CameraFade : MonoBehaviour
    6. {  
    7.     private static CameraFade mInstance = null;
    8.  
    9.     private static CameraFade instance
    10.     {
    11.         get
    12.         {
    13.             if( mInstance == null )
    14.             {
    15.                 mInstance = GameObject.FindObjectOfType(typeof(CameraFade)) as CameraFade;
    16.  
    17.                 if( mInstance == null )
    18.                 {
    19.                     mInstance = new GameObject("CameraFade").AddComponent<CameraFade>();
    20.                 }
    21.             }
    22.  
    23.             return mInstance;
    24.         }
    25.     }
    26.  
    27.     void Awake()
    28.     {
    29.         if( mInstance == null )
    30.         {
    31.             mInstance = this as CameraFade;
    32.             instance.init();
    33.         }
    34.     }
    35.  
    36.     public GUIStyle m_BackgroundStyle = new GUIStyle();                     // Style for background tiling
    37.     public Texture2D m_FadeTexture;                                         // 1x1 pixel texture used for fading
    38.     public Color m_CurrentScreenOverlayColor = new Color(0,0,0,0);          // default starting color: black and fully transparrent
    39.     public Color m_TargetScreenOverlayColor = new Color(0,0,0,0);           // default target color: black and fully transparrent
    40.     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
    41.     public int m_FadeGUIDepth = -1000;                                      // make sure this texture is drawn on top of everything
    42.  
    43.     public float m_FadeDelay = 0;
    44.     public Action m_OnFadeFinish = null;
    45.  
    46.     // Initialize the texture, background-style and initial color:
    47.     public void init()
    48.     {      
    49.         instance.m_FadeTexture = new Texture2D(1, 1);        
    50.         instance.m_BackgroundStyle.normal.background = instance.m_FadeTexture;
    51.     }
    52.  
    53.     // Draw the texture and perform the fade:
    54.     void OnGUI()
    55.     {  
    56.         // If delay is over...
    57.         if( Time.time > instance.m_FadeDelay )
    58.         {
    59.             // If the current color of the screen is not equal to the desired color: keep fading!
    60.             if (instance.m_CurrentScreenOverlayColor != instance.m_TargetScreenOverlayColor)
    61.             {          
    62.                 // If the difference between the current alpha and the desired alpha is smaller than delta-alpha * deltaTime, then we're pretty much done fading:
    63.                 if (Mathf.Abs(instance.m_CurrentScreenOverlayColor.a - instance.m_TargetScreenOverlayColor.a) < Mathf.Abs(instance.m_DeltaColor.a) * Time.deltaTime)
    64.                 {
    65.                     instance.m_CurrentScreenOverlayColor = instance.m_TargetScreenOverlayColor;
    66.                     SetScreenOverlayColor(instance.m_CurrentScreenOverlayColor);
    67.                     instance.m_DeltaColor = new Color( 0,0,0,0 );
    68.  
    69.                     if( instance.m_OnFadeFinish != null )
    70.                         instance.m_OnFadeFinish();
    71.  
    72.                     Die();
    73.                 }
    74.                 else
    75.                 {
    76.                     // Fade!
    77.                     SetScreenOverlayColor(instance.m_CurrentScreenOverlayColor + instance.m_DeltaColor * Time.deltaTime);
    78.                 }
    79.             }
    80.         }
    81.         // Only draw the texture when the alpha value is greater than 0:
    82.         if (m_CurrentScreenOverlayColor.a > 0)
    83.         {          
    84.             GUI.depth = instance.m_FadeGUIDepth;
    85.             GUI.Label(new Rect(-10, -10, Screen.width + 10, Screen.height + 10), instance.m_FadeTexture, instance.m_BackgroundStyle);
    86.         }
    87.     }
    88.  
    89.  
    90.     /// <summary>
    91.     /// Sets the color of the screen overlay instantly.  Useful to start a fade.
    92.     /// </summary>
    93.     /// <param name='newScreenOverlayColor'>
    94.     /// New screen overlay color.
    95.     /// </param>
    96.     private static void SetScreenOverlayColor(Color newScreenOverlayColor)
    97.     {
    98.         instance.m_CurrentScreenOverlayColor = newScreenOverlayColor;
    99.         instance.m_FadeTexture.SetPixel(0, 0, instance.m_CurrentScreenOverlayColor);
    100.         instance.m_FadeTexture.Apply();
    101.     }
    102.  
    103.     /// <summary>
    104.     /// Starts the fade from color newScreenOverlayColor. If isFadeIn, start fully opaque, else start transparent.
    105.     /// </summary>
    106.     /// <param name='newScreenOverlayColor'>
    107.     /// Target screen overlay Color.
    108.     /// </param>
    109.     /// <param name='fadeDuration'>
    110.     /// Fade duration.
    111.     /// </param>
    112.     public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration )
    113.     {
    114.         if (fadeDuration <= 0.0f)      
    115.         {
    116.             SetScreenOverlayColor(newScreenOverlayColor);
    117.         }
    118.         else                   
    119.         {
    120.             if( isFadeIn )
    121.             {
    122.                 instance.m_TargetScreenOverlayColor = new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 );
    123.                 SetScreenOverlayColor( newScreenOverlayColor );
    124.             } else {
    125.                 instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
    126.                 SetScreenOverlayColor( new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 ) );
    127.             }
    128.  
    129.             instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;   
    130.         }
    131.     }
    132.  
    133.     /// <summary>
    134.     /// Starts the fade from color newScreenOverlayColor. If isFadeIn, start fully opaque, else start transparent, after a delay.
    135.     /// </summary>
    136.     /// <param name='newScreenOverlayColor'>
    137.     /// New screen overlay color.
    138.     /// </param>
    139.     /// <param name='fadeDuration'>
    140.     /// Fade duration.
    141.     /// </param>
    142.     /// <param name='fadeDelay'>
    143.     /// Fade delay.
    144.     /// </param>
    145.     public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration, float fadeDelay )
    146.     {
    147.         if (fadeDuration <= 0.0f)      
    148.         {
    149.             SetScreenOverlayColor(newScreenOverlayColor);
    150.         }
    151.         else                   
    152.         {
    153.             instance.m_FadeDelay = Time.time + fadeDelay;          
    154.  
    155.             if( isFadeIn )
    156.             {
    157.                 instance.m_TargetScreenOverlayColor = new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 );
    158.                 SetScreenOverlayColor( newScreenOverlayColor );
    159.             } else {
    160.                 instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
    161.                 SetScreenOverlayColor( new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 ) );
    162.             }
    163.  
    164.             instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;
    165.         }
    166.     }
    167.  
    168.     /// <summary>
    169.     /// Starts the fade from color newScreenOverlayColor. If isFadeIn, start fully opaque, else start transparent, after a delay, with Action OnFadeFinish.
    170.     /// </summary>
    171.     /// <param name='newScreenOverlayColor'>
    172.     /// New screen overlay color.
    173.     /// </param>
    174.     /// <param name='fadeDuration'>
    175.     /// Fade duration.
    176.     /// </param>
    177.     /// <param name='fadeDelay'>
    178.     /// Fade delay.
    179.     /// </param>
    180.     /// <param name='OnFadeFinish'>
    181.     /// On fade finish, doWork().
    182.     /// </param>
    183.     public static void StartAlphaFade(Color newScreenOverlayColor, bool isFadeIn, float fadeDuration, float fadeDelay, Action OnFadeFinish )
    184.     {
    185.         if (fadeDuration <= 0.0f)      
    186.         {
    187.             SetScreenOverlayColor(newScreenOverlayColor);
    188.         }
    189.         else                   
    190.         {
    191.             instance.m_OnFadeFinish = OnFadeFinish;
    192.             instance.m_FadeDelay = Time.time + fadeDelay;
    193.  
    194.             if( isFadeIn )
    195.             {
    196.                 instance.m_TargetScreenOverlayColor = new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 );
    197.                 SetScreenOverlayColor( newScreenOverlayColor );
    198.             } else {
    199.                 instance.m_TargetScreenOverlayColor = newScreenOverlayColor;
    200.                 SetScreenOverlayColor( new Color( newScreenOverlayColor.r, newScreenOverlayColor.g, newScreenOverlayColor.b, 0 ) );
    201.             }
    202.             instance.m_DeltaColor = (instance.m_TargetScreenOverlayColor - instance.m_CurrentScreenOverlayColor) / fadeDuration;
    203.         }
    204.     }
    205.  
    206.     void Die()
    207.     {
    208.         mInstance = null;
    209.         Destroy(gameObject);
    210.     }
    211.  
    212.     void OnApplicationQuit()
    213.     {
    214.         mInstance = null;
    215.     }
    216. }
    217.  
     
    Last edited: Sep 4, 2013
  3. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    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