KITASENJU DESIGN BLOG

memo, html, javascript, unity

set webcams to dropdown

WebcamをDropdownから選べるようにする

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

public class WebCamSelector : MonoBehaviour
{

    [SerializeField] private TMP_Dropdown _dropDown;
    private List<string> _optionList;
    // Start is called before the first frame update
    void Start()
    {
        _optionList = new List<string>();

        WebCamDevice[] devices = WebCamTexture.devices;
        for(int i=0;i<devices.Length;i++){
            _optionList.Add( devices[i].name );
        }

        _dropDown.ClearOptions();//一度すべてのOptionsをクリア
        _dropDown.AddOptions(_optionList);

        _dropDown.onValueChanged.AddListener(_onChange);
        
        Debug.Log("current >> " + _optionList[_dropDown.value] );

    }

    private void _onChange(int index){

        Debug.Log( "select >>>> " + _optionList[index] );

    }


}

"FOOTER"