Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created July 4, 2024 18:06
Show Gist options
  • Save TheCuttlefish/2301bc742e6b5f7e228a1f0d35466e33 to your computer and use it in GitHub Desktop.
Save TheCuttlefish/2301bc742e6b5f7e228a1f0d35466e33 to your computer and use it in GitHub Desktop.
Camera feed to texture
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CamFeed : MonoBehaviour
{
private WebCamTexture webCamTexture;
void Start()
{
// Find the first available camera
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length > 0)
{
webCamTexture = new WebCamTexture(devices[0].name);
GetComponent<Renderer>().material.mainTexture = webCamTexture;
webCamTexture.Play();
}
else
{
Debug.LogError("No webcam found");
}
}
public WebCamTexture GetWebCamTexture()
{
return webCamTexture;
}
void OnDisable()
{
if (webCamTexture != null)
{
webCamTexture.Stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment