zoukankan      html  css  js  c++  java
  • SendMessage,BroadcastMessage

    三者比较

    image

    用于向某个GameObject发送一条信息,让它完成特定功能。
    其实本质是调用绑定GameObject里面的Script里面的函数,可以跨语言的,例如Javascript可以调用C#的函数,我已实验成功。
    ☆另外,如果GameObject本身有两个脚本,例如“move1”和“move2”,两个脚本内有同名函数例如“moveMe()”,会两个函数都执行一次。

    例子:

    广播消息

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

    void BroadcastMessage(string methodName, SendMessageOptions options);

    向上发送消息

    void SendMessageUpwards(string methodName, SendMessageOptions options);

    void SendMessageUpwards(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

    发送消息

    void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

    void SendMessage(string methodName, SendMessageOptions options);

    SendMessageOptions

    SendMessageOptions.RequireReceiver //如果没有找到相应函数,会报错(默认是这个状态)

    SendMessageOptions.DontRequireReceiver //即使没有找到相应函数,也不会报错,自动忽略

    在这个游戏物体上的所有MonoBehaviour上调用名称为methodName的方法。

    接收消息的方法可以通过不要参数的方法来选择忽略参数。当选项被设置为SendMessageOptions.RequireReceiver时,如果消息没有被任何一个组件处理,则会打印一个错误。

    示例代码

    using UnityEngine;
    using System.Collections;
    
    public class ExampleClass : MonoBehaviour {
        void ApplyDamage(float damage) {
            print(damage);
        }
        void Example() {
            BroadcastMessage("ApplyDamage", 5.0F);
            SendMessageUpwards("ApplyDamage", 5.0F);
            SendMessage("ApplyDamage", 5.0F);
    
        }
    }

    文档资料

    Component是一切附加到游戏物体的基类,参见:http://game.ceeger.com/Script/Component/Component.html

  • 相关阅读:
    python种的builtin函数详解第三篇 C
    python中的buildin函数详解(第一篇) C
    python中的formatter的详细用法 C
    python中的builtin函数详解第二篇 C
    python中函数的默认参数陷阱问题 C
    介绍C++11标准的变长参数模板
    猜数字
    父类和子类
    矩形的面积
    水果类
  • 原文地址:https://www.cnblogs.com/zhaoqingqing/p/3853186.html
Copyright © 2011-2022 走看看