当前位置:网站首页>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());
}
}
后边直接看官方文档里边的里边就可以了
边栏推荐
- 神秘的程序员(20-30)
- 手写flexible.js的原理实现,我终于明白移动端多端适配
- How tall is the B+ tree of the MySQL index?
- win10 uwp 无法附加到CoreCLR
- SSM框架练手项目,高企必备的管理系统—CRM管理系统
- How to choose a good SaaS knowledge base tool?
- win10 uwp 让焦点在点击在页面空白处时回到textbox中
- [SUCTF 2019]CheckIn
- Metasploit——辅助模块(Auxiliary)
- Entry node of ring in leetcode/linked list
猜你喜欢

Discuz! Forum program installation + template configuration tutorial

我不写单元测试,被批了

最新!2022版新员工基础安全知识教育培训PPT,企业拿去直接用

【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点

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

Cortex-A7 MPCore Architecture

jmeter-录制脚本

The principle implementation of handwritten flexible.js, I finally understand the multi-terminal adaptation of the mobile terminal

Li Yuanyuan: iMetaLab Suite metaproteomics data analysis and visualization (video + PPT)

Discuz!论坛程序安装+模板配置教程
随机推荐
ARM 汇编基础
我不写单元测试,被批了
怎样选择一个好的SaaS知识库工具?
leetcode300.最长递增子序列(动态规划)
学长告诉我,大厂MySQL都是通过SSH连接的
OpenCV 轮廓 —— 轮廓匹配
虚拟补丁备忘单
win10 uwp 让焦点在点击在页面空白处时回到textbox中
win10 uwp 活动磁贴
win10 uwp 手动锁Bitlocker
虚拟修补:您需要知道的一切
jmeter - record script
动态RDLC报表(四)
BSN季度版本2022年8月31日迭代更新预告
Ark: Survival Evolved Open Server Port Mapping Tutorial
megacli磁盘阵列
以技术御风险,护航云原生 | 同创永益 X 博云举办产品联合发布会
HarmonyOS - 基于ArkUI (JS) 实现图片旋转验证
CPU状态信息us,sy,ni,id,wa,hi,si,st含义
Experience far more than Hue, this is the favorite SQL tool for technicians