1、設(shè)置Playable Director的Update Method為GameTime模式
2、API : using UnityEngine.Playables;
我們需要用到PlayableDirector的time屬性
3、設(shè)置開始和結(jié)束時間段(使用的幀率)我在0-158幀循環(huán)和158到290幀之間循環(huán)
4、代碼文章來源:http://www.zghlxwxcb.cn/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>();//設(shè)置時間段
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是為了在爭取在循環(huán)的時候能最貼合動畫時間,可以自己測試自己動畫時間
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>
/// 計算當(dāng)前幀率 動畫為每秒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;
}
代碼筆記文章來源地址http://www.zghlxwxcb.cn/news/detail-507104.html
到了這里,關(guān)于Unity TimeLine循環(huán)播放某個時間段的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!