WebcamSource

Manages the webcam functionality.

public class WebcamSource : StreamSource

Inherits StreamSource class.

Constructors

Name
Description

WebcamSource()

Creates a new WebcamSource object.

Properties

Name
Type
Description

WebcamIndex

int

Read-Only. The webcam device index.

WebcamTexture

Read-Only. The WebcamTexture onto which the live video input is rendered.

Timestamp

DateTime

Read-Only. Overriden. The frame timestamp.

IsOpen

bool

Read-Only. If the camera is currently playing and its Width and Height are greater than 16 pixels each.

IsPlaying

bool

Read-Only. If the camera is currently playing.

Width

int

Read-Only. Overriden. The width of the WebcamTexture in pixels.

Height

int

Read-Only. Overriden. The height of the WebcamTexture in pixels.

Rotation

int

Read-Only. Overriden. The rotation of the camera.

Pixels

Read-Only. Overriden. The pixel color data for a mipmap level as Color32 structs.

Pointer

IntPtr

Read-Only. Overriden. A native pointer to the WebcamTexture resource.

Methods

Name
Return Type
Description

Open

void

Starts the camera if the app is granted access.

Close

void

Closes the camera.

Pause

void

Pauses the camera.

SwitchCamera(int)

void

Closes the open camera and opens the camera with the specified device index.

Example

The example shows how to manage webcam functionality with the WebcamSource class.

Create a WebcamSource

To create a WebcamSource, add a field for Unity to serialize.

[SerializeField] private WebcamSource _webcam;

Open webcam

Open the webcam to get the live feed.

_webcam.Open();

Get camera feed

To get the camera feed, first add an ImageView field for Unity to serialize. Then, if the webcam is open and has a new frame, draw the texture.

//Create an ImageView
[SerializeField] private ImageView _image;

// Check if the webcam is open.
if (_webcam.IsOpen)
{
    // Draw the texture for the specified camera.
    _image.Load(_webcam);
}

Close webcam

Close the webcam to stop the live feed.

_webcam.Close();

Last updated