一、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. centos 下Qt安装 mysql驱动(亲测可行)

    (前半部分没有试过,因为我的有mysql驱动,实在抱歉) 1.预防万一,先安装一下mysql-devel(一定要装!). 不安装的话后面编译会出现找不到-lmysqlclient的问题. 2. 开始编 ...

  2. angular学习笔记(十四)-$watch(1)

    本篇主要介绍$watch的基本概念: $watch是所有控制器的$scope中内置的方法: $scope.$watch(watchObj,watchCallback,ifDeep) watchObj: ...

  3. 每日英语:Prosecutors Wrap Up Case Against Bo

    Prosecutors wrapped up their case against Bo Xilai on Sunday, sparring with the defiant former Commu ...

  4. Django---时间的时区问题

    在用django1.8版本做项目的时候遇到时间的存储与读取不一致的问题,网上找了很多帖子,但都没有讲明白.本文将在项目中遇到的问题及如何解决的尽可能详细的记录下来,当然本文参考了网上大量相关文章. 在 ...

  5. angularjs获取元素以及angular.element()用法

    addClass()-为每个匹配的元素添加指定的样式类名 after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点 append()-在每个匹配元素里面的末尾处插入参数内容 ...

  6. postgres入门介绍

    mysql被oricle公司收购以后,PG就成了比较流行的开源数据库的首选,而且heroku上面不支持mysql,但是却大力支持PG,所以说,不得不学学PG,并非迫不得已的样子,至少以后PG会比较流行 ...

  7. [WF4.0 实战] AutoResetEvent具体解释(线程独占訪问资源)

    由来: 在学习工作流的过程中,宿主程序中会出现这么一段代码: staticAutoResetEvent instanceUnloaded = new AutoResetEvent(false); 然后 ...

  8. 【Unity】UGUI控件大小适配父容器

    需求:需要把UGUI控件的尺寸调整到指定大小,如匹配至设计的分辨率.或者说想制定覆盖全屏的背景图片. 做法:将这个UGUI控件的RectTransform组件里的Anchor Presets设为预设的 ...

  9. DatagridView内容自动换行和换行符换行

    内容自动换行: 在 DataGridView 新增 Row 时,設定 DataGridView 的 DefaultCellStyle 屬性WrapMode=true: 换行符换行: 在 DataGri ...

  10. JavaScript 深入理解作用域链

    第一步. 定义后:每个已定义函数,都有一个内在属性[scope],其对应一个对象的列表,列表中的对象仅能内部访问. 例如:建立一个全局函数A,那么A的[Scope]内部属性中只包含一个全局对象(Glo ...