当前位置:网站首页>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
}
}
}
}
}
边栏推荐
- CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
- Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
- PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
- TensorBoard可视化处理案例简析
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- DQL basic query
- Mycms we media mall v3.4.1 release, user manual update
- Unity Render Streaming通过Js与Unity自定义通讯
- Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?
- The network card fails to start after the cold migration of the server hard disk
猜你喜欢

February 14, 2022, incluxdb survey - mind map

MyCms 自媒体商城 v3.4.1 发布,使用手册更新

Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication
![[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

Several common optimization methods matlab principle and depth analysis

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

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

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

SQL Injection (POST/Search)

rxjs Observable filter Operator 的实现原理介绍
随机推荐
Task5: multi type emotion analysis
untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
Libuv库 - 设计概述(中文版)
Error running 'application' in idea running: the solution of command line is too long
File uploading and email sending
常见的几种最优化方法Matlab原理和深度分析
Resource Cost Optimization Practice of R & D team
Logback log framework
Universal dividend source code, supports the dividend of any B on the BSC
静态链表(数组的下标代替指针)
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
Red hat satellite 6: better management of servers and clouds
Setting up Oracle datagurd environment
MySQL installation, uninstallation, initial password setting and general commands of Linux
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
February 14, 2022, incluxdb survey - mind map
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则