当前位置:网站首页>Unity Render Streaming通过Js与Unity自定义通讯
Unity Render Streaming通过Js与Unity自定义通讯
2022-07-03 12:59:00 【foenix66】
Js通讯发送示例在WebApp\client\public\videoplayer;
对应C#接收端案例Unity Render Streaming插件的WebBrowserInput
JS发送部分
videoplayer/js/main.js按钮通讯示例
const elementBlueButton = document.createElement('button');
elementBlueButton.id = "blueButton";
elementBlueButton.innerHTML = "Light on";
playerDiv.appendChild(elementBlueButton);
elementBlueButton.addEventListener("click", function () {
sendClickEvent(videoPlayer, 1);
});
案例只能发送一个按钮Number ID
往上溯源,在js/register-events.js文件中的sendClickEvent函数
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);
}
核心代码
videoPlayer.sendMsg(data.buffer); // 这是一个二进制数组发送函数,理论上可以发送任意数据。
data.setUint8(0, InputEvent.ButtonClick); // 二进制数组的第一个字段是1个字节的类型标记
data.setInt16(1, elementId, true); // 第一个字节之后的数据由标签类型定义不同而解释不同,这里转为2字节的short类型,代表按钮id
查看案例已经定义的所有通讯类型
const InputEvent = {
Keyboard: 0,
Mouse: 1,
MouseWheel: 2,
Touch: 3,
ButtonClick: 4,
Gamepad: 5,// 案例定义了0-5标记,分别对应鼠标键盘游戏手柄,和按钮通讯
};
我们新增加一个发送字符串的通讯类型,定义标签为 6
function sendMessage(videoPlayer, message) {
function utf8FromStr(str) {
var strUtf8 = unescape(encodeURIComponent(str)); // UTF8编码文本到二进制
var arr = new Uint8Array(strUtf8.length);
for (var i = 0; i < strUtf8.length; ++i) {
arr[i] = strUtf8.charCodeAt(i);
}
return arr;
}
message = " " + message; // 预留空格作为第一个字节,存放类型标签6
var buf = utf8FromStr(message);
let data = new DataView(buf.buffer);
data.setUint8(0, 6); // 自定义标签6顶替掉第一个预留的空格字符
videoPlayer && videoPlayer.sendMsg(data.buffer); // 发送二进制数据串
}
function sendJson(videoPlayer, json) {
// 也可以发送自定义Json格式化文本数据
sendMessage(videoPlayer, JSON.stringify(json));
}
C#接收部分
Unity Streaming Render示例的WebBrowserInputChannelReceiver是接收webRtc数据通讯的通道(channel),继承自InputChannelReceiverBase类。WebBrowserInputChannelReceiver通过消息注册获取标签类型为4的ButtonClick事件
我们的目标是自定义字符串(JSON)通讯,默认基类不会处理自定义类型。
从派生一个新类,直接继承OnMessage接收二进制数据流函数,OnMessage收到的字节流就是JS中sendMsg发送的二进制原始数据
代码如下:
public class DBBrowserInputChannelReceiver : WebBrowserInputChannelReceiver
{
public GameObject[] targets;
protected override void OnMessage(byte[] bytes)
{
if (bytes.Length > 0)
{
int code = bytes[0]; // 第一个字节为通讯类型标记
if (code == 6) // 我们自定义的字符串通讯类型标记
{
string message = System.Text.UTF8Encoding.UTF8.GetString(bytes, 1, bytes.Length - 1); // 去掉数据流第一个标记字节,剩余字节通过UTF8编码转化回字符串,对应JS的UTF8编码函数unescape(encodeURIComponent(str))
foreach (GameObject go in targets)
{
go.SendMessage("OnNetMessage", message); // 将数据通过Unity的SendMessage发送给处理节点
}
if (code <= 5)
{
base.OnMessage(bytes); // 通过基类处理默认类型消息
}
}
}
}
}
边栏推荐
- 用户和组命令练习
- Resolved (error in viewing data information in machine learning) attributeerror: target_ names
- Spark实战1:单节点本地模式搭建Spark运行环境
- 物联网毕设 --(STM32f407连接云平台检测数据)
- 网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
- The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- Flutter动态化 | Fair 2.5.0 新版本特性
- Convolution emotion analysis task4
- JSON serialization case summary
猜你喜欢
Road construction issues
正则表达式
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
Flink SQL knows why (19): the transformation between table and datastream (with source code)
When updating mysql, the condition is a query
TensorBoard可视化处理案例简析
随机推荐
STM32 and motor development (from MCU to architecture design)
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
Open PHP error prompt under Ubuntu 14.04
Road construction issues
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
Logback 日志框架
106. How to improve the readability of SAP ui5 application routing URL
Task6: using transformer for emotion analysis
Mysql database basic operation - regular expression
MySQL functions and related cases and exercises
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
Setting up Oracle datagurd environment
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter IV exercises]
这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
[how to solve FAT32 when the computer is inserted into the U disk or the memory card display cannot be formatted]
已解决(机器学习中查看数据信息报错)AttributeError: target_names