// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
using UnityEngine.Playables;
namespace Animancer
{
///
/// A which executes
/// on each item in an every frame.
///
public class UpdatableListPlayable : PlayableBehaviour
{
/************************************************************************************************************************/
///
/// Since needs to clone an existing instance,
/// we keep a static template to avoid allocating an extra garbage one every time.
/// This also means the fields can't be readonly because field initializers don't run on the clone.
///
private static readonly UpdatableListPlayable Template = new();
/************************************************************************************************************************/
/// The this behaviour is connected to.
private AnimancerGraph _Graph;
/// Objects to be updated before time advances.
private IUpdatable.List _Updatables;
/************************************************************************************************************************/
/// Creates a new .
public static ScriptPlayable Create(
AnimancerGraph graph,
int inputCount,
IUpdatable.List updatables)
{
var playable = ScriptPlayable.Create(graph._PlayableGraph, Template, inputCount);
var instance = playable.GetBehaviour();
instance._Graph = graph;
instance._Updatables = updatables;
return playable;
}
/************************************************************************************************************************/
/// [Internal] Calls on everything added to this list.
///
/// Called by the after the rest of the s are evaluated.
///
public override void PrepareFrame(Playable playable, FrameData info)
=> _Graph.UpdateAll(
_Updatables,
info.deltaTime * info.effectiveParentSpeed,
info.frameId);
/************************************************************************************************************************/
}
}