AnimancerEvent.InvokerFixed.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using System.Collections;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. namespace Animancer
  6. {
  7. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerEvent
  8. partial struct AnimancerEvent
  9. {
  10. /// <summary>Executes <see cref="Invoker.InvokeAllAndClear"/> after animations in the Fixed Update cycle.</summary>
  11. /// https://kybernetik.com.au/animancer/api/Animancer/InvokerFixed
  12. [AnimancerHelpUrl(typeof(InvokerFixed))]
  13. [AddComponentMenu("")]// Singleton creates itself.
  14. public class InvokerFixed : Invoker
  15. {
  16. /************************************************************************************************************************/
  17. private static InvokerFixed _Instance;
  18. /// <summary>Creates the singleton instance.</summary>
  19. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  20. public static InvokerFixed Initialize()
  21. => AnimancerUtilities.InitializeSingleton(ref _Instance);
  22. /************************************************************************************************************************/
  23. /// <summary>Should this system execute events?</summary>
  24. /// <remarks>If disabled, this system will not be re-enabled automatically.</remarks>
  25. public static bool Enabled
  26. {
  27. get => _Instance != null && _Instance.enabled;
  28. set
  29. {
  30. if (value)
  31. {
  32. Initialize();
  33. _Instance.enabled = true;
  34. }
  35. else if (_Instance != null)
  36. {
  37. _Instance.enabled = false;
  38. }
  39. }
  40. }
  41. /************************************************************************************************************************/
  42. /// <summary>A cached instance of <see cref="UnityEngine.WaitForFixedUpdate"/>.</summary>
  43. public static readonly WaitForFixedUpdate
  44. WaitForFixedUpdate = new();
  45. /************************************************************************************************************************/
  46. /// <summary>Starts the <see cref="LateFixedUpdate"/> coroutine.</summary>
  47. protected virtual void OnEnable()
  48. => StartCoroutine(LateFixedUpdate());
  49. /************************************************************************************************************************/
  50. /// <summary>After animation update with fixed timestep.</summary>
  51. private IEnumerator LateFixedUpdate()
  52. {
  53. while (true)
  54. {
  55. yield return WaitForFixedUpdate;
  56. InvokeAllAndClear();
  57. }
  58. }
  59. /************************************************************************************************************************/
  60. }
  61. }
  62. }