How to do it...

To manage an object's behavior using the State pattern architecture, perform the following steps:

  1. Create a new C# script class called GameState:
    public class GameState     {         public enum EventType         {             ButtonWinGame,             ButtonLoseGame         }          protected MyGameManager gameManager;         public GameState(MyGameManager manager)         {             gameManager = manager;         }          public virtual void OnMyStateEntered() {}         public virtual void OnMyStateExit() {}         public virtual void StateUpdate() {}         public virtual void OnEventReceived(EventType eventType) {}     } 
  1. Create a new C# script class called StateGamePlaying:
    using UnityEngine;      public class StateGamePlaying : GameState     {         public StateGamePlaying(MyGameManager manager) :          base(manager) { }  public override void OnMyStateEntered() ...

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.