Version: 2021.1
언어: 한국어
Video Player 컴포넌트
비디오 클립

MovieTexture에서 VideoPlayer로 마이그레이션

레거시 MovieTexture 컴포넌트를 사용하여 동영상을 다운로드하고 재생하는 프로젝트가 있는 경우 새로운 VideoPlayer 컴포넌트를 사용하도록 업데이트할 수 있습니다.

MovieTexture에서 VideoPlayer로 쉽게 마이그레이션할 수 있도록 이 페이지에서는 각 컴포넌트를 사용하여 동영상을 다운로드하고 재생하는 방법에 대한 예시를 제공합니다.

동영상 재생

MovieTexture

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();
            }
        }
    }
}

VideoPlayer

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();
            }
        }
    }
}

동영상 다운로드

MovieTexture

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();
        }
    }
}

VideoPlayer

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();
    }
}

  • 2019–05–07 페이지 게시됨
Video Player 컴포넌트
비디오 클립
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961