包含此页的版本:
不含此页的版本:
此示例演示如何使用SetTime()手动设置可播放剪辑的开始的方法。此示例还演示了如何使用Pause()暂停可播放剪辑的方法。
在使用ControlTiming脚本,则项目必须具有以下内容:
RequireComponent属性 添加此组件(如果不存在)。要使用ControlTimingscript,请按照以下步骤作:
将脚本组件添加到游戏对象。为脚本文件命名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();
}
}
在 Script 组件中,选择暂停剪辑的动画剪辑。您还可以设置开始时间(以秒为单位)。如果设定的值大于片段的长度,并且片段没有循环播放,则片段会播放最后一帧。
选择 运行(Play) 将编辑器切换到播放模式。
在脚本组件中,尝试不同的开始时间。由于动画剪辑已暂停,因此仅显示动画的第一帧。
如果你已经安装了 PlayableGraph 可视化工具包,请使用它来显示 PlayableGraph。