当前位置:网站首页>Unity Webgl与JS相互交互 Unity 2021.2之后的版本
Unity Webgl与JS相互交互 Unity 2021.2之后的版本
2022-08-09 17:13:00 【charlsdm】
Interaction with browser scripting
When building content for the web, you might need to communicate with other elements on your web page. Or you might want to implement functionality using Web APIs which Unity doesn’t currently expose by default. In both cases, you need to directly interface with the browser’s JavaScript engine. Unity WebGL
provides different methods to do this.
Calling JavaScript functions from Unity scripts
The recommended way of using browser JavaScript in your project is to add your JavaScript sources to your project, and then call those functions directly from your script code. To do so, place files with JavaScript code using the .jslib extension under a “Plugins” subfolder in your Assets folder. The plugin file needs to have a syntax like this:
mergeInto(LibraryManager.library, {
Hello: function () {
window.alert(“Hello, world!”);
},
HelloString: function (str) {
window.alert(UTF8ToString(str));
},
PrintFloatArray: function (array, size) {
for(var i = 0; i < size; i++)
console.log(HEAPF32[(array >> 2) + i]);
},
AddNumbers: function (x, y) {
return x + y;
},
StringReturnValueFunction: function () {
var returnStr = “bla”;
var bufferSize = lengthBytesUTF8(returnStr) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(returnStr, buffer, bufferSize);
return buffer;
},
BindWebGLTexture: function (texture) {
GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
},
});
Then you can call these functions from your C# scripts
like this:
using UnityEngine;
using System.Runtime.InteropServices;
public class NewBehaviourScript : MonoBehaviour {
[DllImport("__Internal")]
private static extern void Hello();
[DllImport("__Internal")]
private static extern void HelloString(string str);
[DllImport("__Internal")]
private static extern void PrintFloatArray(float[] array, int size);
[DllImport("__Internal")]
private static extern int AddNumbers(int x, int y);
[DllImport("__Internal")]
private static extern string StringReturnValueFunction();
[DllImport("__Internal")]
private static extern void BindWebGLTexture(int texture);
void Start() {
Hello();
HelloString("This is a string.");
float[] myArray = new float[10];
PrintFloatArray(myArray, myArray.Length);
int result = AddNumbers(5, 7);
Debug.Log(result);
Debug.Log(StringReturnValueFunction());
var texture = new Texture2D(0, 0, TextureFormat.ARGB32, false);
BindWebGLTexture(texture.GetNativeTexturePtr());
}
}
后边直接看官方文档里边的里边就可以了
边栏推荐
- win10 uwp 改变鼠标
- 方舟开服务器Vmware虚拟机安装不上?
- Ark: Survival Evolved Open Server Port Mapping Tutorial
- win10 uwp 简单MasterDetail
- How tall is the B+ tree of the MySQL index?
- approach和method的区别
- The senior told me that the MySQL of the big factory is connected through SSH
- AlphaControls 控件 TsPanel TsGroupBox 块与组的结合
- .NET静态代码织入——肉夹馍(Rougamo) 发布1.1.0
- The strongest distributed lock tool: Redisson
猜你喜欢
Detailed explanation of JVM memory model and structure (five model diagrams)
Li Yuanyuan: iMetaLab Suite metaproteomics data analysis and visualization (video + PPT)
crm系统哪家好?好用的crm管理系统推荐
jmeter - record script
JMeter笔记6 | JMeter录制(配置代理)
李乐园:iMetaLab Suite宏蛋白质组学数据分析与可视化(视频+PPT)
学长告诉我,大厂MySQL都是通过SSH连接的
从事软件测试一年,只会基础的功能测试,怎么进一步学习?
偷偷盘点一下各大互联网公司的实习薪资
低代码平台和专业开发人员——完美搭档?
随机推荐
MySQL索引的B+树到底有多高?
BSN季度版本2022年8月31日迭代更新预告
win10 uwp 获取指定的文件 AQS
GoFrame缓冲输出到客户端Flush()
win10 uwp 简单MasterDetail
虚拟修补:您需要知道的一切
动手学深度学习_风格迁移
Jenkins使用pipeline部署服务到远程服务器
动态RDLC报表(六)
Wallys/QCA 9880/802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz advanced edition
国际土壤模型协会 International Soil Modeling Consortium-ISMC
The difference between approach and method
试试使用 Vitest 进行组件测试,确实很香。
重定向操作
秋招面试大厂总被刷下来,你这样做保准你事半功倍!
动手学深度学习_全卷积网络 FCN
mysql如何查看所有复合主键的表名?
腾讯云2022年8月积分商城规则更新
动态RDLC报表(三)
approach和method的区别