Unity Timeline 播放到指定位置

原文

https://www.cnblogs.com/jingjiangtao/p/14274463.html

脚本需要挂载到PlayableDirector组件所在的物体上。

    public class CameraTimelineControl : MonoBehaviour
    {
        [Tooltip("Timeline面板的帧率")]
        public int frameRate = 60;

        private PlayableDirector _playableDirector;
        private float _targetTime;
        private float _deviation = 0.04f;

        private void Awake()
        {
            _playableDirector = GetComponent<PlayableDirector>();
            _playableDirector.timeUpdateMode = DirectorUpdateMode.Manual;
        }

        private void Update()
        {
            UpdateTime();
        }

        public void PlayToFrame(int frame)
        {
            _targetTime = FrameToSeconds(frame);
        }

        public void PlayToSeconds(float seconds)
        {
            _targetTime = seconds;
        }

        private void UpdateTime()
        {
            if (_playableDirector.time > _targetTime + _deviation)
            {
                _playableDirector.time -= Time.deltaTime;
                _playableDirector.Evaluate();
            }
            else if (_playableDirector.time < _targetTime - _deviation)
            {
                _playableDirector.time += Time.deltaTime;
                _playableDirector.Evaluate();
            }
            else if (_playableDirector.time != _targetTime)
            {
                _playableDirector.time = _targetTime;
                _playableDirector.Evaluate();
            }
        }

        private float FrameToSeconds(int frame)
        {
            return (float)frame / frameRate;
        }
    }

 

frameRate字段是Timeline面板所选择的帧率:

 

 

  

 

posted @ 2021-01-14 09:21  是只香菇  阅读(3118)  评论(0编辑  收藏  举报