Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AnimatorStateObserver
- {
- private Animator _animator;
- /// <summary>
- ///
- /// </summary>
- /// <typeparam name="int">Animation Hash</typeparam>
- /// <typeparam name="EmeraldAIState">State</typeparam>
- /// <returns></returns>
- private Dictionary<int, EmeraldAIState> _states;
- private Dictionary<int, Action> _onEnableActions;
- private Dictionary<int, Action> _onDisableActions;
- public AnimatorStateObserver (Animator animator, Dictionary<int, EmeraldAIState> states = null)
- {
- _animator = animator;
- _states = states ?? new Dictionary<int, EmeraldAIState> ();
- _onEnableActions = new Dictionary<int, Action> ();
- _onDisableActions = new Dictionary<int, Action> ();
- }
- public void Subscribe ()
- {
- foreach (var item in _states)
- {
- int key = item.Key;
- item.Value.OnEnable +=
- (_onEnableActions[key] = () => Set (key, true));
- item.Value.OnDisable +=
- (_onDisableActions[key] = () => Set (key, false));
- }
- }
- public void UnSubcribe ()
- {
- foreach (var item in _states)
- {
- int key = item.Key;
- item.Value.OnEnable -= _onEnableActions[key];
- _onEnableActions.Remove(key);
- item.Value.OnDisable -= _onDisableActions[key];
- _onDisableActions.Remove(key);
- }
- }
- private void Set (int hash, bool active)
- {
- _animator.SetBool (hash, active);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment