유니티 안에서 웹캠(WebCamTexture) 화면 띄우는 방법
유니티 화면에서 카메라를 띄우는 방법은 매우 간단합니다.
https://docs.unity3d.com/kr/530/ScriptReference/WebCamTexture.html
UnityEngine.WebCamTexture - Unity 스크립팅 API
WebCam Textures are textures onto which the live video input is rendered.
docs.unity3d.com
GetComponent - RawImge 오브젝트를 하나 만들어주고
유니티 엔진에서 제공하는 WebCamTexture 를 사용해줍니다
WebCamTexture는 라이브 비디오 입력을 렌더링하는 텍스쳐로 카메라를 그려주기 위해서 사용됩니다
private void Start()
{
WebCamDevice[] devices = WebCamTexture.devices;
for (int i = 0; i < devices.Length; i++)
{
Debug.Log(devices[i].name);
}
}
WebCamTexture.devices 는 사용가능한 디바이스의 리스트를 반환해주는 변수로
위와 같이 디버그를 찍어주면 사용할 디바이스의 리스트 이름을 불러올수 있습니다.
또는
WebCamTexture.dieviceName을 사용하여
해당 디바이스의 이름을 지정해 사용 가능합니다.
public class WebcamSample : MonoBehaviour
{
public RawImage display;
WebCamTexture camTexture;
private int currentIndex = 0;
private void Start()
{
if (camTexture != null)
{
display.texture = null;
camTexture.Stop();
camTexture = null;
}
WebCamDevice device = WebCamTexture.devices[currentIndex];
camTexture = new WebCamTexture(device.name);
display.texture = camTexture;
camTexture.Play();
}
}
장치를 사용하기 전에
WebCamTexture를 사용하고 있으면
사용하던 camTexture를 멈추고
새로운 장치를 WebCamDevice에 넣어주고
camTexture = new WebCamTexture(device.name);
새로운 WebCamTexture를 생성해주고
Play()를 하여 카메라를 시작해주면 끝!
스크립트안에 그려줄 RawImg를 넣어주면됨