Control the timing of a playable clip
Learn about the SetTime() method and how to manually set the start of a paused playable.
읽는 시간 2분최근 업데이트: 한 달 전
This example demonstrates how to use the method to manually set the start of a playable clip. This example also demonstrates how to use the method to pause a playable clip.
SetTime()Pause()Prerequisites
Before you use the script in your project, your project must have the following:
ControlTiming- A GameObject, such as a cube or a capsule. You don't need to manually add an Animator component to this GameObject. The attribute adds this component if it's not present.
RequireComponent - An animation clip that animates the properties of the GameObject. For example, a clip that animates the position and rotation of the GameObject.
Add and run the script
To use the script in your project, follow these steps:
ControlTiming-
Add a script component to your GameObject. Name the script fileand use the following code:
ControlTiming.csusing 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's 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.