Application.ExternalCall 外部调用

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

Description描述

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.

这个函数调用时不会被阻塞,即ExternalCall立即返回的功能而不必等待被完成。

The number of passed arguments can be varying:

传递的参数数量是可变的。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public void Awake() {
		Application.ExternalCall("MyFunction1");
		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
//调用网页上的MyFunction2并使用字符串参数。
Application.ExternalCall ("MyFunction2", "Hello from Unity!");

// Calls MyFunction3 in web page with several arguments of different types
//调用网页上的MyFunction3并使用几个不同类型的参数。
Application.ExternalCall ("MyFunction3", "one", 2, 3.0);

The functions to be called are just declared in the HTML page using standard syntax, for example:
被调用的在HTML中的函数只需要使用标准的语法即可,例如:

<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 );
	}
-->
</script>

参见: Application.ExternalEval.

Page last updated: 2010-12-15