当前位置:网站首页>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); // 通过基类处理默认类型消息
}
}
}
}
}
边栏推荐
- 【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
- Brief analysis of tensorboard visual processing cases
- Flink SQL knows why (12): is it difficult to join streams? (top)
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- DQL basic query
- Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
- Flutter动态化 | Fair 2.5.0 新版本特性
- Logback 日志框架
- Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
- MySQL installation, uninstallation, initial password setting and general commands of Linux
猜你喜欢
[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈..."/>开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...

regular expression

Flutter动态化 | Fair 2.5.0 新版本特性

Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?

Several common optimization methods matlab principle and depth analysis

35道MySQL面试必问题图解,这样也太好理解了吧

这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看

Unity EmbeddedBrowser浏览器插件事件通讯

MySQL constraints

CVPR 2022 | 美团技术团队精选6篇优秀论文解读
随机推荐
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
父亲和篮球
Will Huawei be the next one to fall
【被动收入如何挣个一百万】
Smbms project
Brief analysis of tensorboard visual processing cases
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
服务器硬盘冷迁移后网卡无法启动问题
stm32和电机开发(从mcu到架构设计)
R语言gt包和gtExtras包优雅地、漂亮地显示表格数据:nflreadr包以及gtExtras包的gt_plt_winloss函数可视化多个分组的输赢值以及内联图(inline plot)
2022-02-13 plan for next week
CVPR 2022 | interpretation of 6 excellent papers selected by meituan technical team
MapReduce实现矩阵乘法–实现代码
User and group command exercises
道路建设问题
MyCms 自媒体商城 v3.4.1 发布,使用手册更新
Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
The difference between stratifiedkfold (classification) and kfold (regression)