Pro_Unit

AnimationEventsHandler

Mar 5th, 2021
713
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×