ウェブブラウザ入力処理
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 インスタンスから入力デバイスを取得して、ユーザ入力を処理します。以下はキーボード入力を処理する例です。
// キーボードのデバイスを取得して、w キー押下時に処理
Keyboard keyboard = input.RemoteKeyboard;
if(keyboard.wKey.isPressed)
{
// ...
}