This example demonstrates how to use the SetTime()
method to manually set the start of a playable clip. This example also demonstrates how to use the Pause()
method to pause a playable clip.
Before you use the ControlTiming
script in your project, your project must have the following:
RequireComponent
attribute adds this component if it is not present.To use the ControlTiming
script in your project, follow these steps:
Add a script component to your GameObject. Name the script file ControlTiming.cs
and use the following code:
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
[RequireComponent(typeof(Animator))]
public class ControlTiming : MonoBehaviour
{
public AnimationClip clip;
public float time;
PlayableGraph graph;
AnimationClipPlayable clipPlayable;
void Start()
{
// Create and name the graph.
graph = PlayableGraph.Create("ControlTiming");
var output = AnimationPlayableOutput.Create(graph, "Animation", GetComponent<Animator>());
// Wrap the clip in a playable.
clipPlayable = AnimationClipPlayable.Create(graph, clip);
// Connect the Playable to an output.
output.SetSourcePlayable(clipPlayable);
// Play the graph.
graph.Play();
// Pause the clip playable. This stops time from progressing automatically.
clipPlayable.Pause();
}
void Update ()
{
// Control the time manually.
clipPlayable.SetTime(time);
}
void OnDisable()
{
// Destroy all Playables and Outputs created by the graph.
graph.Destroy();
}
}
In the Script component, select the animation clip for the paused clip. You can also set the start time in seconds. If you set a value that is greater than the length of the clip and the clip doesn’t loop, the clip plays the last frame.
Select Play to switch the Editor to Play mode.
In the Script component, experiment with different start times. Because the animation clip is paused, only the first frame of the animation displays.
If you have installed the PlayableGraph Visualizer package, use it to display the PlayableGraph.