当前位置:网站首页>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
}
}
}
}
}
边栏推荐
- Libuv库 - 设计概述(中文版)
- 人身变声器的原理
- Detailed explanation of multithreading
- Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
- Resolved (error in viewing data information in machine learning) attributeerror: target_ names
- Spark实战1:单节点本地模式搭建Spark运行环境
- 常见的几种最优化方法Matlab原理和深度分析
- 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
- [today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
- Smbms project
猜你喜欢

Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators

Resource Cost Optimization Practice of R & D team

PowerPoint tutorial, how to save a presentation as a video in PowerPoint?

MySQL_ JDBC

【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
![[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]](/img/95/09552d33d2a834af4d304129714775.png)
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]

PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?

This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version

Flink SQL knows why (13): is it difficult to join streams? (next)

Several common optimization methods matlab principle and depth analysis
随机推荐
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
File uploading and email sending
Multi table query of MySQL - multi table relationship and related exercises
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
Logback log framework
IBEM 数学公式检测数据集
Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Mysql database basic operation - regular expression
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
Father and basketball
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
JSP and filter
Flutter动态化 | Fair 2.5.0 新版本特性
常见的几种最优化方法Matlab原理和深度分析
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)