找回密碼
 注冊帳號

掃一掃,訪問微社區

component.broadcastmessage 廣播消息

2015-1-5 03:17| 發布者: 楊炎| 查看: 2230| 評論: 0|原作者: 蠻牛|來自: unity3d腳本manual

摘要: component.broadcastmessage 廣播消息functionbroadcastmessage(methodname: string,parameter: object = null,options:sendmessageoptions=sendmessageoptions.requirereceiver) : voiddescription描述calls the me ...

component.broadcastmessage 廣播消息

functionbroadcastmessage(methodname: string,parameter: object = null,options:sendmessageoptions=sendmessageoptions.requirereceiver) : void

description描述

calls the method named methodname on everymonobehaviourin 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 tosendmessageoptions.requirereceiveran error is printed when the message is not picked up by any component.

接受消息的此方法(函數)可以通過設置沒有引用參數表來忽略傳過來的消息中的參數。如果選項option中設置成了sendmessageoptions.requirereceiver,那么當沒有任何腳本組件接受此消息時,一個相應的錯誤會彈出來

c#

javascript

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);

}

相關閱讀

文章點評
相關文章
女校游泳队彩金