一、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. javascript ES6模块化

    一.将模块导出去 /* *将模块导出去 *a.js文件 */ const a=12; export default a; /* *将模块导出去 *b.js文件 */ const a=5; export ...

  2. [svc]lnmp一键安装脚本(含有np与mysql分离)

    基于lanny一键安装包:(含lnmp所需软件及配置文件) 安装nginx: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliy ...

  3. 【精】iOS 文件操作:沙盒(SandBox)、文件操作(FileManager)、程序包(NSBundle)

    1.沙盒机制介绍 iOS 中的沙盒机制(SandBox)是一种安全体系. 每一个 iOS 应用程序都有一个单独的文件系统(存储空间).并且仅仅能在相应的文件系统中进行操作,此区域被称为沙盒. 全部的非 ...

  4. SqlServer 如何知道是否发生了索引碎片

    --如何知道是否发生了索引碎片 SELECT object_name(dt.object_id) Tablename,si.name IndexName,dt.avg_fragmentation_in ...

  5. ny269 VF

    VF 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Vasya is the beginning mathematician. He decided to make ...

  6. 理解 Linux 配置文件

    介绍 每个 Linux 程序都是一个可执行文件,它含有操作码列表,CPU 将执行这些操作码来完成特定的操作.例如,ls 命令是由 /bin/ls 文件提供的,该文件含有机器指令的列表,在屏幕上显示当前 ...

  7. java 多线程7: (suspend方法与resume方法) 挂起与恢复

    suspend方法与resume 是 实例方法,已废弃 缺点一:会造成独占,从而造成死锁 缺点二:会造成数据不同步,不能保证数据原子性

  8. SharePoint自动化系列——通过PowerShell创建SharePoint Lists

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 代码如下(保存到本地ps1文件中,右键run with PowerShell即可): Add-PS ...

  9. Makefile学习之路——3

    特殊变量: 在Makefile中,有两个变量特殊变量会经常用到:MAKE和MAKECMDGOALS.MAKE变量表示的是当前处理Makefile的命令名是什么.当需要在Makefile中运行另一个Ma ...

  10. JAVA-JSP表达式

    相关资料: <21天学通Java Web开发> 结果总结: 1.<%= %>JSP表达式中的代码会首先执行,然后转换成字符串显示到网页上.2.JSP表达式标签不必也不能使用分号 ...