Component.BroadcastMessage 广播消息

function BroadcastMessage (methodName : string, parameter : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver) : void

Description描述

Calls the method named methodName on every MonoBehaviour in this game object or any of its children.

在游戏物体每一个MonoBehaviour和它的全部子物体上调用名为methodName的方法。

通俗的解释:

BroadcastMessage朝物体和所有子物体发送消息。

对一个物体及其所有子物体,进行消息的广播,如果其中任何子物体上贴有脚本,而脚本中有相应的处理此消息的函数,则Invoke调用之。

The receiving method can choose to ignore parameter by having zero arguments. if options is set to SendMessageOptions.RequireReceiver an error is printed when the message is not picked up by any component.

接受消息的此方法(函数)可以通过设置没有引用参数表来忽略传过来的消息中的参数。如果选项Option中设置成了SendMessageOptions.RequireReceiver,那么当没有任何脚本组件接受此消息时,一个相应的错误会弹出来

using UnityEngine;

using System.Collections;



public class example : MonoBehaviour {

	void ApplyDamage(float damage) {

		print(damage);

	}

	public void Awake() {

		BroadcastMessage("ApplyDamage", 5.0F);

	}

}
// Calls the function ApplyDamage with a value of 5

//调用函数 ApplyDamage 值为5

BroadcastMessage ("ApplyDamage", 5.0);



// Every script attached to the game object and all its children

// that has a ApplyDamage function will be called.

//附加到游戏物体的每个脚本和全部的子物体,有一个ApplyDamage函数将被调用

function ApplyDamage (damage : float) {

	print (damage);

}
Page last updated: 2010-12-13