unity不能直接调用html的方法,要通过jslib文件调用
步骤:1)在assets文件夹下新建Plugins文件夹
2)在Plugins文件夹下新建一个txt,改成“自己想要的名字.jslib”
3)在jslib文件中写入想要的方法
mergeInto(LibraryManager.library, {
Unity_WebGL:function(){
PassInformation();
},
});
含义:html中的方法是PassInformation,unity中调用的方法是Unity_WebGL,
这个Unity_WebGL就像一个中介一样。第一行这个一定要有,实际
是mergeInto(LibraryManager.library,obj),这个obj也可以在外边写,
多个函数要用逗号隔开
4)unity中的C#脚本
添加引用[DllImport("__Internal")]
注意:不管jslib文件叫什么名字,引用时都要写__Internal,jslib文件中写了几个
函数,这里就要引用几次,之后就像C#的普通函数一样使用
5)unity打包出来的index.html
要添加如下代码:
<script>
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/webgl.json", {
onProgress: UnityProgress
});
function PassInformation() //承接Unity
{
console.debug("Pass information");
}
function helloworld() {
console.debug("helloworld");
}
</script>
位置如图
经过如上步骤,打包出来进行操作即可在浏览器控制台中看到相应信息