How it works...

The scene is very straightforward for this recipe. There is the single Main Camera GameObject that has the MyGameManager script object component attached to it.

A C# scripted class is defined for each state that the game needs to manage—for this example, StateGamePlaying, StateGameWon, and StateGameLost. Each of these state classes is a subclass of GameState. GameState defines properties 
and methods that all subclass states will possess:

  • An enumerated type EventType, which defines the two possible button click events that the game might generate: ButtonWinGame and ButtonLoseGame.
  • The gameManager variable so that each state object has a link to the game manager.
  • The constructor method that accepts a reference to MyGameManager ...

Get Unity 2018 Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.