Created
July 4, 2024 18:06
-
-
Save TheCuttlefish/2301bc742e6b5f7e228a1f0d35466e33 to your computer and use it in GitHub Desktop.
Camera feed to texture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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