ウェブブラウザ入力処理
Unity Render Streaming はウェブブラウザからの入力をサポートしています。ウェブブラウザから Unity を操作することができます。
入力デバイス
ウェブブラウザの入力は以下のデバイスをサポートしています。同時に複数のデバイスを利用できます。
- マウス
- キーボード
- タッチ操作
- ゲームパッド
ウェブブラウザからバイト列を送信し、Unity 側で New Input System を利用しています。
データフォーマット
マウスイベント
| index | value | size |
|---|---|---|
| 0 | 0 | 1 |
| 1 | position x | 2 |
| 3 | position y | 2 |
| 5 | button | 1 |
キーボードイベント
| index | value | size |
|---|---|---|
| 0 | 1 | 1 |
| 1 | key code | 1 |
タッチイベント
タッチイベントのデータサイズは検出した指先の数に依存しています。バイト列の最初に含まれる length は検出した指先の数を表します。
| index | value | size |
|---|---|---|
| 0 | 2 | 1 |
| 1 | phase | 1 |
| 2 | length | 1 |
| 3 | position x | 2 |
| 5 | position y | 2 |
| 7 | force | 4 |
ゲームパッドイベント
ゲームパッドのイベントは4種類あります。それぞれデータフォーマットが異なります。
| イベント名 | value |
|---|---|
| button down | 0 |
| button up | 1 |
| button pressed | 2 |
| axis | 3 |
button down 、button up 、button pressed の場合:
| index | value | size |
|---|---|---|
| 0 | 5 | 1 |
| 1 | event type | 1 |
| 2 | button index | 1 |
| 3 | value | 8 |
axis の場合:
| index | value | size |
|---|---|---|
| 0 | 5 | 1 |
| 1 | event type | 1 |
| 2 | button index | 1 |
| 3 | axis x | 8 |
| 11 | axis y | 8 |
複数ユーザ
RemoteInputReceiver クラスは複数ユーザからの入力を制御します。RemoteInputReceiver.Create メソッドを呼び出すと RemoteInput インスタンスを作成します。RTCDataChannel で受け取ったメッセージを RemoteInput.ProcessInput メソッドに渡します。
// Create a RemoteInput instance
RemoteInput input = RemoteInputReceiver.Create();
channel.OnMessage = bytes => input.ProcessInput(bytes);
RemoteInput インスタンスから入力デバイスを取得して、ユーザ入力を処理します。以下はキーボード入力を処理する例です。
// Get the keyboard device, process on w key press
Keyboard keyboard = input.RemoteKeyboard;
if(keyboard.wKey.isPressed)
{
// ...
}
Unity UI を使用する
UnityUI を利用するには、EventSystem コンポーネントと同じオブジェクトに配置されている StandaloneInputModule で、Replace with InputSystemUIInputModule ボタンを押す必要があります。

こうすることでシーンに配置された UnityUI をブラウザから操作することができます。
注記
RunInBackground チェックボックスをオンに設定した場合でも、 Unity アプリケーションをバックグラウンドで実行すると、ブラウザから UnityUI は応答しません。この問題は InputSystem パッケージで修正される予定です。
バックグラウンド実行で UnityUI を利用する回避方法
UnityUI をバックグラウンドで利用するには、以下の方法を試してください。
パッケージマネージャで
com.unity.inputsystemパッケージを1.1.0-preview.2に更新します。Project Setting > Player > Other Settings に移動し、
Allow 'unsafe' codeを有効にします。シーン内にある
EventSystemコンポーネントを以下のコンポーネントに置き換えます。
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
public class CustomEventSystem : EventSystem
{
protected override void Awake()
{
base.Awake();
unsafe
{
InputSystem.onDeviceCommand += InputSystemOnDeviceCommand;
}
}
private static unsafe long? InputSystemOnDeviceCommand(InputDevice device, InputDeviceCommand* command)
{
if (command->type != QueryCanRunInBackground.Type)
{
// return null is skip this evaluation
return null;
}
((QueryCanRunInBackground*)command)->canRunInBackground = true;
return InputDeviceCommand.GenericSuccess;
}
protected override void OnApplicationFocus(bool hasFocus)
{
//Do not change focus flag on eventsystem
}
}
- 補足として、Unity エディタ上では、メニューから Window > Analysis > Input Debugger でウィンドウを開いて、
Lock Input to Game Viewオプションを有効にしてください。