SHARE
TWEET

AnimatorStateObserver

Pro_Unit Sep 15th, 2019 (edited) 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  public class AnimatorStateObserver
  2.     {
  3.         private Animator _animator;
  4.         /// <summary>
  5.         ///
  6.         /// </summary>
  7.         /// <typeparam name="int">Animation Hash</typeparam>
  8.         /// <typeparam name="EmeraldAIState">State</typeparam>
  9.         /// <returns></returns>
  10.         private Dictionary<int, EmeraldAIState> _states;
  11.         private Dictionary<int, Action> _onEnableActions;
  12.         private Dictionary<int, Action> _onDisableActions;
  13.  
  14.         public AnimatorStateObserver (Animator animator, Dictionary<int, EmeraldAIState> states = null)
  15.         {
  16.             _animator = animator;
  17.             _states = states ?? new Dictionary<int, EmeraldAIState> ();
  18.  
  19.             _onEnableActions = new Dictionary<int, Action> ();
  20.             _onDisableActions = new Dictionary<int, Action> ();
  21.         }
  22.  
  23.         public void Subscribe ()
  24.         {
  25.             foreach (var item in _states)
  26.             {
  27.                 int key = item.Key;
  28.  
  29.                 item.Value.OnEnable +=
  30.                     (_onEnableActions[key] = () => Set (key, true));
  31.  
  32.                 item.Value.OnDisable +=
  33.                     (_onDisableActions[key] = () => Set (key, false));
  34.  
  35.             }
  36.         }
  37.  
  38.         public void UnSubcribe ()
  39.         {
  40.             foreach (var item in _states)
  41.             {
  42.                 int key = item.Key;
  43.  
  44.                 item.Value.OnEnable -= _onEnableActions[key];
  45.                 _onEnableActions.Remove(key);
  46.                
  47.                 item.Value.OnDisable -= _onDisableActions[key];
  48.                 _onDisableActions.Remove(key);
  49.             }
  50.         }
  51.  
  52.         private void Set (int hash, bool active)
  53.         {
  54.             _animator.SetBool (hash, active);
  55.         }
  56.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top