Version: 2023.2
言語: 日本語
Configure a Web Canvas size
Web networking

Web browser access to device features

The Unity Web platform supports WebCam access. To allow a Web application to access the webcam on a device, the browser must request its user to provide access to the camera. Without the permission to access the camera, the browser returns incomplete or inaccurate information.

Note: Currently, the Web platform supports WebCam devices only.

ブラウザーのウェブカメラへのアクセス許可をリクエストするには、以下のように、Application.RequestUserAuthorization API を使用します。

using UnityEngine;
using UnityEngine.iOS;
using System.Collections;

// Get WebCam information from the browser
public class ExampleClass : MonoBehaviour
{
    private WebCamDevice[] devices;
    
    // Use this for initialization
    IEnumerator Start()
    {
        yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
        if (Application.HasUserAuthorization(UserAuthorization.WebCam))
        {
            Debug.Log("webcam found");
            devices = WebCamTexture.devices;
            for (int cameraIndex = 0; cameraIndex < devices.Length; ++cameraIndex)
            {
                Debug.Log("devices[cameraIndex].name: ");
                Debug.Log(devices[cameraIndex].name);
                Debug.Log("devices[cameraIndex].isFrontFacing");
                Debug.Log(devices[cameraIndex].isFrontFacing);
            }
        }
        else
        {
            Debug.Log("no webcams found");
        }
    }
}

ノート: Unity は、MediaDevices.getUserMedia() API を使用してデバイスへのアクセス許可をユーザーにリクエストすることを推奨します。この機能は、安全なコンテキスト (HTTPS) でのみ使用可能です。

Configure a Web Canvas size
Web networking