AnimancerState.DelayedPause.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. using UnityEngine.Playables;
  3. namespace Animancer
  4. {
  5. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerState
  6. partial class AnimancerState
  7. {
  8. /// <summary>
  9. /// Uses <see cref="AnimancerPlayable.RequirePostUpdate"/> to pause a <see cref="AnimancerNode._Playable"/>.
  10. /// </summary>
  11. public class DelayedPause : Key, IUpdatable
  12. {
  13. /************************************************************************************************************************/
  14. /// <summary>The <see cref="AnimancerNode.Root"/>.</summary>
  15. public AnimancerPlayable Root { get; set; }
  16. /// <summary>The state that will be paused.</summary>
  17. public AnimancerState State { get; set; }
  18. /************************************************************************************************************************/
  19. /// <summary>
  20. /// Gets a <see cref="DelayedPause"/> from the <see cref="ObjectPool"/> and initializes it for the `state`.
  21. /// </summary>
  22. public static void Register(AnimancerState state)
  23. {
  24. var root = state.Root;
  25. if (root == null)
  26. return;
  27. var pause = ObjectPool.Acquire<DelayedPause>();
  28. pause.Root = root;
  29. pause.State = state;
  30. root.RequirePostUpdate(pause);
  31. }
  32. /************************************************************************************************************************/
  33. /// <summary>
  34. /// Pauses the <see cref="State"/> if <see cref="IsPlaying"/> hasn't been set to true and returns this
  35. /// object to the <see cref="ObjectPool"/>.
  36. /// </summary>
  37. public void Update()
  38. {
  39. if (!State.IsPlaying)
  40. State._Playable.Pause();
  41. Root.CancelPostUpdate(this);
  42. Root = null;
  43. State = null;
  44. ObjectPool.Release(this);
  45. }
  46. /************************************************************************************************************************/
  47. }
  48. }
  49. }