블로그 이미지
Every unexpected event is a path to learning for you. blueasa

카테고리

분류 전체보기 (2794)
Unity3D (852)
Programming (478)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (185)
협업 (11)
3DS Max (3)
Game (12)
Utility (68)
Etc (98)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (55)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday

Ever stuck on how to render a video on a TV screen in a game? Lets us learn the concept of playing a video in Unity.

You can import many different formats of video file into Unity. Unity stores such imported video files as VideoClip assets. A Video Clip is an imported video file, which the Video Player component uses to play video content. The playing video also accompanies audio content, if it has any. Typical file extensions for video files include .mp4, .mov, .webm, and .wmv.

To check the video file compatibility in unity, click here.

The Video Player component

Video Player component is used to attach video files to GameObjects, and play them on the GameObject’s Texture at run time.

By default, the Material Property of a Video Player component is set to MainTex, which means that when the Video Player component is attached to a GameObject that has a Renderer, it automatically assigns itself to the Texture on that Renderer (because this is the main Texture for the GameObject).

You can also set the following specific target for the video to play on, using the Render Mode property of Video Player:

  • A Camera plane
  • A Render Texture
  • A Material Texture parameter
  • Any Texture field in a component

Playing a video already in Assets

If you already have a video clip in your asset folder, you can simply set the Source property of Video Player component to Video Clip and drag and drop your video clip in the inspector. Alternatively, you can set the video clip in script using clip property.

    public VideoPlayer myVideoPlayer;
    public VideoClip myClip;
    myVideoPlayer.clip = myClip;

You can tick the property Play On Awake if you want to play the video the moment the Scene launches. Otherwise you can trigger the video playback with Play() command at another point during run time.

    myVideoPlayer.Play();

Playing Video from url in Unity

You might be having a video on server which you want to load on runtime. Here is how we can do it:

    private IEnumerator playVideoInThisURL(string _url)
    {
        myVideoPlayer.source = UnityEngine.Video.VideoSource.Url;
        myVideoPlayer.url = _url;
        myVideoPlayer.Prepare();

        while (myVideoPlayer.isPrepared == false){
           yield return null; 
        }
        myVideoPlayer.Play();
    }

Here, we have waited till the video player successfully prepared the content to be played.

Note, the url can also contain the path of the file.

Downloading and Saving Video from Url

The above line of code plays the video directly from the url. Alternatively, you can download the video file and then play the video from this path. The line of code below downloads the video clip and saves it at Application.persistentDataPath.

    private IEnumerator loadVideoFromThisURL(string _url)
    {
        UnityWebRequest _videoRequest = UnityWebRequest.Get(_url);
        yield return _videoRequest.SendWebRequest();

        if (_videoRequest.isDone == false || _videoRequest.error != null)
        {
            yield return null;
        }
        else
        {
            Debug.Log("Video Done - " + _videoRequest.isDone);
            byte[] _videoBytes = _videoRequest.downloadHandler.data;
            string _pathToFile = Path.Combine(Application.persistentDataPath, "video.mp4");
            File.WriteAllBytes(_pathToFile, _videoBytes);
            StartCoroutine(this.playVideoInThisURL(_pathToFile));
            yield return null;
        }
    }

Now we have learnt the art of playing video in Unity. Drop in your comments for any queries or feedback.

Happy Coding!

 

[출처] https://codesaying.com/playing-video-in-unity/

 

Playing Video in Unity - Code Saying

Ever stuck on how to render a video on a TV screen in a game? Or a full screen video? Lets us learn the art playing a video in Unity.

codesaying.com

 

반응형
Posted by blueasa
, |