EventUtilities.cs 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using System;
  3. using UnityEngine;
  4. using Object = UnityEngine.Object;
  5. namespace Animancer.Examples.Events
  6. {
  7. /// <summary>Various utility delegates which can be assigned to Animancer Events.</summary>
  8. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/events/utilities">Event Utilities</see></example>
  9. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Events/EventUtilities
  10. ///
  11. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(Events) + "/" + nameof(EventUtilities))]
  12. public static class EventUtilities
  13. {
  14. /************************************************************************************************************************/
  15. // Since the methods in this class are intended for use with Animancer Events, they are declared as delegate fields
  16. // rather than regular methods so that assigning them doesn't create any garbage.
  17. /************************************************************************************************************************/
  18. // If you intend to use any of these methods in your own scripts, it is recommended that you copy the ones you need into
  19. // your own utility class so that you can delete the Animancer Examples once you are done with them.
  20. /************************************************************************************************************************/
  21. /// <summary>
  22. /// Logs a message with the details of the <see cref="AnimancerEvent.CurrentEvent"/> and
  23. /// <see cref="AnimancerEvent.CurrentState"/>.
  24. /// </summary>
  25. /// <example>
  26. /// Go through every event in a transition and make it Log the event in addition to its normal callback:
  27. /// <para></para><code>
  28. /// [SerializeField] private ClipTransition _Transition;
  29. ///
  30. /// private void Awake()
  31. /// {
  32. /// for (int i = 0; i &lt; _Transition.Events.Count; i++)
  33. /// {
  34. /// _Transition.Events.AddCallback(i, EventUtilities.LogCurrentEvent);
  35. /// }
  36. /// }
  37. /// </code></example>
  38. public static readonly Action LogCurrentEvent = () =>
  39. {
  40. Debug.Log(
  41. $"An {nameof(AnimancerEvent)} was triggered:" +
  42. $"\n- Event: {AnimancerEvent.CurrentEvent}" +
  43. $"\n- State: {AnimancerEvent.CurrentState.GetDescription()}",
  44. AnimancerEvent.CurrentState.Root?.Component as Object);
  45. };
  46. /************************************************************************************************************************/
  47. /// <summary>Sets the <see cref="AnimancerState.Time"/> of the <see cref="AnimancerEvent.CurrentState"/> to 0.</summary>
  48. /// <example>
  49. /// Play a non-looping animation but force it to loop:
  50. /// <para></para><code>
  51. /// [SerializeField] private AnimancerComponent _Animancer;
  52. /// [SerializeField] private AnimationClip _NonLoopingClip;
  53. ///
  54. /// private void Awake()
  55. /// {
  56. /// var state = _Animancer.Play(_NonLoopingClip);
  57. /// state.Events.OnEnd = EventUtilities.RestartCurrentState;
  58. /// }
  59. /// </code></example>
  60. public static readonly Action RestartCurrentState = () =>
  61. {
  62. AnimancerEvent.CurrentState.Time = 0;
  63. };
  64. /************************************************************************************************************************/
  65. /// <summary>
  66. /// Pauses the <see cref="AnimancerEvent.CurrentState"/> at the current
  67. /// <see cref="AnimancerEvent.normalizedTime"/>.
  68. /// </summary>
  69. /// <remarks>
  70. /// This can be useful for having an animation which stops at certain times to wait for something else to
  71. /// happen without needing to separate the animation into separate <see cref="AnimationClip"/>s.
  72. /// </remarks>
  73. public static readonly Action PauseAtCurrentEvent = () =>
  74. {
  75. AnimancerEvent.CurrentState.IsPlaying = false;
  76. AnimancerEvent.CurrentState.NormalizedTime = AnimancerEvent.CurrentEvent.normalizedTime;
  77. };
  78. /************************************************************************************************************************/
  79. }
  80. }