unity打包web平台之unity调用html的方法(unity2019.4.10+谷歌浏览器)
unity不能直接调用html的方法,要通过jslib文件调用
步骤:
1)在assets文件夹下新建Plugins文件夹
2)在Plugins文件夹下新建一个txt,改成“自己想要的名字.jslib”
3)在jslib文件中写入想要的方法
mergeInto(LibraryManager.library, {undefined
UnitySendReactString: function (score) {
unitySendReactString(Pointer_stringify(score));
console.log("success");
},
});
含义:html中的方法是PassInformation,unity中调用的方法是Unity_WebGL,
这个Unity_WebGL就像一个中介一样。第一行这个一定要有,实际
是mergeInto(LibraryManager.library,obj),这个obj也可以在外边写
unity往react传值 string类型数据,调用html方法
1.Plugins文件夹下创建 *.jslib文件
2.编写内部方法,**注意大小写 Unity 里的 unity**
3.unity 创建脚本
4.编辑.cs脚本调用代码,哪里需要往react传值,哪里调用 SendToReactString(str)
5.unity打包出来的webgl包中,编辑index.html,注意 unitySendReactString大小写
react 给unity发送消息
直接查找场景中的物体,调用SendMessage
方法.如上图,查找到ReactToUnity 物体,调用物体挂载的脚本中的SendToUnityString
方法, 第三个参数为 传入的值.如下图.
以下为转载参考
unity调用web
原文链接:https://blog.csdn.net/jiamian123456/article/details/122129470