Time for action – creating a GameData script

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.

Note

By now, you should know how to create a C# Script in Unity or in MonoDevelop. From now on, you should simply create the script using whichever is most convenient at the time.

  1. In the 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 ()
      {
      }
    }
  2. Attach this GameData script to the GameManager to make it a Component. All the data in all the variables will also persist through Scene changes.

What just happened?

Three variables are created to store the images for the splash screens. The variables store values of type Texture2D.

Understanding images as textures in Unity

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.

Using splash screens between game levels

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.

Note

This is an example of why assigning variable values in the Inspector is preferred over assigning the images using code. If you ever want to change the image, it's simple in the Inspector, as opposed to editing code.

Using splash screens between game levels

Displaying the splash screens

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.

Displaying the splash screens

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:

Displaying the splash screens

An analysis of the preceding code screenshot is given below:

In StateManager:

Line 10: public GameData gameDataRef;

  • The gameDataRef object reference stores a reference to a GameData Component object
  • Therefore the type is GameData
  • It is also public since other classes will be accessing this variable

Line 30: gameDataRef = GetComponent<GameData>();

  • In the Start() method, we call GetComponent() to get a reference to the GameData Component object
  • The reference is assigned to the gameDataRef variable

Line 9: [HideInInspector]

  • This is a Unity attribute that will prevent a public variable from showing in the Inspector
  • Since we want the gameDataRef 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 showing

Here'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:

Displaying the splash screens

In BeginState:

Line 19: Code removed.

Line 23: The GUI.DrawTexture method.

  • The GUI.DrawTexture() method draws a Texture2D in a rectangle that is the size of the game screen
  • The manager.gameDataRef.beginStateSplash statement is the Dot Syntax used to retrieve the image stored in the GameData Component
  • The manager object reference stores a reference to the StateManager Component
  • The gameDataRef variable in StateManager stores the reference to the GameData Component
  • The beginStateSplash variable in GameData stores the Texture2D image
  • This is a direct retrieval of the image since there was no need to call GetComponent()

Line 27: The if statement.

  • This if statement is checking if a GUI button is clicked or if any keyboard key is pressed
  • If either one happens, then SwitchState() is called to switch to SetupState

Here is the result when you click on Play, a fullscreen splash screen with a GUI button:

Displaying the splash screens

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));
      }
    }
  }
}

Have a go hero – adjusting the button size and placement

You may find that the button size and placement isn't very good for the image you're using for the splashscreen. Try changing the button code to place it on different areas of the screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.103.154