当前位置:网站首页>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());
}
}
后边直接看官方文档里边的里边就可以了
边栏推荐
猜你喜欢

The senior told me that the MySQL of the big factory is connected through SSH

Wallys/QCA 9880/802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz advanced edition
![[极客大挑战 2019]HardSQL](/img/99/74cd7c56b3915db371ebc7811f2987.png)
[极客大挑战 2019]HardSQL

50道Redis面试题,来看看你会多少?

偷偷盘点一下各大互联网公司的实习薪资

低代码平台和专业开发人员——完美搭档?

怎样选择一个好的SaaS知识库工具?

ARM 汇编基础

Engaged in software testing for a year, only basic functional testing, how to further study?

Metasploit——辅助模块(Auxiliary)
随机推荐
openEuler Xiong Wei: How do you view the SIG organization model in the open source community?
最新!2022版新员工基础安全知识教育培训PPT,企业拿去直接用
动态RDLC报表(四)
进行知识管理的好处有哪些?
ARM 汇编基础
动态RDLC报表(七)
EPIC是什么平台?
win10 uwp 获取按钮鼠标左键按下
毕昇编译器优化:Lazy Code Motion
谭中意:你知道 “开源女王” 是谁吗?
[极客大挑战 2019]HardSQL
International Soil Modeling Consortium-ISMC
重定向操作
论文解读:Deep-4MCW2V:基于序列的预测指标,以鉴定大肠杆菌中的N4-甲基环胞嘧啶位点
AI基础环境搭建和设置总文
苦日子快走开
.NET 6学习笔记(4)——解决VS2022中Nullable警告
Can't install the Vmware virtual machine on the Ark Kai server?
What are some good open source automation testing frameworks to recommend?
JMeter notes 6 | JMeter recording agent (configuration)