LoginSignup
13
16

More than 5 years have passed since last update.

MovieTextureの具体的な使い方

Last updated at Posted at 2016-07-14


本記事はUnity5.5までを対象としたものです。
Unity5.6以降からVideoPlayerが実装されましたので
そちらを使用することをお勧めします。

Unityで動画を再生したいときにMovieTextureを使う方法があります。
公式のマニュアル( http://docs.unity3d.com/ja/current/Manual/class-MovieTexture.html )だと
「対応フォーマットの動画ファイルをプロジェクトに入れると自動的に変換される。
変換にはQuicktimeが使われるのでインストールしておく必要がある。」

と言うような事が記載されていますが、Windows版QuickTimeはセキュリティの
問題を残したままバージョンアップが停止しており事実上使用できません。

また、マニュアルに記載されている内容だと具体的な方法が
わかりにくいので実際に動画を再生する方法をまとめることにしました。

1)動画データを用意する

MovieTextureは実際にはogvを再生しているようなのでUnityによる変換を使用しないで
予めogvを用意しておきます。ogvはffmpegで変換可能です。
ビルド済みのffmpegは https://ffmpeg.zeranoe.com/builds/ あたりで手に入ります。

変換例

enc.bat
ffmpeg -y -i "title.mp4" -b 8M -ab 128k "title.ogv"

2)Unityのプロジェクトに配置する

変換したogvファイルをStreamingAssetsフォルダにコピーします。

3)再生するためのスクリプトを書く

MovieTextureはInspectorでマテリアルにテクスチャとして設定という
流れではなくMovieTextureを扱うスクリプトを書きます。
これをそのままコピペして使用しても構いません。

Movie.cs

    using UnityEngine;
    using System.Collections;

    public class Movie : MonoBehaviour {

        public string _movieFile;

        // Use this for initialization
        void Start () {

            StartCoroutine(moviePlay(_movieFile));
        }

        // Update is called once per frame
        void Update () {

        }

        private IEnumerator moviePlay(string movieFile)
        {
            string  movieTexturePath    = Application.streamingAssetsPath + "/" + movieFile;
            string  url                 = "file://" + movieTexturePath;
            WWW     movie               = new WWW(url);

            while (!movie.isDone) {
                yield return null;
            }

            MovieTexture movieTexture = movie.movie;

            while (!movieTexture.isReadyToPlay) {
                yield return null;
            }

            var renderer = GetComponent<MeshRenderer>();
            renderer.material.mainTexture = movieTexture;

            movieTexture.loop = true;
            movieTexture.Play ();

    #if false
            //オーディオを使用する場合はこの部分を有効にしてください
            var audioSource = GetComponent<AudioSource>();
            audioSource.clip = movieTexture.audioClip;
            audioSource.loop = true;
            audioSource.Play ();
    #endif
        }
    }

4)コンポーネントを設定する

GameObjectにスクリプトを設定すると動画を再生するGameObjectになります。
(マテリアルは何か適当なものを設定しておきましょう)

設定例

unity_movietexture_inspector.jpg

余談

再生するとエラーが出ますが調べてみたところ
どうやらUnity側のバグのようです(5.4でも未解決だそうです)

13
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
16