Version: 6000.3
语言: 中文
控制播放状态
创建具有不同输出的 PlayableGraph

控制可播放片段的时序

此示例演示如何使用SetTime()手动设置可播放剪辑的开始的方法。此示例还演示了如何使用Pause()暂停可播放剪辑的方法。

先决条件

在使用ControlTiming脚本,则项目必须具有以下内容:

  • 一个游戏对象Unity 场景中的基本对象,可以表示角色、道具、风景、相机、航路点等。游戏对象的功能由附加到它的组件定义。更多信息
    请参阅术语表
    例如立方体或胶囊。您无需手动添加Animator 组件模型上的一个组件,使用动画系统为该模型设置动画。该组件具有对控制动画的 Animator 控制器资源的引用。更多信息
    请参阅术语表
    到这个游戏对象。这RequireComponent属性 添加此组件(如果不存在)。
  • 对游戏对象的属性进行动画处理的动画剪辑。例如,对游戏对象的位置和旋转进行动画处理的剪辑。

添加并运行脚本

要使用ControlTimingscript,请按照以下步骤作:

  1. 将脚本组件添加到游戏对象。为脚本文件命名ControlTiming.cs并使用以下代码:

    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();
        }
    }
    
  2. 在 Script 组件中,选择暂停剪辑的动画剪辑。您还可以设置开始时间(以秒为单位)。如果设定的值大于片段的长度,并且片段没有循环播放,则片段会播放最后一帧。

  3. 选择 运行(Play) 将编辑器切换到播放模式。

  4. 在脚本组件中,尝试不同的开始时间。由于动画剪辑已暂停,因此仅显示动画的第一帧。

  5. 如果你已经安装了 PlayableGraph 可视化工具包,请使用它来显示 PlayableGraph。

其他资源

控制播放状态
创建具有不同输出的 PlayableGraph