public void SendMessage (string methodName);
public void SendMessage (string methodName, object value);
public void SendMessage (string methodName, object value, SendMessageOptions options);
public void SendMessage (string methodName, SendMessageOptions options);

参数

methodName要调用的方法的名称。
value该方法的可选参数。
options如果目标对象没有为消息实现该方法,是否应报错?

描述

调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。

通过具有零参数,该接收方法可选择忽略此参数。 如果选项设置为 SendMessageOptions.RequireReceiver,则在任何组件均未拾取此消息时输出错误。

注意,不会将消息发送到非活动对象(即,在 Editor 中或使用 GameObject.SetActive 函数已停用的对象)。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void ApplyDamage(float damage) { print(damage); } void Example() { SendMessage("ApplyDamage", 5.0F); } }