VideoPlayer.Prepare
Declaration
public void Prepare();Описание
Подготавливает движок воспроизведения, чтобы он был готов к воспроизведению.
Для подготовки движок воспроизведения резервирует ресурсы, необходимые для воспроизведения, и предварительно загружает часть контента, который будет воспроизводиться. Если подготовка успешна, этот метод выдает VideoPlayer.prepareCompleted событие и наборы VideoPlayer.isPrepared до trueVideoPlayer затем готова к отображению кадров немедленно и вы можете получить доступ ко всем свойствам, связанным с источником.
Если вы не подготовили VideoPlayer перед просмотром видео, VideoPlayer.Play метод будет делать подготовку, но видео не будет воспроизводиться мгновенно. Если вы используете VideoPlayer.Stop, VideoPlayer снова становится неподготовленным, потому что он высвобождает свои ресурсы по причинам производительности. Чтобы остановить видео, но сохранить его подготовку, используйте VideoPlayer.Pause вместо этого.
Дополнительные ресурсы VideoPlayer.isPrepared.
using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; // The button to play the video in the script only becomes interactable after the preparation is finished. // To start the preparation process, press the Space key in Play mode.
// Attach this script to the GameObject you want to play a video clip on. // Attach a VideoPlayer component with a video clip and assign a UI Button in the Inspector.
public class PrepareExample: MonoBehaviour { VideoPlayer videoPlayer; public Button playButton;
private void Awake() { // Get the VideoPlayer component attached to GameObject with this script attached. videoPlayer = GetComponent<VideoPlayer>(); // Attach the event handler, which triggers when the VideoPlayer finishes its preparation. videoPlayer.prepareCompleted += OnPrepareCompleted; videoPlayer.playOnAwake = false; playButton.interactable = false; }
// Event handler for when VideoPlayer finishes the preparation process. void OnPrepareCompleted(VideoPlayer vp) { Debug.Log("Preparation complete. You can now play the video."); // Preparation is complete so allow interactions with the play button. playButton.interactable = true; playButton.onClick.AddListener(OnPlayButtonClicked); }
void OnPlayButtonClicked() { // If the play button is clicked and the preparation is done, play the video. if(videoPlayer.isPrepared) { videoPlayer.Play(); } }
private void Update() { // Press Spacebar to prepare the video. if (Input.GetKeyDown("space")) { if (!videoPlayer.isPrepared) { videoPlayer.Prepare(); } } } }