UnityでWebカメラの画像を表示

コンピューター

Webカメラの画像を表示させるポイントです。

今回の環境

Unity 2021.3.16f1

BUFFALO BSWHD06MBK

作成方法

Hierarchyで、3D Object → Planeをクリックする。

下記のコードをAssets → Create → C# scriptから作成する。

スクリプトの名前をWebCamControllerにする。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class WebCamController : MonoBehaviour
{
    // Start is called before the first frame update

    int width = 1920;
    int height = 1080;
    int fps = 30;
    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();
    }

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

作成したスクリプトを先ほどのPlaneにアタッチ(ドラッグする)する。

Game の所で → Free Aspect → ☑Vysync (Game view only)

これをチェックしておかないと、画像がでません。

キーワード

VSYNC 

Vertical Synchronizing signal

垂直同期信号

fps

frames per second

フレームレート


タイトルとURLをコピーしました