【Unity-iOS】Nativeで動画を撮影してRenderTextureに表示する
Unity-iOSでネイティブの機能を用いて動画を撮影して,撮影した動画をそのままUnity上のRenderTextureに表示する方法の1つ.
Environment
- Unity2019.1.12f1
- iOS 13.1.2 (iPhone XR)
- Xcode 11.1
Methods
1.UnityNativeCameraをインポート
githubより yasirkula/UnityNativeCamera をインポートする.
https://github.com/yasirkula/UnityNativeCamera/releases
このアセットを入れることで簡単にiOSネイティブのカメラ機能をUnityから利用することができる.
2.サンプルコンポーネントの作成
以下がサンプルソース.コピペする.
NativeRecMovieAndPlay.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class NativeRecMovieAndPlay : MonoBehaviour { [SerializeField] private VideoPlayer videoPlayer; [SerializeField] private Text pathText; private void Update() { // 画面がタップされていない場合スキップ if (!Input.GetMouseButtonDown(0)) return; // まだカメラにアクセスできない場合スキップ if (NativeCamera.IsCameraBusy()) return; // 動画を撮影 NativeCamera.RecordVideo(OnRecord); } // 撮影後に呼び出される private void OnRecord(string path) { // 撮影されていない場合スキップ if (string.IsNullOrEmpty(path)) return; //Textコンポーネントがアタッチされていたら撮影した動画のパスを出力する if(pathText != null) pathText.text = "file://" + path; //VideoPlayerコンポーネントに動画のファイルパスを設定 videoPlayer.url = "file://" + path; //再生 videoPlayer.Play(); } } |
3.VideoPlayerコンポーネントの配置とRenderTextureの作成
先に以下のようなRenderTextureを作成しておく.以下のサンプルは1920*1080に設定した図.
今回のRenderTextureの名前はVideoにした.

次にUnityのデフォルトの機能にあるVideoPlayerコンポーネントを空のGameObjectなどを作成してアタッチしておく.
コンポーネントの設定を以下のように設定しておく.
SourceはURLにし,PlayOnAwakeのチェックを外しておく.
Loopなどは適宜変更.
RenderModeをRenderTextureにして,TargetTextureに作成したRenderTextureを設定する.
4.配置など
VideoPlayerコンポーネントをアタッチしたオブジェクトなどにNativeRecMovieAndPlayコンポーネントをアタッチして,インスペクター上でVideoPlayerコンポーネントと必要ならパス確認用のテキストコンポーネントをアタッチする.
RenderTextureはこれだけだと動画再生されていても見えないので,適宜RawImageなどに貼り付けて可視化しておく.
5.Build
通常通りUnityでビルド後Xcodeでビルドする.
上記サンプルソースを変更してなければ,画面をタップ → Nativeカメラ起動,動画撮影,撮影したものの確認 → Unity上のRenderTextureとして動画再生.となる.
Licence
以下のライセンスのソースを利用させて頂いてます.
MITライセンスの”UnityNativeCamera”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
MIT License Copyright (c) 2018 Süleyman Yasir KULA Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |