Version: 2022.1
言語: 日本語
Cursor locking and full-screen mode in WebGL
WebGL アプリケーションのビルドと配信

WebGL - ブラウザーによるデバイス機能へのアクセス

Unity WebGL は WebCam へのアクセス をサポートしています。WebGL アプリケーションがデバイスのウェブカメラにアクセスできるようにするには、カメラへのアクセス許可をブラウザーからユーザーにリクエストする必要があります。カメラへのアクセス許可がないと、ブラウザーは、不完全あるいは不正確な情報を返します。

ノート: 現在、Unity WebGL は WebCam デバイスのみをサポートしています。

ブラウザーのウェブカメラへのアクセス許可をリクエストするには、以下のように、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) でのみ使用可能です。

Cursor locking and full-screen mode in WebGL
WebGL アプリケーションのビルドと配信