Unity3D: Opening Splash Screen pre uGUI

99% of the games we all play have a splash screen.  What is it?  The screen (or series of screens) at the very beginning of a game, which displays the developer, publisher, etc.  It is usually the first thing the player sees, and leads into the main menu.  I've written a few at this point, and wanted to share my quick-and-easy method so others can add it to their game as well. All in all, the implementation should take no more than 10 minutes.


Initialization

Before we do anything, let's get the script all set up.  The image below shows all the code, with comments to describe what each variable is used for.

Note that "GameScreen", which the SplashScreen is inheriting from, is specific to my code.  If you're not doing anything crazy with the GUI, you would just inherit MonoBehaviour like normal.  All of the public variables (BlackScreen, Screens, and FadeTime) are available in the editor, which makes swapping out files and/or adding new screens quick and simple.


Update Logic

The next thing to do is to fade each screen in and then out in turn.  Rather than doing this in OnGUI() we are going to do it in Update().  Ideally, complex logic should never be written in OnGUI(); it should only be used for displaying things.

The idea behind the Update cycle is simple.  Let's say FadeTime is 1 second.  We are going to count to 1 second, fading the first screen in over that time.  Then we are going to let is stay for 1 second, then fade it out over 1 more second.  Once this process finishes, we start it over with the second splash screen, and so on until we've gone through all of them.

Note that in the following code, "GUIState.ShowScreen()" is a function I've created elsewhere.  In place of this, you would switch from the splash screen to whatever screen you want to have come next, such as your main menu screen.  One way to do this is to deactivate the current script and activate the main menu script, if they're each individual scripts.


Update GUI

The variables are initialized, and the logic is updating, now it's time to display everything!  This function is very simple: All we do is draw the black screen, then on top of it draw the current splash screen with its alpha set accordingly.  Thank to the Update() function, alpha will increase and decrease over time.


Conclusion

And that's it!  You'll now have a working splash screen.  All you need to do is attach is to a game object and make sure it's enabled at the start of the game.  Make sure you set the BlackScreen, Screens, and FadeTime parameters through the editor.