Unity API学习之消息机制理论与应用

目录

消息机制

示例1:同一物体中不同组件之间发送消息

示例2:父与子对象之间的消息发送(BroadcastMassage)

父对象向子对象发送消息

​编辑

子对象向父对象发送消息


消息机制

在Unity中,SendMessage 方法用于在游戏对象及其所有子对象上调用指定名称的方法。这种方法可以用于在不需要知道接收方的确切类型的情况下,向游戏对象发送消息。

基本语法如下:

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

methodName: 要调用的方法的名称。
value: 可选参数,要传递给方法的参数。
options: 可选参数,用于指定如何处理未找到接收方的情况。

SendMessage的传参情况分析:(以下的调用方法名以Mesage为准)

1. 在当前物体中组件名为Mesage

2. 当前物体中的其他脚本中有Mesage方法

示例1:同一物体中不同组件之间发送消息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_Message : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        gameObject.SendMessage("Mesage");
        gameObject.SendMessage("Mesages", 1);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    //无参发出消息
    public void Mesage()
    {
        print("消息发送!!!");
    }
    //有参发出消息
    public void Mesages(int value)
    {
        print("本组件接收到消息" + value);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Mesages : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        print("其他组件的消息发送给Mesages组件");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Mesage : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        print("其他组件的消息发送给Mesage组件");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class example : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Mesage()
    {
        print("消息发送!!!");
    }
    public void Mesages(int value)
    {
        print("example组件接收到消息" + value);
    }
}

注:发送消息若没有接收者则会报错,若不想要报错则需要输入如下代码

SendMessage("GetTestMsg",SendMessageOptions.DontRequireReceiver);

示例2:父与子对象之间的消息发送(BroadcastMassage)

父对象向子对象发送消息

