Unity初学者のため、備忘です。
webcamTextureを利用した最もシンプルな例
構成
Windows 10
Unity2017 3.1f1
Android 8.1 Oreo搭載端末
Planeを設置する
Hierarchy -> Create -> 3D Object -> Plane
を選択し、カラのプロジェクトにplaneを配置します。
このplaneに対して取得した映像を表示します。
![2018-07-27.png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F83956%2F830d973b-aa7c-67fb-c14d-2448dbbfc33b.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=832ce1923ef600bf19771d0c9c013437)
なお、私の使用している検証中の端末では以下のサイズが適していました。
![キャプチャ.PNG](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F83956%2F0c8c841e-5032-710b-1039-0e9d17cd472b.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=4c359d2045e14a24afcb1fdd469a6794)
WebCamControllerを実装する
Project -> Create -> C# Script
からコントローラーを作成し、Planeに対してドラッグドロップします。
WebCamController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WebCamController : MonoBehaviour {
int width = 1920;
int height = 1080;
int fps = 60;
WebCamTexture webcamTexture;
void Start () {
WebCamDevice[] devices = WebCamTexture.devices;
webcamTexture = new WebCamTexture(devices[0].name, this.width, this.height, this.fps);
GetComponent<Renderer> ().material.mainTexture = webcamTexture;
webcamTexture.Play();
}
}
Build & Run する
実機でカメラから取得した映像が取得、表示されます。