Unity TimeLine循环播放某个时间段

1、设置Playable Director的Update Method为GameTime模式
在这里插入图片描述

2、API : using UnityEngine.Playables;
我们需要用到PlayableDirector的time属性
在这里插入图片描述
3、设置开始和结束时间段(使用的帧率)我在0-158帧循环和158到290帧之间循环
在这里插入图片描述
4、代码

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;
}

代码笔记

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值