​​using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_MessageParent : MonoBehaviour
{
    // Start is called before the first frame update
    //BroadcastMessage向下广播,包括子对象
    void Start()
    {
        BroadcastMessage("getMesage");
    }
    public void getMesage()
    {
        print("Parent");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_MessageChild : MonoBehaviour
{
    //向自身和上级发送消息,总当前的目录地位开始
    /*private void Start()
    {
        SendMessageUpwards("getMesage");
    }*/
    public void getMesage()
    {
        print("Child");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_MessageChild1 : MonoBehaviour
{
    //向自身和上级发送消息,总当前的目录地位开始
   
    public void getMesage()
    {
        print("Child1");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_MessageChild_child : MonoBehaviour
{
    //向自身和上级发送消息,总当前的目录地位开始
    public void getMesage()
    {
        print("Child_child");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_MessageChild_child1 : MonoBehaviour
{
    //向自身和上级发送消息,总当前的目录地位开始
    public void getMesage()
    {
        print("Child_child1");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_MessageChild_child1 : MonoBehaviour
{
    //向自身和上级发送消息,总当前的目录地位开始
    public void getMesage()
    {
        print("Child_child1");
    }
}

子对象向父对象发送消息

搜索范围:子对象以及上两级父对象

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_MessageChild : MonoBehaviour
{
    //向自身和上级发送消息,总当前的目录地位开始
    private void Start()
    {
        SendMessageUpwards("getMesage");
    }
    public void getMesage()
    {
        print("Child");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_MessageTest : MonoBehaviour
{
    //向自身和上级发送消息,总当前的目录地位开始
    
    public void getMesage()
    {
        print("Test");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_MessageParent : MonoBehaviour
{
    // Start is called before the first frame update
    //BroadcastMessage向下广播,包括子对象
    void Start()
    {
        /*BroadcastMessage("getMesage");*/
        
    }
    public void getMesage()
    {
        print("Parent");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO8_Message : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        gameObject.SendMessage("Mesage");
        gameObject.SendMessage("Mesages", 1);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    //无参发出消息
    public void Mesage()
    {
        print("消息发送!!!");
    }
    //有参发出消息
    public void Mesages(int value)
    {
        print("本组件接收到消息" + value);
    }
    public void getMesage()
    {
        print("Message");
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/697588.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Zabbix6.0自动发现Linux服务器并添加主机

文章目录 一、整体流程二、操作过程 一、整体流程 Zabbix自动发现主机功能是Zabbix监控系统的一个重要功能,它能够自动发现并添加新的主机到监控系统中,从而减少人为繁琐的操作! 步骤操作1️⃣ 第一步创建自动发现规则2️⃣ ​第二步创建自…

汇编语言作业(五)

目录 一、实验目的 二、实验内容 三、实验步骤以及结果 四、实验结果与分析 五、 实验总结 一、实验目的 1.熟悉掌握汇编语言的程序结构,能正确书写数据段、代码段等 2,利用debug功能,查看寄存器(CS,IP,AX,DS..)及数据段的…

Python集合的基本概念和使用方法

目录 集合(Set) 基本概念 基本特性 基本操作 集合运算 成员测试 高级操作 集合推导式 总结 集合(Set) Python集合(Set)是Python语言中一个非常实用且强大的数据结构,它用于存储多个不…

Python实现删除Word文档中带有“指定内容”的段落文本(7)

前言 本文是该专栏的第7篇,后面会持续分享Python办公自动化干货知识,记得关注。 在处理word文档内容的时候,有时候我们需要一个干净整洁的文本内容。比如说,如下图所示的情况: 在处理上述word文档内容的时候,我们希望将文本底部的“下载链接”以及“附件信息”两个段落,…

力扣199. 二叉树的右视图

给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 示例 1: 输入: [1,2,3,null,5,null,4] 输出: [1,3,4]示例 2: 输入: [1,null,3] 输出: [1,3]示例 3: 输入: [] 输出: [] /*** Def…

二叉树顺序结构——堆的结构与实现

二叉树顺序结构——堆的结构与实现 一、二叉树的顺序结构二、堆的概念及结构三、堆的实现堆向下调整算法堆的创建建堆时间复杂度堆的插入(堆向上调整算法)堆的删除堆的代码实现(使用VS2022的C语言)初始化、销毁构建、插入、删除返回堆顶元素、判空、返回有效元素个数 四、完整 …

【Python教程】4-字符串、列表、字典、元组与集合操作

在整理自己的笔记的时候发现了当年学习python时候整理的笔记,稍微整理一下,分享出来,方便记录和查看吧。个人觉得如果想简单了解一名语言或者技术,最简单的方式就是通过菜鸟教程去学习一下。今后会从python开始重新更新&#xff0…

7.高级纹理

前面的基础纹理包括法线纹理、渐变纹理和遮罩纹理等。这些纹理都属于低纬(一维或二维)纹理。 立方体纹理(Cubemap)实现环境映射 渲染纹理(Render Texture) 程序纹理(Procedure Texture&#…

java线程生命周期介绍

Java线程的生命周期包含以下几个状态: 1.新建(New):线程对象被创建,但是还没有调用start()方法。 1.运行(Runnable):线程正在运行或者是就绪状态,等待CPU时间片。 1.阻塞(Blocked):线程暂时停止执行&…

每日5题Day21 - LeetCode 101 - 105

每一步向前都是向自己的梦想更近一步&#xff0c;坚持不懈&#xff0c;勇往直前&#xff01; 第一题&#xff1a;101. 对称二叉树 - 力扣&#xff08;LeetCode&#xff09; class Solution {public boolean isSymmetric(TreeNode root) {if(root null){return true;}Stack<…

已解决Error || KeyError: ‘The truth value of a Series is ambiguous‘

已解决Error || KeyError: ‘The truth value of a Series is ambiguous’ &#x1f680; 原创作者&#xff1a; 猫头虎 作者微信号&#xff1a; Libin9iOak 作者公众号&#xff1a; 猫头虎技术团队 更新日期&#xff1a; 2024年6月6日 博主猫头虎的技术世界 &#x1f3…

第十篇——等价性:信息是如何压缩的?

目录 一、背景介绍二、思路&方案三、过程1.思维导图2.文章中经典的句子理解3.学习之后对于投资市场的理解4.通过这篇文章结合我知道的东西我能想到什么&#xff1f; 四、总结五、升华 一、背景介绍 基于信息是如何进行压缩的&#xff0c;引出来等价信息的概念&#xff1b;…

如何制定工程战略

本文介绍了领导者如何有效制定工程战略&#xff0c;包括理解战略核心、如何收集信息并制定可行的策略&#xff0c;以及如何利用行业最佳实践和技术债务管理来提升团队效能和产品质量。原文: How to Build Engineering Strategy 如果你了解过目标框架&#xff08;如 OKR&#xf…

某药监局后缀(第一部分)

声明 本文章中所有内容仅供学习交流使用&#xff0c;不用于其他任何目的&#xff0c;抓包内容、敏感网址、数据接口等均已做脱敏处理&#xff0c;严禁用于商业用途和非法用途&#xff0c;否则由此产生的一切后果均与作者无关&#xff01; 本文章未经许可禁止转载&#xff…

Unity编辑器扩展,快捷键的使用

代码部分 编辑器界面 使用方法&#xff1a; 使用方法和如图1一样&#xff0c;只需要在Menuitem的路径后面加上标识符号就行。 "#"对应的是shift "&"对应的是Alt "%"对应的是ctrl 比如我图中的是&#xff0c;%#s对应的是CtrlShifts&…

ReentrantLock底层原理

ReentrantLock public ReentrantLock() {sync new NonfairSync(); }public ReentrantLock(boolean fair) {sync fair ? new FairSync() : new NonfairSync(); }ReentrantLock 的默认实现是非公平锁&#xff0c;实际上 ReentrantLock 中的方法&#xff0c;几乎都让 sync 实现…

【C语言】11.字符函数和字符串函数

文章目录 1.字符分类函数2.字符转换函数3.strlen的使用和模拟实现4.strcpy的使用和模拟实现5.strcat的使用和模拟实现6.strcmp的使用和模拟实现7.strncpy函数的使用8.strncat函数的使用9.strncmp函数的使用10.strstr的使用和模拟实现11.strtok函数的使用12.strerror函数的使用 …

买CPU怕买到假货?这担忧属实有点……

这担忧属实有点多了 前段时间有朋友问小白&#xff1a;买CPU会买到假的吗&#xff1f; 啥&#xff1f;CPU还有假的&#xff1f; 这个问题让这几天闷闷不乐的小白笑得根本停不下来。 如果有哪个小伙伴能手搓个CPU出来&#xff0c;那这位小伙伴估计就不愁财富不到家了。 正文…

爬虫工具yt-dlp

yt-dlp是youtube-dlp的一个fork&#xff0c;youtube-dlp曾经也较为活跃&#xff0c;但后来被众多网站屏蔽&#xff0c;于是大家转而在其基础上开发yt-dlp。yt-dlp的github项目地址为&#xff1a;GitHub - yt-dlp/yt-dlp: A feature-rich command-line audio/video downloaderA …

入侵报警系统的智慧核心——ARMxy工控机深度应用

智能安防领域高清视频监控、人脸识别门禁系统以及入侵报警系统的智能化升级&#xff0c;正以前所未有的速度推动着行业的变革。在这场变革中&#xff0c;ARMxy工业计算机以其卓越的性能、高度的灵活性及强大的集成能力&#xff0c;成为了众多安防解决方案中的核心组件。 高清视…