AnimationEvents.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using UnityEngine;
  3. namespace Assets.HeroEditor4D.Common.Scripts.CharacterScripts
  4. {
  5. /// <summary>
  6. /// Animation events. If you want to get animation callback, use it.
  7. /// For example, if you want to know exact hit moment for attack animation, use custom event 'Hit' that is fired in most attack animations.
  8. /// </summary>
  9. public class AnimationEvents : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// Subscribe it to get animation callback.
  13. /// </summary>
  14. public event Action<string> OnEvent = s => { };
  15. /// <summary>
  16. /// Set trigger.
  17. /// </summary>
  18. public void SetTrigger(string triggerName)
  19. {
  20. GetComponent<Animator>().SetTrigger(triggerName);
  21. }
  22. /// <summary>
  23. /// Set bool param, usage example: Idle=false
  24. /// </summary>
  25. public void SetBool(string value)
  26. {
  27. var parts = value.Split('=');
  28. GetComponent<Animator>().SetBool(parts[0], bool.Parse(parts[1]));
  29. }
  30. /// <summary>
  31. /// Set integer param, usage example: WeaponType=2
  32. /// </summary>
  33. public void SetInteger(string value)
  34. {
  35. var parts = value.Split('=');
  36. GetComponent<Animator>().SetInteger(parts[0], int.Parse(parts[1]));
  37. }
  38. /// <summary>
  39. /// Called from animation.
  40. /// </summary>
  41. public void CustomEvent(string eventName)
  42. {
  43. OnEvent(eventName);
  44. }
  45. /// <summary>
  46. /// Set characters' expression. Called from animation.
  47. /// </summary>
  48. public void SetExpression(string expression)
  49. {
  50. GetComponent<Character4D>().Parts.ForEach(i => i.SetExpression(expression));
  51. }
  52. }
  53. }