unity timeline倒播
一、核心:
通过playableDirector.playableGraph.GetRootPlayable(i).SetSpeed(speed)接口,设置PlayableDirector的速度。
二、playableGraph报空
若playableDirector不勾选Play On Awake,则默认没有PlayableGraph,需执行playableDirector…RebuildGraph()创建一个新的。
三、倒播注意事项:
1.先设置playableDirector的WrapMode为None;
2.若开始时间设置为结束时间,则timeline直接会判定播放结束,无法进行倒播,我直接使用了playableDirector.duration - 0.001f。
void SetTimeLineSpeed(PlayableDirector playableDirector, float speed)
{
//保证playableDirector不勾选Play On Awake时,playableDirector.playableGraph能正确使用
if(!playableDirector.playableGraph.IsValid())
playableDirector.RebuildGraph();
if (speed < 0)
{
playableDirector.extrapolationMode = DirectorWrapMode.None;
playableDirector.initialTime = playableDirector.duration - 0.001f;
playableDirector.time = playableDirector.duration - 0.001f;
}
int cont = playableDirector.playableGraph.GetRootPlayableCount();
Debug.Log(cont);
for (int i = 0; i < cont; i++)
{
playableDirector.playableGraph.GetRootPlayable(i).SetSpeed(speed);
}
//playableDirector.Play();
}