Advertisement
Pro_Unit

AnimationEventsHandler

Mar 5th, 2021
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class AnimationEventsHandler : MonoBehaviour
  7. {
  8.     [SerializeField] private List<EventAction> _actions;
  9.     private Dictionary<int, EventAction> _actionsDictionary = new Dictionary<int, EventAction>();
  10.  
  11.     private void OnValidate()
  12.     {
  13.         foreach (var eventAction in _actions)
  14.             eventAction?.OnValidate();
  15.     }
  16.  
  17.     private void Start()
  18.     {
  19.         InitActionsDictionary();
  20.     }
  21.  
  22.     private void InitActionsDictionary()
  23.     {
  24.         _actionsDictionary = _actions.ToDictionary(action => (int) action);
  25.     }
  26.  
  27.     public void CallEvent(AnimationEvent animationEvent)
  28.     {
  29.         if (_actionsDictionary.Count == 0) InitActionsDictionary();
  30.  
  31.         if (!_actionsDictionary.TryGetValue(animationEvent, out var action))
  32.             action = _actions.Find(eventAction => (int) eventAction == (int) animationEvent);
  33.  
  34.         action?.Invoke();
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement