当前位置:网站首页>Unity render streaming communicates with unity through JS
Unity render streaming communicates with unity through JS
2022-07-03 13:33:00 【foenix66】
Unity Render Streaming adopt Js And Unity Custom communication
Js The example of communication sending is in WebApp\client\public\videoplayer;
Corresponding C# Receiver case Unity Render Streaming The plug-in WebBrowserInput
JS Send part
videoplayer/js/main.js Example of button communication
const elementBlueButton = document.createElement('button');
elementBlueButton.id = "blueButton";
elementBlueButton.innerHTML = "Light on";
playerDiv.appendChild(elementBlueButton);
elementBlueButton.addEventListener("click", function () {
sendClickEvent(videoPlayer, 1);
});
A case can only send one button Number ID
Go back to the source , stay js/register-events.js In the document sendClickEvent function
export function sendClickEvent(videoPlayer, elementId) {
let data = new DataView(new ArrayBuffer(3));
data.setUint8(0, InputEvent.ButtonClick);
data.setInt16(1, elementId, true);
videoPlayer && videoPlayer.sendMsg(data.buffer);
}
Core code
videoPlayer.sendMsg(data.buffer); // This is a binary array sending function , Theoretically, any data can be sent .
data.setUint8(0, InputEvent.ButtonClick); // The first field of the binary array is 1 Type mark of bytes
data.setInt16(1, elementId, true); // The data after the first byte is interpreted differently according to the label type definitions , Turn here to 2 Bytes of short type , For buttons id
View all communication types defined in the case
const InputEvent = {
Keyboard: 0,
Mouse: 1,
MouseWheel: 2,
Touch: 3,
ButtonClick: 4,
Gamepad: 5,// The case defines 0-5 Mark , Corresponding to mouse, keyboard, game console , Communicate with buttons
};
We add a new communication type for sending strings , Define the label as 6
function sendMessage(videoPlayer, message) {
function utf8FromStr(str) {
var strUtf8 = unescape(encodeURIComponent(str)); // UTF8 Encode text to binary
var arr = new Uint8Array(strUtf8.length);
for (var i = 0; i < strUtf8.length; ++i) {
arr[i] = strUtf8.charCodeAt(i);
}
return arr;
}
message = " " + message; // Leave a space as the first byte , Storage type label 6
var buf = utf8FromStr(message);
let data = new DataView(buf.buffer);
data.setUint8(0, 6); // Custom tag 6 Replace the first reserved space character
videoPlayer && videoPlayer.sendMsg(data.buffer); // Send binary data string
}
function sendJson(videoPlayer, json) {
// You can also send customized Json Format text data
sendMessage(videoPlayer, JSON.stringify(json));
}
C# Receiving part
Unity Streaming Render Example WebBrowserInputChannelReceiver It's reception webRtc Data communication channel (channel), Inherited from InputChannelReceiverBase class .WebBrowserInputChannelReceiver The tag type obtained through message registration is 4 Of ButtonClick event
Our goal is to customize the string (JSON) Communications , The default base class does not handle custom types .
Derive a new class from , Direct inheritance OnMessage Receive binary data stream function ,OnMessage The byte stream received is JS in sendMsg Binary raw data sent
The code is as follows :
public class DBBrowserInputChannelReceiver : WebBrowserInputChannelReceiver
{
public GameObject[] targets;
protected override void OnMessage(byte[] bytes)
{
if (bytes.Length > 0)
{
int code = bytes[0]; // The first byte is the communication type mark
if (code == 6) // Our custom string communication type tag
{
string message = System.Text.UTF8Encoding.UTF8.GetString(bytes, 1, bytes.Length - 1); // Remove the first marked byte of the data stream , The remaining bytes pass UTF8 Encoding is converted back to string , Corresponding JS Of UTF8 Coding function unescape(encodeURIComponent(str))
foreach (GameObject go in targets)
{
go.SendMessage("OnNetMessage", message); // Pass the data through Unity Of SendMessage Send to processing node
}
if (code <= 5)
{
base.OnMessage(bytes); // Process default type messages through base classes
}
}
}
}
}
边栏推荐
- Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
- MySQL constraints
- 服务器硬盘冷迁移后网卡无法启动问题
- php:&nbsp; The document cannot be displayed in Chinese
- untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
- Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
- 编程内功之编程语言众多的原因
- PostgreSQL installation
- The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
- Can newly graduated European college students get an offer from a major Internet company in the United States?
猜你喜欢
![[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion](/img/3b/28327bbf5eb19254f03500a41e2adb.jpg)
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion

Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL

SQL Injection (POST/Search)

Can newly graduated European college students get an offer from a major Internet company in the United States?

2022-02-14 incluxdb cluster write data writetoshard parsing

Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)

Logseq 评测:优点、缺点、评价、学习教程

Setting up remote links to MySQL on Linux

18W word Flink SQL God Road manual, born in the sky

8皇后问题
随机推荐
Server coding bug
JS 将伪数组转换成数组
The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
Road construction issues
MySQL functions and related cases and exercises
2022-02-13 plan for next week
Ubuntu 14.04 下开启PHP错误提示
[sort] bucket sort
The difference between stratifiedkfold (classification) and kfold (regression)
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
SQL Injection (POST/Search)
常见的几种最优化方法Matlab原理和深度分析
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
8 Queen question
KEIL5出现中文字体乱码的解决方法
Smbms project
February 14, 2022, incluxdb survey - mind map
R language uses the data function to obtain the sample datasets available in the current R environment: obtain all the sample datasets in the datasets package, obtain the datasets of all packages, and
Introduction to the implementation principle of rxjs observable filter operator