Using Webcam or Camera in Unity3d

Using webcam or camera in unity3d.

Posted by dogukanhan on January 09, 2021 · 1 min read

Using Webcam or Camera in Unity3d

Create a cube in hierarchy(by right clicking) window. Make sure you can see the cube with camera when you start playing.

Create a c# script in project window and name it.

Add the code inside the script.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class CameraScript : MonoBehaviour
{
    WebCamTexture backCam;

    void Start()
    {
        if (backCam == null)
            backCam = new WebCamTexture();

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

        if (!backCam.isPlaying)
            backCam.Play();

    }

    void Update()
    {

    }
}

Drag and Drop Script to Cube object. when you press play you can see your webcam input is in the cube. (If the webcam is reversed rotate z 180 degrees)