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, 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.
WebCamTextureOn 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
Constructor | Description |
|---|---|
| WebCamTexture() | Create a WebCamTexture. |
| WebCamTexture(System.Int32,System.Int32) | Create a WebCamTexture. |
| WebCamTexture(System.Int32,System.Int32,System.Int32) | Create a WebCamTexture. |
| WebCamTexture(System.String) | Create a WebCamTexture. |
| WebCamTexture(System.String,System.Int32,System.Int32) | Create a WebCamTexture. |
| WebCamTexture(System.String,System.Int32,System.Int32,System.Int32) | Create a WebCamTexture. |
Properties
Property | Description |
|---|---|
| autoFocusPoint | This property allows you to set/get the auto focus point of the camera. This works only on Android and iOS devices. |
| deviceName | Set this to specify the name of the device to use. |
| didUpdateThisFrame | Did the video buffer update this frame? |
| isDepth | This property is true if the texture is based on depth data. |
| isPlaying | Returns if the camera is currently playing. |
| requestedFPS | Set the requested frame rate of the camera device (in frames per second). |
| requestedHeight | Set the requested height of the camera device. |
| requestedWidth | Set the requested width of the camera device. |
| videoRotationAngle | Returns an clockwise angle (in degrees), which can be used to rotate a polygon so camera contents are shown in correct orientation. |
| videoVerticallyMirrored | Returns if the texture image is vertically flipped. |
Static Properties
Property | Description |
|---|---|
| devices | Return a list of available devices. |
Methods
Method | Description |
|---|---|
| GetPixel | Gets the pixel color at coordinates ( |
| GetPixels | Gets the pixel color data for a mipmap level as Color structs. |
| GetPixels32 | Gets the pixel color data for a mipmap level as Color32 structs. |
| Pause | Pauses the camera. |
| Play | Starts the camera. |
| Stop | Stops the camera. |
Inheritance
Operators
Operator | Description |
|---|---|
| operator == | Compares two object references to see if they refer to the same object. |
| bool | Determines whether the object exists. |
| operator != | Compares if two objects refer to a different object. |