1.unity发布成WebGL后,打开后缀为.html
在.html中添加以下内容
<script>
var gameInstance = UnityLoader.instantiate("gameContainer", "Build/tt.json", {onProgress: UnityProgress});
</script>
<script>
function GetIDd(id) //注:函数名和unity中的保持一致
{
gameInstance.SendMessage("Main Camera","GetIDInfo",1);
//unity中挂载接收消息脚本的物体 函数名 传递的参数值
console.log("SendMessage");
}
</script>
2.unity脚本中
private void Start()
{
Application.ExternalCall("GetIDd", "QQQQQ");//注:函数名和js中的保持一致
}
public void GetIDInfo(int id)
{
if (id == 1)
Debug.Log("111111111111");
else
Debug.Log("000000000000000000");
//接受不同参数需要执行不同的操作
}