unity3d与web进行交互

一、unity调用web的js函数

在unity中写如下调用语句:

Application.ExternalCall( "SayHello", "The game says hello!" );

在html中定义函数如下

function SayHello(args){
    alert(args);
}

二、在web中用js调用unity对象函数

在unity中定义函数如下,并将脚本绑定到名为Interface的GameObject中

function createOutTask(){
    GameObject.Find("CreateOutTask").GetComponent(CreateOutTask).createOutTask();
}

在web中调用unity对象时,需特别注意使用的unity的版本

3.5及以前版本,调用如下:

GetUnity().SendMessage("Interface", "createOutTask", "")

4.0版本,调用如下:

var config = {
     1730, 
    height: 980,
    params: { enableDebugging:"0", disableContextMenu: true }
    
};
var u = new UnityObject2(config);

u.getUnity().SendMessage("Interface", "createOutTask", "");
对于4.0版本的调用,参照官方文档http://docs.unity3d.com/Documentation/Manual/UnityWebPlayerandbrowsercommunication.html
Note: keep in mind that if the function doesn't have any arguments, then an empty string ("") should be passed as an argument.
原文地址:https://www.cnblogs.com/huntdream/p/3031629.html