Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


WebCamTexture

WebCam Textures are textures onto which the live video input is rendered.
Read time 8 minutesLast updated 3 days ago

Definition

  • Type: Class
  • Namespace: UnityEngine
  • Assembly: UnityEngine.AudioModule
  • Inherits from: Texture
public sealed class WebCamTexture : Texture

Remarks

On Android, iOS, and WebGL platforms,
WebCamTexture
requires the camera permission. On Android, you can request it at runtime using the Permission API. For more information, refer to Request runtime permissions documentation.
On iOS and WebGL, you can request camera permission at runtime using Application.RequestUserAuthorization.
Note: On Android and iOS platforms, Unity doesn't support multiple WebCamTextures simultaneously.
The following code example demonstrates how to request the user for camera permission on iOS, Android, and WebGL platforms.

Examples

using UnityEngine;using System;using System.Collections;using UnityEngine;#if UNITY_ANDROIDusing UnityEngine.Android;#endifpublic class WebCam : MonoBehaviour{#if UNITY_IOS || UNITY_WEBGL private bool CheckPermissionAndRaiseCallbackIfGranted(UserAuthorization authenticationType, Action authenticationGrantedAction) { if (Application.HasUserAuthorization(authenticationType)) { if (authenticationGrantedAction != null) authenticationGrantedAction(); return true; } return false; } private IEnumerator AskForPermissionIfRequired(UserAuthorization authenticationType, Action authenticationGrantedAction) { if (!CheckPermissionAndRaiseCallbackIfGranted(authenticationType, authenticationGrantedAction)) { yield return Application.RequestUserAuthorization(authenticationType); if (!CheckPermissionAndRaiseCallbackIfGranted(authenticationType, authenticationGrantedAction)) Debug.LogWarning($"Permission {authenticationType} Denied"); } }#elif UNITY_ANDROID private void PermissionCallbacksPermissionGranted(string permissionName) { StartCoroutine(DelayedCameraInitialization()); } private IEnumerator DelayedCameraInitialization() { yield return null; InitializeCamera(); } private void PermissionCallbacksPermissionDenied(string permissionName) { Debug.LogWarning($"Permission {permissionName} Denied"); } private void AskCameraPermission() { var callbacks = new PermissionCallbacks(); callbacks.PermissionDenied += PermissionCallbacksPermissionDenied; callbacks.PermissionGranted += PermissionCallbacksPermissionGranted; Permission.RequestUserPermission(Permission.Camera, callbacks); }#endif void Start() {#if UNITY_IOS || UNITY_WEBGL StartCoroutine(AskForPermissionIfRequired(UserAuthorization.WebCam, () => { InitializeCamera(); })); return;#elif UNITY_ANDROID if (!Permission.HasUserAuthorizedPermission(Permission.Camera)) { AskCameraPermission(); return; }#endif InitializeCamera(); } private void InitializeCamera() { WebCamTexture webcamTexture = new WebCamTexture(); Renderer renderer = GetComponent<Renderer>(); renderer.material.mainTexture = webcamTexture; webcamTexture.Play(); }}

Constructors

Properties

Property

Description

autoFocusPointThis property allows you to set/get the auto focus point of the camera. This works only on Android and iOS devices.
deviceNameSet this to specify the name of the device to use.
didUpdateThisFrameDid the video buffer update this frame?
isDepthThis property is true if the texture is based on depth data.
isPlayingReturns if the camera is currently playing.
requestedFPSSet the requested frame rate of the camera device (in frames per second).
requestedHeightSet the requested height of the camera device.
requestedWidthSet the requested width of the camera device.
videoRotationAngleReturns an clockwise angle (in degrees), which can be used to rotate a polygon so camera contents are shown in correct orientation.
videoVerticallyMirroredReturns if the texture image is vertically flipped.

Static Properties

Property

Description

devicesReturn a list of available devices.

Methods

Method

Description

GetPixelGets the pixel color at coordinates (
x
,
y
).
GetPixelsGets the pixel color data for a mipmap level as Color structs.
GetPixels32Gets the pixel color data for a mipmap level as Color32 structs.
PausePauses the camera.
PlayStarts the camera.
StopStops the camera.

Inheritance

Inherited Members

Operators

Operator

Description

operator ==Compares two object references to see if they refer to the same object.
boolDetermines whether the object exists.
operator !=Compares if two objects refer to a different object.