Skip to content

Instantly share code, notes, and snippets.

@kitasenjudesign
Created July 14, 2022 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitasenjudesign/5ab2fa8c7d998ffcc77b4e2397cd4d87 to your computer and use it in GitHub Desktop.
Save kitasenjudesign/5ab2fa8c7d998ffcc77b4e2397cd4d87 to your computer and use it in GitHub Desktop.
MyWebcam.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyWebcam : MonoBehaviour
{
private bool _isBackCamera = false;
public WebCamTexture _webcamTex;
[SerializeField] private List<string> _camNames;
[SerializeField] private int _testCamIndex=0;
[SerializeField] private int _currentIndex=0;
private GUIStyle _style;
public void SetCamera( bool isBack ){
_isBackCamera=isBack;
if(_webcamTex){
_webcamTex.Stop();
}
var idx = isBack ? 0 : 1;
WebCamDevice[] devices = WebCamTexture.devices;
_camNames=new List<string>();
for(int i=0;i<devices.Length;i++){
Debug.Log("device" + i + " > "+ devices[i].name);
_camNames.Add(devices[i].name);
//var res = devices[i].availableResolutions;
}
#if UNITY_EDITOR
_webcamTex = new WebCamTexture(devices[_testCamIndex].name, 1280, 720, 30);
_currentIndex=_testCamIndex;
#else
Debug.Log("Select " + idx + " " + devices[idx].name );
_webcamTex = new WebCamTexture(devices[idx].name, 1280, 720, 30);
_currentIndex=idx;
#endif
//Debug.Log( "SIZE " + _webcamTex.width + "x" + _webcamTex.height );
_webcamTex.Play();
}
private void OnGUI(){
if(_webcamTex){
WebCamDevice[] devices = WebCamTexture.devices;
if(_style==null){
_style = new GUIStyle();
_style.fontSize=35;
_style.normal.textColor = Color.red;
}
var res = devices[_currentIndex].availableResolutions;
if(res!=null){
for(int i=0;i<res.Length;i++){
GUI.Label(
new Rect(100, Screen.height/2+50*i, 200, 50),
_currentIndex + "_" + res[i].width + "x" + res[i].height,
_style
);
}
}
GUI.Label(
new Rect(100, Screen.height/2-50, 500, 200),
"webcam " + _webcamTex.width + "x" + _webcamTex.height,
_style
);
GUI.DrawTexture(new Rect(100, 200, 400, 300), _webcamTex, ScaleMode.StretchToFill);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment