如果您有使用传统 MovieTexture 组件下载和播放电影的项目,您可以更新它们以使用更新的 VideoPlayer 组件。
为了帮助您从 MovieTexture 迁移到 VideoPlayer,本页提供了使用每个组件下载和回放电影的示例。
using UnityEngine;
public class PlayMovieMT : MonoBehaviour
{
public AudioClip movieAudioClip;
public MovieTexture movieTexture;
void Start()
{
var audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = movieAudioClip;
}
void Update()
{
if (Input.GetButtonDown("Jump"))
{
var audioSource = GetComponent<AudioSource>();
GetComponent<Renderer>().material.mainTexture = movieTexture;
if (movieTexture.isPlaying)
{
movieTexture.Pause();
audioSource.Pause();
}
else
{
movieTexture.Play();
audioSource.Play();
}
}
}
}
using UnityEngine;
public class PlayMovieVP : MonoBehaviour
{
public UnityEngine.Video.VideoClip videoClip;
void Start()
{
var videoPlayer = gameObject.AddComponent<UnityEngine.Video.VideoPlayer>();
var audioSource = gameObject.AddComponent<AudioSource>();
videoPlayer.playOnAwake = false;
videoPlayer.clip = videoClip;
videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.MaterialOverride;
videoPlayer.targetMaterialRenderer = GetComponent<Renderer>();
videoPlayer.targetMaterialProperty = "_MainTex";
videoPlayer.audioOutputMode = UnityEngine.Video.VideoAudioOutputMode.AudioSource;
videoPlayer.SetTargetAudioSource(0, audioSource);
}
void Update()
{
if (Input.GetButtonDown("Jump"))
{
var vp = GetComponent<UnityEngine.Video.VideoPlayer>();
if (vp.isPlaying)
{
vp.Pause();
}
else
{
vp.Play();
}
}
}
}
using UnityEngine;
public class DownloadMovieMT : MonoBehaviour
{
void Start()
{
StartCoroutine(GetMovieTexture());
}
IEnumerator GetMovieTexture()
{
using (var uwr = UnityWebRequestMultimedia.GetMovieTexture("https://myserver.com/mymovie.ogv"))
{
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.LogError(uwr.error);
yield break;
}
MovieTexture movie = DownloadHandlerMovieTexture.GetContent(uwr);
GetComponent<Renderer>().material.mainTexture = movie;
movie.loop = true;
movie.Play();
}
}
}
using UnityEngine;
public class DownloadMovieVP : MonoBehaviour
{
void Start()
{
var vp = gameObject.AddComponent<UnityEngine.Video.VideoPlayer>();
vp.url = "https://myserver.com/mymovie.mp4";
vp.isLooping = true;
vp.renderMode = UnityEngine.Video.VideoRenderMode.MaterialOverride;
vp.targetMaterialRenderer = GetComponent<Renderer>();
vp.targetMaterialProperty = "_MainTex";
vp.Play();
}
}