当前位置:网站首页>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());
}
}
后边直接看官方文档里边的里边就可以了
边栏推荐
猜你喜欢
随机推荐
动态RDLC报表(三)
Li Yuanyuan: iMetaLab Suite metaproteomics data analysis and visualization (video + PPT)
Self-taught software testing, how far can I go out to find a job?
megacli磁盘阵列
leetcode/链表中环的入口节点
QoS - ROS2 principle 9 】 【 deadline, activity and life
李乐园:iMetaLab Suite宏蛋白质组学数据分析与可视化(视频+PPT)
Logic unauthorized and horizontal and vertical unauthorized payment tampering, verification code bypass, interface
试试使用 Vitest 进行组件测试,确实很香。
方舟单机/管理员特殊物品指令代码大全
Wallys/QCA 9880/802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz advanced edition
win10 uwp 活动磁贴
win10 uwp 设置启动窗口大小 获取窗口大小
自学软件测试,学到什么程度可以出去找工作啊?
太细了!阿里大佬耗时39天整理出一份Redis进阶笔记,满满的干货
Prometheus full installation
最新!2022版新员工基础安全知识教育培训PPT,企业拿去直接用
The most complete architect knowledge map in history
2022 全球 AI 模型周报
以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会

![[Pycharm easy to use function]](/img/f8/4c131516033286ba8bcb511d395462.png)







![[SUCTF 2019]CheckIn](/img/4a/cae4dbe47c3b9d0fb37fe337bb5c3f.png)