主要原因:“视频播放”代码部分执行过晚,将其提前准备,准备好之后再进行其他游戏对象处理。
解决方法:增加了_videoPlayer.prepareCompleted += OnPreCompleted;
videoPlayer.prepareCompleted
在视频播放器准备完成后调用。
当调用这个回调后,可以开始播放,并且帧会立即可用。
VideoPlayer:
(代码部分有删减,仅作为思路参考)
public class ImageCtrl : MonoBehaviour
{
private GameObject video1Button;
public VideoPlayer _videoPlayer;
private bool isPlaying = false;//是否正在播放
private void Awake()
{
video1Button = transform.Find("PlayVideoBtn").gameObject;
_videoPlayer.prepareCompleted += OnPreCompleted;
}
private void OnPreCompleted(VideoPlayer source)
{
Debug.Log("OnPreCompleted");
/*其他游戏对象处理*/
video1Button.gameObject.SetActive(false);
}
void Start()
{
Init();
video1Button.GetComponent<Button>().onClick.AddListener(() =>
{
isPlaying = true;
PalyVideo();
});
}
private void Init()
{
_videoPlayer.Pause();
}
private void Update()
{
if (isPlaying)
{
PalyVideo();
}
}
/// <summary>
/// 播放视频
/// </summary>
private void PalyVideo()
{
_videoPlayer.Play();
//Debug.Log("PalyVideo");
}
}