using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
public class PlayVideoOnUGUI : MonoBehaviour
{
private Texture texture;
//定义参数获取VideoPlayer组件和RawImage组件
private VideoPlayer videoPlayer;
private RawImage rawImage;
private void Awake()
{
rawImage = this.GetComponent<RawImage>();
texture = rawImage.texture;
videoPlayer = this.GetComponent<VideoPlayer>();
}
private void OnDisable()
{
rawImage.texture = texture;
}
private void Update()
{
//如果videoPlayer没有对应的视频texture,则返回
if (videoPlayer.texture == null)
{
return;
}
//把VideoPlayerd的视频渲染到UGUI的RawImage
rawImage.texture = videoPlayer.texture;
}
}