UpdatableListPlayable.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine.Playables;
  3. namespace Animancer
  4. {
  5. /// <summary>
  6. /// A <see cref="PlayableBehaviour"/> which executes <see cref="IUpdatable.Update"/>
  7. /// on each item in an <see cref="IUpdatable.List"/> every frame.
  8. /// </summary>
  9. public class UpdatableListPlayable : PlayableBehaviour
  10. {
  11. /************************************************************************************************************************/
  12. /// <summary>
  13. /// Since <see cref="ScriptPlayable{T}.Create(PlayableGraph, int)"/> needs to clone an existing instance,
  14. /// we keep a static template to avoid allocating an extra garbage one every time.
  15. /// This also means the fields can't be readonly because field initializers don't run on the clone.
  16. /// </summary>
  17. private static readonly UpdatableListPlayable Template = new();
  18. /************************************************************************************************************************/
  19. /// <summary>The <see cref="AnimancerGraph"/> this behaviour is connected to.</summary>
  20. private AnimancerGraph _Graph;
  21. /// <summary>Objects to be updated before time advances.</summary>
  22. private IUpdatable.List _Updatables;
  23. /************************************************************************************************************************/
  24. /// <summary>Creates a new <see cref="UpdatableListPlayable"/>.</summary>
  25. public static ScriptPlayable<UpdatableListPlayable> Create(
  26. AnimancerGraph graph,
  27. int inputCount,
  28. IUpdatable.List updatables)
  29. {
  30. var playable = ScriptPlayable<UpdatableListPlayable>.Create(graph._PlayableGraph, Template, inputCount);
  31. var instance = playable.GetBehaviour();
  32. instance._Graph = graph;
  33. instance._Updatables = updatables;
  34. return playable;
  35. }
  36. /************************************************************************************************************************/
  37. /// <summary>[Internal] Calls <see cref="IUpdatable.Update"/> on everything added to this list.</summary>
  38. /// <remarks>
  39. /// Called by the <see cref="PlayableGraph"/> after the rest of the <see cref="Playable"/>s are evaluated.
  40. /// </remarks>
  41. public override void PrepareFrame(Playable playable, FrameData info)
  42. => _Graph.UpdateAll(
  43. _Updatables,
  44. info.deltaTime * info.effectiveParentSpeed,
  45. info.frameId);
  46. /************************************************************************************************************************/
  47. }
  48. }