We could create a regular C# class for this, but we want to be able to assign some values in Unity's Inspector, so we'll create a Unity C# Script instead. There are times when you will want to use the Inspector, and other times, when assigning values in code is better.
To start, we are going to create three variables to store the images used for the three splash screens in BeginState
, WonStates
, and LostStates
.
Scripts
folder, create a new C# Script named GameData
, containing the following code:using UnityEngine; using System.Collections; public class GameData : MonoBehaviour { public Texture2D beginStateSplash; public Texture2D lostStateSplash; public Texture2D wonStateSplash; void Start () { } void Update () { } }
GameData
script to the GameManager to make it a Component. All the data in all the variables will also persist through Scene changes.Three variables are created to store the images for the splash screens. The variables store values of type Texture2D
.
You have to understand that Unity treats images as textures. Have a look at Unity Manual | User Guide | Asset Import and Creation | Importing Assets. Here's a quote:
Textures
Unity supports all image formats. Even when working with layered Photoshop files, they are imported without disturbing the Photoshop format. This allows you to work with a single texture file for a very care-free and streamlined experience.
In the Unity Manual, there's a description on how to make a splash screen. Look in Unity Manual | FAQ | Graphics Questions | How do I make a Splash Screen?
However, there is an easier way by using the GUI system. Since an image is a texture, we'll just display our images using the GUI.DrawTexture()
method. This draws a texture in a rectangle. We will simply make the rectangle fullscreen.
In the previous code, the three variables will each store a Texture2D
, an image. The images are assigned to these variables by dragging the images into the Inspector.
The first thing to do is import the three images you have. If you don't have anything specific already, pictures of friends or family will do nicely.
Once you have them imported, just drag them to the three variable properties in the Inspector. We have these texture images for our splash screens in the GameData
Component, how do we get them to be displayed in the States we desire? By using Dot Syntax, of course. We could have each State that needs to display a splash screen, call GetComponent()
to get the GameData
Component.
That wouldn't be too bad with just five States needing splash screens. However, the GameData
Component is for storing many types of data, not just splash screen images. This means that GameData
will be accessed often for data, and calling GetComponent()
repeatedly will slow down a game.
There is a more efficient way. Just call GetComponent()
one time and store the GameData
object reference that's retrieved into a variable. In fact, this is exactly what Unity suggests you do. Here's a quote from Scripting Reference | Overview: Performance Optimization:
3. Cache component lookups
Another optimization is caching of components. This optimization unfortunately requires a bit of coding effort and is not always worth it. But if your script is really used a lot and you need to get the last bit of performance out of it, this can be a very good optimization.
Whenever you access a component through GetComponent or an accessor variable, Unity has to find the right component from the game object. This time can easily be saved by caching a reference to the component in a private variable.
Since every State already contains a reference to StateManager
, we will have StateManager
call GetComponent()
and store the GameData
reference in a variable named gameDataRef
. Let us have a look a the code in the following screenshot:
An analysis of the preceding code screenshot is given below:
In StateManager
:
Line 10: public GameData gameDataRef;
gameDataRef
object reference stores a reference to a GameData
Component objectGameData
public
since other classes will be accessing this variableLine 30: gameDataRef = GetComponent<GameData>();
Start()
method, we call GetComponent()
to get a reference to the GameData
Component objectgameDataRef
variableLine 9: [HideInInspector]
public
variable from showing in the InspectorgameDataRef
variable to only store a reference to GameData
, there's no reason to allow it to be changed in the Inspector, so HideInInspector
prevents the variable from showingHere's the code in BeginState
that gets the Texture2D
image from GameData
, then displays it on screen. It's just one line of code. I have it on three lines to fit the page:
In BeginState
:
Line 19: Code removed.
Line 23: The GUI.DrawTexture
method.
GUI.DrawTexture()
method draws a Texture2D
in a rectangle that is the size of the game screenmanager.gameDataRef.beginStateSplash
statement is the Dot Syntax used to retrieve the image stored in the GameData
Componentmanager
object reference stores a reference to the StateManager
ComponentgameDataRef
variable in StateManager
stores the reference to the GameData
ComponentbeginStateSplash
variable in GameData
stores the Texture2D
imageGetComponent()
Line 27: The if
statement.
if
statement is checking if a GUI button is clicked or if any keyboard key is pressedSwitchState()
is called to switch to SetupState
Here is the result when you click on Play, a fullscreen splash screen with a GUI button:
For our little game demonstration, the BeginState
code is complete. There's not much to it, a little State Machine code, one line of code to display the splash screen, and a line of code to detect when to switch to SetupState
.
The following code is the complete code of BeginState
:
using UnityEngine; using Assets.Code.Interfaces; namespace Assets.Code.States { public class BeginState : IStateBase { private StateManager manager; public BeginState (StateManager managerRef) { manager = managerRef; if(Application.loadedLevelName != "Scene0") Application.LoadLevel("Scene0"); } public void StateUpdate () { } public void ShowIt () { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), manager.gameDataRef.beginStateSplash, ScaleMode.StretchToFill); if (GUI.Button(new Rect(10, 10, 250, 60), "Press Here or Any Key to Continue") || Input.anyKeyDown) { manager.SwitchState (new SetupState (manager)); } } } }
18.118.189.251