Application.ExternalCall 外部调用

static function ExternalCall (functionName : string, params args : object[]) : void


Calls a function in the containing web page (Web Player only).

调用一个包含在网页中的函数(只用于Web Player)。

This will call JavaScript function functionName in the web page that contains the web player, passing given arguments to it. Supported argument types are the primitive types (string, int, float, char) and arrays of them. Any other objects are converted to string (using ToString method) and passed as strings.

调用包含在网页中名为functionNameJavaScript函数,并传递给定的参数。支持原始的数据类型(string, int, float, char)和这些类型的数字。如何其他的对象被转化为字符串(使用ToString方法)并作为字符串传递。

The function is called non-blocking, i.e. ExternalCall immediately returns without waiting for the function that was called to complete.


The number of passed arguments can be varying:


using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public void Awake() {
		Application.ExternalCall("MyFunction2", "Hello from Unity!");
		Application.ExternalCall("MyFunction3", "one", 2, 3.0F);
// Calls MyFunction1 in web page with no arguments
// 调用网页上的MyFunction1并不使用参数。
Application.ExternalCall ("MyFunction1");

// Calls MyFunction2 in web page with a string
Application.ExternalCall ("MyFunction2", "Hello from Unity!");

// Calls MyFunction3 in web page with several arguments of different types
Application.ExternalCall ("MyFunction3", "one", 2, 3.0);

The functions to be called are just declared in the HTML page using standard syntax, for example:

<script language="JavaScript" type="text/javascript">
// Using the above call from Unity, this will   receive
// 使用来自Unity的调用,这将接受
// "Hello from Unity!" as the argument.
// "Hello from Unity!" 做为参数
function MyFunction2( arg )
		alert( arg );

参见: Application.ExternalEval.

Page last updated: 2010-12-15