기술 자료

Unity Engine


User Manual

Script Reference

Unity Engine


Play an animation clip

Learn how to create a PlayableGraph that plays a single animation clip.
읽는 시간 2분최근 업데이트: 한 달 전

This example demonstrates a
PlayableGraph
with one playable output linked to one playable node. The playable node plays a single animation clip (
clip
). Before a playable node can use an animation clip, you must wrap the clip in an
AnimationClipPlayable
.

Prerequisites

To use the
PlayAnimationClip
script in your project, your project must have the following:
  • A GameObject, such as a cube or a capsule. You don't need to manually add an Animator component to this GameObject. The
    RequireComponent
    attribute adds this component if it's not present.
  • 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
PlayAnimationClip
script in your project, follow these steps:
  1. Add a script component to your GameObject. Name the script file
    PlayAnimationClip.cs
    and use the following code:
    using UnityEngine;using UnityEngine.Playables;using UnityEngine.Animations;[RequireComponent(typeof(Animator))]public class PlayAnimationClip : MonoBehaviour{ public AnimationClip clip; PlayableGraph graph; void Start() { graph = PlayableGraph.Create("PlayAnimationClip"); graph.SetTimeUpdateMode(DirectorUpdateMode.GameTime); var output = AnimationPlayableOutput.Create(graph, "Animation", GetComponent<Animator>()); // Wrap the clip in a playable. var clipPlayable = AnimationClipPlayable.Create(graph, clip); // Connect the Playable to an output. output.SetSourcePlayable(clipPlayable); // Plays the Graph. graph.Play(); } void OnDisable() { // Destroys all Playables and PlayableOutputs created by the graph. graph.Destroy(); }}
  2. In the Script component, select the animation clip (
    clip
    ) that the PlayableGraph will play at runtime.
  3. Select Play to switch the Editor to Play mode.
  4. If you have installed the PlayableGraph Visualizer package, select
    PlayAnimationClip
    to display the PlayableGraph:

The PlayableGraph generated by the PlayAnimationClip script

Additional resources