1、设置Playable Director的Update Method为GameTime模式
2、API : using UnityEngine.Playables;
我们需要用到PlayableDirector的time属性
3、设置开始和结束时间段(使用的帧率)我在0-158帧循环和158到290帧之间循环
4、代码文章来源:https://www.toymoban.com/news/detail-507104.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using System;
public class ModlesManager : MonoBehaviour
{
public GameObject m_Book;//PlayableDirector 组件的载体
public List<TimeScopes> m_TimeScopes = new List<TimeScopes>();//设置时间段
private PlayableDirector m_BookAndLight;
private bool m_IsCanPlay;//是否播放timeline
void Start()
{
m_BookAndLight = m_Book.GetComponent<PlayableDirector>();
}
public void SetIsCanPlay(bool ison)
{
m_IsCanPlay = ison;
}
private void PlayTimeline()
{
if (m_IsCanPlay)
{
m_BookAndLight.time += Time.deltaTime;
//加30是为了在争取在循环的时候能最贴合动画时间,可以自己测试自己动画时间
if(m_BookAndLight.time>= Getfps(m_TimeScopes[1].m_TimeStart+30))
{
}
if (m_BookAndLight.time >= Getfps(m_TimeScopes[m_TimeScopeindex].m_TimeEnd))
{
if (m_TimeScopes[m_TimeScopeindex].m_IsLoop)
{
m_BookAndLight.time = Getfps(m_TimeScopes[m_TimeScopeindex].m_TimeStart);
}else
{
m_BookAndLight.Pause();
m_IsCanPlay = false;
}
}
}
}
/// <summary>
/// 计算当前帧率 动画为每秒60帧
/// </summary>
/// <param name="targetfps"></param>
/// <returns></returns>
private double Getfps(double targetfps)
{
return targetfps / 60;
}
}
[Serializable]
public class TimeScopes
{
public double m_TimeStart;
public double m_TimeEnd;
public bool m_IsLoop;
}
代码笔记文章来源地址https://www.toymoban.com/news/detail-507104.html
到了这里,关于Unity TimeLine循环播放某个时间段的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!