Unity VideoPlayer 指定位置开始播放

如果 source是 videoclip(以下两种方式都可以):

复制代码
  _videoPlayer.Play();
        Debug.Log("time: " + _videoPlayer.clip.length);
        _videoPlayer.time = 10;

[SerializeField] VideoPlayer videoPlayer;

public void SetClipWithTime(VideoClip clip, float time) 
{
    StartCoroutine (SetTimeRoutine(clip, time));
}

IEnumerator SetTimeRoutine (VideoClip clip, float time)
{
    if(videoPlayer.isPlaying)
    {
        videoPlayer.Stop();
    }

    videoPlayer.clip = clip;

    videoPlayer.Prepare();
    yield return new WaitUntil (() => videoPlayer.isPrepared);  
    yield return new WaitUntil (() => videoPlayer.canSetTime);

    videoPlayer.Play();
    videoPlayer.time = time;
}

如果是source是URL:

复制代码
IEnumerator VideoPlay()
    {
        _videoPlayer.Prepare();

        yield return new WaitUntil(() => _videoPlayer.isPrepared);
        yield return new WaitUntil(() => _videoPlayer.canSetTime);
        // 检查是否完成初始化
        //while (!_videoPlayer.isPrepared)
        //{
        //    yield return new WaitForSeconds(1f);
        //    break;
        //}
        // 设置开头是第100帧
        _videoPlayer.frame += 100;
        // 开始播放
        _videoPlayer.Play();
        yield return new WaitForSeconds(0.01f);
        _videoPlayer.Pause();
    }

 IEnumerator VideoPlay()
    {
        _videoPlayer.Prepare();
        // 检查是否完成初始化
        while (!_videoPlayer.isPrepared)
        {
            yield return new WaitForSeconds(1f);
            break;
        }
        // 设置开头是第100帧
        _videoPlayer.frame += 100;
        // 开始播放
        _videoPlayer.Play();
        //yield return new WaitForSeconds(0.01f);
        //_videoPlayer.Pause();
    }
相关推荐
Avalon7124 小时前
Unity3D响应式渲染UI框架UniVue
游戏·ui·unity·c#·游戏引擎
风酥糖6 小时前
Godot游戏练习01-第33节-新增会爆炸的敌人
游戏·游戏引擎·godot
ellis19706 小时前
Unity UI性能优化一之插件【Unity UI Optimization Tool】
unity·性能优化
Zik----9 小时前
Unity基础学习笔记(B站视频课整理)
unity·vr
郑寿昌18 小时前
UE5与UE6在Lumen和Nanite的差异解析
游戏引擎·图形渲染·着色器
郝学胜-神的一滴1 天前
罗德里格斯旋转公式(Rodrigues‘ Rotation Formula)完整推导
c++·unity·godot·图形渲染·three.js·unreal
郑寿昌1 天前
UE6 AI加速Lumen光线追踪降噪技术解析
人工智能·游戏引擎
晴夏。1 天前
GAS下的网络同步的全面分析【超级全面】
游戏引擎·ue·gas·网络同步
田鸡_1 天前
Unity新输入系统(Input System)教学篇
unity·游戏引擎·游戏程序
EQ-雪梨蛋花汤1 天前
【Unity笔记】Unity 音游模板与免费资源:高效构建节奏游戏开发全指南
笔记·unity·游戏引擎