一、GameObject发送消息的方法

GameObject类有三个方法可以实现发送消息,即SendMessage、BroadcastMessage和SendMessageUpwards。但是它们之间也是有区别的,如下:

假如cube0是cube1和cube2的父类一级,而cube1和cube2处于同一级。

  • SendMessage只能向自身发送消息,和自身同级的物体不会接收到消息。cube1不能接收到cube2发送的消息。
  • BroadcastMessage是向自身以及所有子类发送消息,和自身同级的物体不会收到消息。cube0、cube1和cube2可以接收cube0发送的消息。
  • SendMessageUpwards向自身和其所有父类发送消息。和自身同级的物体不能接收到消息。cube0可以接收到cube1和cube2发送的消息,但是cube1和cube2不能接收到彼此发送的消息。

二、SendMessage方法

函数原型:

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

参数methodName为接收信息的方法名字,参数value为信息的内容,参数options为信息接收的方式,默认为SendMessageOpions.RequireReciver。另外还有一种接收信息的方式为SendMessageOptions.DontRequireReciver。前者要求信息的接收方式必须有接受信息的方法,否则程序会报错,而后者没有要求。

三、BroadcastMessage方法

函数原型:

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

参数解释同SendMessage。

四、SendMessageUpwards方法

函数原型:

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

参数解释同SendMessage。

五、演示

创建3个GameObject对象cube0、cube1、cube2,cube0是cube1和cube2的父类一级,而cube1和cube2处于同一级。按顺序分别绑定脚本BroadcastMessage_ts.cs、SendMessage_ts.cs和SendMessageUpwards_ts.cs,脚本代码如下:

1、BroadcastMessage_ts脚本

using UnityEngine;
using System.Collections; public class BroadMessage_ts : MonoBehaviour { // Use this for initialization
void Start () { gameObject.BroadcastMessage("GetParentMessage",gameObject.name+":use BroadcastMessage send!");
} public void GetParentMessage(string str)
{
Debug.Log(gameObject.name + "收到父类发送的消息: " + str);
} public void GetChildrenMessage(string str)
{
Debug.Log(gameObject.name + "收到子类发送的消息: " + str);
} // Update is called once per frame
void Update () { }
}

2、SendMessage_ts脚本

using UnityEngine;
using System.Collections; public class SendMessage_ts : MonoBehaviour { // Use this for initialization
void Start () { gameObject.SendMessage("GetSelfMessage", gameObject.name + "use SendMessage send!");
} public void GetSelfMessage(string str)
{
Debug.Log(gameObject.name + "收到自身发送的消息: " + str);
} public void GetParentMessage(string str)
{
Debug.Log(gameObject.name + "收到父类发送的消息: " + str);
} public void GetChildrenMessage(string str)
{
Debug.Log(gameObject.name + "收到子类发送的消息: " + str);
} // Update is called once per frame
void Update () { }
}

3、SendMessageUpwards_ts脚本

using UnityEngine;
using System.Collections; public class SendMessageUpwards_ts : MonoBehaviour { // Use this for initialization
void Start()
{ gameObject.SendMessageUpwards("GetChildrenMessage", gameObject.name + "use SendMessageUpwards send!");
} public void GetChildrenMessage(string str)
{
Debug.Log(gameObject.name + "收到子类发送的消息: " + str);
} public void GetParentMessage(string str)
{
Debug.Log(gameObject.name + "收到父类发送的消息: " + str);
} // Update is called once per frame
void Update () { }
}

4、运行结果

