[UNITY] Need help with fading splash screen

Discussion in 'Game Development' started by Serkiee, May 5, 2014 with 6 replies and 1,392 views.

  1. Serkiee

    Serkiee There's a time and place for everything.

    Messages:
    1,482
    Ratings:
    367
    I searched around for a bit but can't really find what I'm looking for. After a while I found a script that fades in, loads the level and fades out. I only want it to fade in and out and not load the level.

    Code:
    using UnityEngine;
    using System.Collections;
    
    public class SplashScreen : MonoBehaviour
    {
        public int guiDepth = 0;
        public string levelToLoad = ""; // this has to correspond to a level (file>build settings)
        public Texture2D splashLogo; // the logo to splash;
        public float fadeSpeed = 0.3f;
        public float waitTime = 0.5f; // seconds to wait before fading out
        public bool waitForInput = false; // if true, this acts as a "press any key to continue"
        public bool startAutomatically = true;
        private float timeFadingInFinished = 0.0f;
       
        public enum SplashType
        {
            LoadNextLevelThenFadeOut,
            FadeOutThenLoadNextLevel
        }
        public SplashType splashType;
       
        private float alpha = 0.0f;
       
        private enum FadeStatus
        {
            Paused,
            FadeIn,
            FadeWaiting,
            FadeOut
        }
        private FadeStatus status = FadeStatus.FadeIn;
       
        private Camera oldCam;
        private GameObject oldCamGO;
       
        private Rect splashLogoPos = new Rect();
        public enum LogoPositioning
        {
            Centered,
            Stretched
        }
        public LogoPositioning logoPositioning;
       
        private bool loadingNextLevel = false;
       
        void Start()
        {
            if (startAutomatically)
            {
                status = FadeStatus.FadeIn;
            }
            else
            {
                status = FadeStatus.Paused;
            }
            oldCam = Camera.main;
            oldCamGO = Camera.main.gameObject;
           
            if (logoPositioning == LogoPositioning.Centered)
            {
                splashLogoPos.x = (Screen.width * 0.5f) - (splashLogo.width * 0.5f);
                splashLogoPos.y = (Screen.height * 0.5f) - (splashLogo.height * 0.5f);
               
                splashLogoPos.width = splashLogo.width;
                splashLogoPos.height = splashLogo.height;
            }
            else
            {
                splashLogoPos.x = 0;
                splashLogoPos.y = 0;
               
                splashLogoPos.width = Screen.width;
                splashLogoPos.height = Screen.height;
            }
           
           
           
            if (splashType == SplashType.LoadNextLevelThenFadeOut)
            {
                DontDestroyOnLoad(this);
                DontDestroyOnLoad(Camera.main);
            }
            if ((Application.levelCount <= 1) || (levelToLoad == ""))
            {
                Debug.LogWarning("Invalid levelToLoad value.");
            }
        }
       
        public void StartSplash()
        {
            status = FadeStatus.FadeIn;
        }
       
        void Update()
        {
            switch(status)
            {
            case FadeStatus.FadeIn:
                alpha += fadeSpeed * Time.deltaTime;
                break;
            case FadeStatus.FadeWaiting:
                if ((!waitForInput && Time.time >= timeFadingInFinished + waitTime) || (waitForInput && Input.anyKey))
                {
                    status = FadeStatus.FadeOut;
                }
                break;
            case FadeStatus.FadeOut:
                alpha += -fadeSpeed * Time.deltaTime;
                break;
            }
        }
       
        void OnGUI()
        {
            GUI.depth = guiDepth;
            if (splashLogo != null)
            {
                GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, Mathf.Clamp01(alpha));
                GUI.DrawTexture(splashLogoPos, splashLogo);
                if (alpha > 1.0f)
                {
                    status = FadeStatus.FadeWaiting;
                    timeFadingInFinished = Time.time;
                    alpha = 1.0f;
                    if (splashType == SplashType.LoadNextLevelThenFadeOut)
                    {
                        oldCam.depth = -1000;
                        loadingNextLevel = true;
                        if ((Application.levelCount >= 1) && (levelToLoad != ""))
                        {
                            Application.LoadLevel(levelToLoad);
                        }
                    }
                }
                if (alpha < 0.0f)
                {
                    if (splashType == SplashType.FadeOutThenLoadNextLevel)
                    {
                        if ((Application.levelCount >= 1) && (levelToLoad != ""))
                        {
                            Application.LoadLevel(levelToLoad);
                        }
                    }
                    else
                    {
                        Destroy(oldCamGO); // somehow this doesn't work
                        Destroy(this);
                    }
                }
            }
        }
       
        void OnLevelWasLoaded(int lvlIdx)
        {
            if (loadingNextLevel)
            {
                Destroy(oldCam.GetComponent<AudioListener>());
                Destroy(oldCam.GetComponent<GUILayer>());
            }
        }
       
        void OnDrawGizmos()
        {
            Gizmos.color = new Color(1f, 0f, 0f, .5f);
            Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
        }
    }
     
  2. PCMasterRace

    PCMasterRace Glorious

    Messages:
    4,483
    Ratings:
    1,371
    Why don't you just remove the part that does application.loadlevel?
     
  3. OP
    Serkiee

    Serkiee There's a time and place for everything.

    Messages:
    1,482
    Ratings:
    367
    When I tried I did something wrong and it didn't work anymore
     
  4. OP
    Serkiee

    Serkiee There's a time and place for everything.

    Messages:
    1,482
    Ratings:
    367
    I didn't really explain it very well. When you run the game it fades in the splash screen then loads the next level/screen then fades out. I want it to fade in, fade out then load the level
     
  5. PCMasterRace

    PCMasterRace Glorious

    Messages:
    4,483
    Ratings:
    1,371
    Okay, I'm confused, the script already does what you're asking. It fades in on an image/splashscreen, then fades out to the new scene. I just used it myself.
     
  6. OP
    Serkiee

    Serkiee There's a time and place for everything.

    Messages:
    1,482
    Ratings:
    367
    It fades in then before it fades out it loads the next level. I want it to load the next level after it finishes fading out
     
  7. PCMasterRace

    PCMasterRace Glorious

    Messages:
    4,483
    Ratings:
    1,371
    Sorry, I was in college.

    Okay, are you sure you're comfortable coding a game? It's right there in front of you already in the script, it took twenty seconds of reading.

    In your inspector, go to your game object that you've added the script to and change splash type from LoadNextLevelThenFadeOut to FadeOutThenLoadNextLevel. Try understand the code yourself before asking for answers. It's always best to have written the code yourself in the end.

    [​IMG]

    Code:
    public enum SplashType
        {
            LoadNextLevelThenFadeOut,
            FadeOutThenLoadNextLevel
        }
    
    void Update()
        {
            switch(status)
            {
            case FadeStatus.FadeIn:
                alpha += fadeSpeed * Time.deltaTime;
                break;
            case FadeStatus.FadeWaiting:
                if ((!waitForInput && Time.time >= timeFadingInFinished + waitTime) || (waitForInput && Input.anyKey))
                {
                    status = FadeStatus.FadeOut;
                }
                break;
            case FadeStatus.FadeOut:
                alpha += -fadeSpeed * Time.deltaTime;
                break;
            }
        }
    
        void OnGUI()
        {
            GUI.depth = guiDepth;
            if (splashLogo != null)
            {
                GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, Mathf.Clamp01(alpha));
                GUI.DrawTexture(splashLogoPos, splashLogo);
                if (alpha > 1.0f)
                {
                    status = FadeStatus.FadeWaiting;
                    timeFadingInFinished = Time.time;
                    alpha = 1.0f;
                    if (splashType == SplashType.LoadNextLevelThenFadeOut)
                    {
                        oldCam.depth = -1000;
                        loadingNextLevel = true;
                        if ((Application.levelCount >= 1) && (levelToLoad != ""))
                        {
                            Application.LoadLevel(levelToLoad);
                        }
                    }
                }
                if (alpha < 0.0f)
                {
                    if (splashType == SplashType.FadeOutThenLoadNextLevel)
                    {
                        if ((Application.levelCount >= 1) && (levelToLoad != ""))
                        {
                            Application.LoadLevel(levelToLoad);
                        }
                    }
                    else
                    {
                        Destroy(oldCamGO); // somehow this doesn't work
                        Destroy(this);
                    }
                }
            }
        }
     
    Last edited: May 6, 2014

Share This Page