Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

enabling webcam

Discussion in 'AR/VR (XR) Discussion' started by giancamati, Jan 31, 2012.

  1. giancamati

    giancamati

    Joined:
    Aug 4, 2010
    Posts:
    518
    Hi guys,

    not sure which group is more suitable for this, but I was wondering if you could give me directions to how I can possibly activate my webcam connected to the PC.

    any help?

    Thank, you.
    GC.
     
  2. LuciousCain

    LuciousCain

    Joined:
    Oct 16, 2022
    Posts:
    2
    I was wondering the same thing. i found this code. but i can't figure out how to make the usb camera actually show up in the raw image area. or even my own image in the raw image window in the lower left corner.
    and if you go here https://learn.microsoft.com/en-us/windows/mixed-reality/develop/unity/locatable-camera-in-unity
    which is Microsoft stating :
    The "WebCam" capability must be declared for an app to use the camera.

    1. In the Unity Editor, go to the player settings by navigating to the "Edit > Project Settings > Player" page
    2. Select the "Windows Store" tab
    3. In the "Publishing Settings > Capabilities" section, check the WebCam and Microphone capabilities
    there is no capabilities for windows or anywhere you can check to turn on you live webcam.



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

    public class WebcamViewer : MonoBehaviour
    {
    [Tooltip("All the found cameras, raw")]
    public WebCamDevice[] foundCams;
    [Tooltip("Lazy list for easier camera access")]
    public List<Cam> cameraList;

    private Cam selectedCam;
    /// <summary>
    /// Currently selected Camera Cam
    /// </summary>
    public Cam SelectedCamera
    {
    get
    {
    return selectedCam;
    }
    set
    {
    selectedCam = value;
    SelectedNewCam();
    }
    }

    [Tooltip("A single WebCamTexture to always use/reference")]
    public WebCamTexture webcamTexture;
    [Tooltip("RawImage for displaying final image/texture")]
    public RawImage camDisplay;

    /// <summary>
    /// Where webcam will be dumped and a copy kept for references
    /// </summary>
    private Color32[] uneditedImage;
    /// <summary>
    /// Active image to be edited
    /// </summary>
    private Color32[] editedImage;

    /// <summary>
    /// Single texture to reuse
    /// </summary>
    public Texture2D displayImg;

    void Start()
    {
    //Instantiate and setup
    cameraList = new List<Cam>();
    foundCams = WebCamTexture.devices;

    //Get RawImage from self
    camDisplay = gameObject.GetComponent<RawImage>();

    //Setup webcam capture
    webcamTexture = new WebCamTexture();

    //Put all the cameras into a more nice list with public view
    foreach (WebCamDevice webcam in foundCams)
    {
    cameraList.Add(new Cam(webcam));
    }

    //If list has more than 1 ior more camera, then put the first in as selected camera
    if (cameraList.Count > 0)
    {
    //Set camera variable
    SelectedCamera = cameraList[0];
    //Update for new camera being selected
    SelectedNewCam();

    //Set base editedImage
    editedImage = uneditedImage;
    //Reverse the complete image
    System.Array.Reverse(editedImage);
    //Update texture with new image
    displayImg.SetPixels32(editedImage);
    camDisplay.texture = displayImg;
    }
    }


    /// <summary>
    /// If the selected camera has changed, reset the texture to show other camera
    /// </summary>
    private void SelectedNewCam()
    {
    //Set the new web camera
    webcamTexture.name = SelectedCamera.name;

    //Update for new webcam resolution, just in case
    uneditedImage = new Color32[webcamTexture.width * webcamTexture.height];
    editedImage = new Color32[webcamTexture.width * webcamTexture.height];
    displayImg = new Texture2D(webcamTexture.width, webcamTexture.height);

    //Update the texture display
    camDisplay.texture = displayImg;

    //Run the webcam
    webcamTexture.Play();
    }
    }

    [System.Serializable]
    public class Cam
    {
    public string name;
    public WebCamDevice camera;

    public Cam(WebCamDevice newCamera)
    {
    name = newCamera.name;
    camera = newCamera;
    }
    }
     
  3. LuciousCain

    LuciousCain

    Joined:
    Oct 16, 2022
    Posts:
    2
    i figured it out....use this script( the word webCam is what made my webcam work if it doesn't work, play with that word) also couldn't figure out UI Raw Image..i spent forever thinking it was in that..no just use an object and then press play.. hope this works for anyone who reads it..

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;


    public class CameraScript : MonoBehaviour

    {

    static WebCamTexture usbCam;

    void Start()

    {

    if (usbCam == null)

    usbCam = new WebCamTexture();

    GetComponent<Renderer>().material.mainTexture = usbCam;


    if (!usbCam.isPlaying)

    usbCam.Play();

    }



    // Update is called once per frame

    void Update()

    {



    }

    }