Version: 2020.2
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;

public class Example : MonoBehaviour { void Start() { /// Calls the function ApplyDamage with a value of 5 SendMessage("ApplyDamage", 5.0); }

// Every script attached to the game object // that has a ApplyDamage function will be called. void ApplyDamage(float damage) { print(damage); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961