如何使用GameObject类发送消息的更多相关文章

  1. 如何在MFC DLL中向C#类发送消息

    如何在MFC DLL中向C#类发送消息 一. 引言 由于Windows Message才是Windows平台的通用数据流通格式,故在跨语言传输数据时,Message是一个不错的选择,本文档将描述如何在 ...

  2. Android Handler类 发送消息-post()和postDelay(), Looper讲解

    https://blog.csdn.net/weixin_41101173/article/details/79701832 首先,post和postDelay都是Handler的方法,用以在子线程中 ...

  3. 【C#】给无窗口的进程发送消息

    注:本文适用.net2.0+的winform程序 一个winform程序,我希望它不能多开(但是如何防多开不是本文要讲的),那么在用户启动第二个实例的时候,作为第二个实例来说,大概可以有这么几种做法: ...

  4. 增加线程异步发送消息的方法一(Thread)

    @RequestMapping(value="order/updateOrder.do") public String updateOrder(HttpServletRequest ...

  5. 【转载】Delphi7从子线程中发送消息到主线程触发事件执行

    在对数据库的操作时,有时要用一个子线程来进行后台的数据操作.比如说数据备份,转档什么的.在主窗口还能同是进行其它操作.而有时后台每处理一个数据文件,要向主窗口发送消息,让主窗口实时显示处理进度在窗口上 ...

  6. java socket 一个服务器对应多个客户端,可以互相发送消息

    直接上代码,这是网上找的demo,然后自己根据需求做了一定的修改.代码可以直接运行 服务器端: package socket; import java.io.BufferedReader; impor ...

  7. [转] C#中发送消息给指定的窗口,以及接收消息

    原文C#中发送消息给指定的窗口,以及接收消息 public class Note { //声明 API 函数 [DllImport("User32.dll", EntryPoint ...

  8. Android 主线程和线程之间相互发送消息

    通过分析Activity源码,我们知道每个Activity都有一个Looper,所以主线程在接收Message是不需要调用Looper.prepare()和Looper.loop(),但是线程是不带L ...

  9. Winform 程序嵌入WPF程序 并发送消息

    废话不多说,先看解决方案目录 WindowsFormsDemo是主程序,WpfApp是嵌入的WPF程序,先看WPF程序,程序默认启动的页面是MainWindow.xaml,这里注释掉App.xaml里 ...

随机推荐

  1. Java的Statement、PreparedStatement、PreparedStatement + 批处理 的区别

    首先来说一下连接了数据库之后执行的sql语句:通常连接了数据库之后,我们就会获得statement 类的对象或者是他的子类的对象(PreparedStatement类),通过这个对象我们就可以利用它提 ...

  2. Install Ambari 2.2.0 from Public Repositories(Hadoop)

    Step1: Download the Ambari repository on the Ambari Server host For Redhat/CentOS/Oracle:   cd /etc/ ...

  3. Maven pom.xml 报 Missing artifact jdk.tools:jdk.tools:jar:1.7

    linux中用eclipse 新建maven project,pom.xml中报Missing artifact jdk.tools:jdk.tools:jar:1.7 解决方法: 在pom.xml中 ...

  4. jdbc连接hive0.14

    Jdbc连接hive0.14版本号 眼下官网最新版本号是hive0.13,要想下载最新的hive得去git上去clone一个. Hive0.14最大特点是支持直接插入. 如今做一个jdbc连接hive ...

  5. word2vec中文类似词计算和聚类的使用说明及c语言源代码

    word2vec相关基础知识.下载安装參考前文:word2vec词向量中文文本相似度计算 文件夹: word2vec使用说明及源代码介绍 1.下载地址 2.中文语料 3.參数介绍 4.计算相似词语 5 ...

  6. Let's call it a "return"

    Preface Long time no see. For some reason that I failed to keep updating this blog, which is really ...

  7. [学习笔记]Spring依赖注入

    依赖: 典型的企业应用程序不可能由单个对象(在spring中,也可称之bean)组成,再简单的应用也是由几个对象相互配合工作的,这一章主要介绍bean的定义以及bean之间的相互协作. 依赖注入: s ...

  8. Oracle PLSQL Demo - 03.流程判断[IF ELEIF ELSE]

    declare v_job ) := 'Programmer'; v_sal number; begin if v_job = 'Programmer' then v_sal :; elsif v_j ...

  9. JavaHbase连接代码示例

    package com.rokid.hbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; im ...

  10. Mysql取分组中前N条记录

    表结构如下:CREATE TABLE `dwb_rmirror_req_d` ( `thedate` varchar(10) NOT NULL DEFAULT '', `node` varchar(1 ...