KITASENJU DESIGN BLOG

memo, html, javascript, unity

複数webcamからひとつを選択

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

public class MyWebcam2 : MonoBehaviour
{
    private WebCamDevice[] devices;
    private WebCamTexture webCamTexture;
    [SerializeField] private Material outputMat;
    private GUIStyle _guiStyle;
    private bool _isStart=false;
    // Start is called before the first frame update
    void Start()
    {

        devices = WebCamTexture.devices;

    }
    private void OnGUI(){
        
        if(_isStart) return;

        if(_guiStyle==null){
            _guiStyle = new GUIStyle(GUI.skin.button);
            _guiStyle.fontSize = 22;
            _guiStyle.normal.textColor = Color.white;
        }

        for(var i=0;i<devices.Length;i++){
            if( GUI.Button(new Rect(50, 100*i, 300, 100),devices[i].name,_guiStyle) ){
                StartWebcam(i);
            }
        }

    }

    private void StartWebcam(int idx){
        
        _isStart=true;

        webCamTexture = new WebCamTexture(devices[idx].name, 1280, 720, 30);
        webCamTexture.Play();
        outputMat.SetTexture("_MainTex",webCamTexture);

    }

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

}

"FOOTER"