Advertisement
infinite_ammo

StateMonoBehaviour.cs

Feb 17th, 2015
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class StateMonoBehaviour : MonoBehaviour
  5. {
  6.     public delegate IEnumerator StateMethod();
  7.     public StateMethod state { get; private set; }
  8.     public StateMethod lastState { get; private set; }
  9.     public string stateName { get; private set; }
  10.     public string lastStateName { get; private set; }
  11.  
  12.     public void SetState(StateMethod stateMethod)
  13.     {
  14.         if (stateMethod != state)
  15.         {
  16.             //if (gameObject.GetComponent<Player>() != null)
  17.             //  Debug.LogWarning("setting state: " + stateMethod.Method.Name);
  18.            
  19.             StopAllCoroutines();
  20.  
  21.             lastStateName = stateName;
  22.             stateName = stateMethod.Method.Name;
  23.  
  24.             //Debug.LogWarning("SetState: " + stateName);
  25.  
  26.             lastState = state;
  27.             state = stateMethod;
  28.  
  29.             StartCoroutine(DoSetState());
  30.         }
  31.     }
  32.  
  33.     void OnEnable()
  34.     {
  35.         //if (state != null)
  36.         //  SetState(state);
  37.     }
  38.  
  39.     void OnDisable()
  40.     {
  41.         StopAllCoroutines();
  42.         state = null;
  43.     }
  44.  
  45.     IEnumerator DoSetState()
  46.     {
  47.         yield return null;
  48.         StartCoroutine(state());
  49.     }
  50.  
  51.     public void InvokeState(StateMethod stateMethod, float time)
  52.     {
  53.         StartCoroutine(DoInvokeState(stateMethod, time));
  54.     }
  55.  
  56.     IEnumerator DoInvokeState(StateMethod stateMethod, float time)
  57.     {
  58.         yield return new WaitForSeconds(time);
  59.         SetState(stateMethod);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement