Advertisement
nbannister

State

Mar 7th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class State : MonoBehaviour {
  7.     public StateMachine Machine;
  8.  
  9.     public static implicit operator bool (State state) {
  10.         return state != null;
  11.     }
  12.  
  13.     public void Initialize (StateMachine machine) {
  14.         Machine = machine;
  15.         OnStateInitialize (machine);
  16.     }
  17.  
  18.     protected virtual void OnStateInitialize (StateMachine machine = null) {
  19.  
  20.     }
  21.  
  22.     public void StateEnter () {
  23.         enabled = true;
  24.         OnStateEnter ();
  25.     }
  26.  
  27.     protected virtual void OnStateEnter () {
  28.  
  29.     }
  30.  
  31.     public void StateExit () {
  32.         OnStateExit ();
  33.         enabled = false;
  34.     }
  35.  
  36.     protected virtual void OnStateExit () {
  37.  
  38.     }
  39.  
  40.     public void OnEnable () {
  41.         if (this != Machine.GetCurrentState) {
  42.             enabled = false;
  43.         }
  44.     }
  45.  
  46.     public void OnDisable () {
  47.         if (this == Machine.GetCurrentState) {
  48.             enabled = true;
  49.         }
  50.     }
  51.    
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement