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

news2024/10/9 0:51:22

目录

消息机制

示例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.coloradmin.cn/o/1807889.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【云岚到家】-day02-2-客户管理-认证授权

【云岚到家】-day02-2-客户管理-认证授权 第二章 客户管理1 认证模块1.1 需求分析1.2 小程序认证1.2.1 测试小程序认证1.2.1.1 参考官方流程1.2.1.2 申请小程序账号1.2.1.3 创建jzo2o-customer1.2.1.4 部署前端1.2.1.5 编译运行1.2.1.6 真机调试 2 阅读代码2.1 小程序认证流程2…

虚拟机ping不通主机,但是主机可以ping通虚拟机

我在Windows10系统安装了虚拟机,设置的主机与虚拟机的连接方式是桥接,安装好后,发现虚拟机ping不通主机,但是主机可以ping通虚拟机。 我的操作是:关闭防火墙,发现虚拟机可以ping通主机了。说明是Windows10…

设计高并发秒杀系统:保障稳定性与数据一致性

✨✨谢谢大家捧场,祝屏幕前的小伙伴们每天都有好运相伴左右,一定要天天开心哦!✨✨ 🎈🎈作者主页: 喔的嘛呀🎈🎈 目录 引言 一. 系统架构设计 1. 系统架构图 二、 系统流程 三…

AI绘画工具Ideogram测评:和Midjourney不分伯仲的AI图像工具之一

Ideogram 是一款令人印象深刻的人工智能图像工具,但尽管它于去年 8 月推出并具有不可思议的文本渲染能力,但它并没有引起其他一些更引人注目的 GenAI 服务的关注。 随着该公司推出其生成式人工智能模型 1.0 版本,这种情况即将发生改变&#…

详解 Flink 的容错机制

一、检查点 Checkpoint 1. 介绍 有状态流应用中的检查点(checkpoint),其实就是所有任务的状态在某个时间点的一个快照(一份拷贝),这个时间点应该是所有任务都恰好处理完一个相同的输入数据的时刻。在一个流…

帕友的小贴士,锻炼

帕金森病作为一种慢性神经系统疾病,对患者的生活质量产生了深远的影响。虽然医学界对于帕金森病的治疗仍在不断探索,但合理的锻炼已经被证实是改善患者症状、提高生活质量的有效途径之一。本文旨在为帕金森病患者推荐一些适合的锻炼方法,帮助…

2024 年最佳 iPhone 数据恢复软件

最好的 iPhone 数据恢复软件是什么? 说到 iPhone 数据恢复,拥有合适的软件对于恢复丢失或删除的文件至关重要,无论是照片、视频、消息、联系人还是其他重要数据。那么,最好的 iPhone 数据恢复软件是什么?有几个因素有…

使用C++结合OpenCV进行图像处理与分类

⭐️我叫忆_恒心,一名喜欢书写博客的在读研究生👨‍🎓。 如果觉得本文能帮到您,麻烦点个赞👍呗! 近期会不断在专栏里进行更新讲解博客~~~ 有什么问题的小伙伴 欢迎留言提问欧,喜欢的小伙伴给个三…

力扣hot100: 48. 旋转图像

LeetCode:48. 旋转图像 受到力扣hot100:54. 螺旋矩阵的启发,我们可以对旋转图像按层旋转,我们只需要记录四个顶点,并且本题是一个方阵,四个顶点就能完成图像的旋转操作。 1、逐层旋转 注意到&#xff0…

设计随笔 ---- ADR4525 篇

ADR4525一颗超低噪声、高精度2.5V基准电压源; Fluke 17B准确度指标: ADR4525指标: Fluke 17B测试结果: 2.5V的基准,输出只有2.477V,其实这么高精度的电压基准用3位半的万用表来测试本身就是一个错误&#…

3-哈希表-51-四数相加 II-LeetCode454

3-哈希表-51-四数相加 II-LeetCode454 LeetCode: 题目序号454 更多内容欢迎关注我(持续更新中,欢迎Star✨) Github:CodeZeng1998/Java-Developer-Work-Note 技术公众号:CodeZeng1998(纯纯技术文&#xff…

《QT实用小工具·七十》openssl+qt开发的P2P文件加密传输工具

1、概述 源码放在文章末尾 该项目实现了P2P的文件加密传输功能,具体包含如下功能: 1、 多文件多线程传输 2、rsaaes文件传输加密 3、秘钥随机生成 4、断点续传 5、跨域传输引导服务器 项目界面如下所示: 接收界面 发送界面 RSA秘钥生成…

(二)深度学习基础练习题(54道选择题)

本文整理了深度学习基础知识相关的练习题,共54道,适用于想巩固深度学习基础的同学。来源:如荷学数据科学题库(技术专项-深度学习)。 1) 2) 3) 4) 5) 6&#…

【CW32F030CxTx StartKit开发板】开发资料

本来是参加21ic的评测活动,不知道为什么评测文章一直被提示有不良内容,所以只好先在此记录一下相关的资料。 此次测试的是CW32F030CxTxStartKit 评估板。该开发板为用户提供一种经济且灵活的方式使用 CW32F030CxTx 芯片构建系统原型,可进行性…

插卡式仪器模块:音频分析模块(插卡式)

• 24 位分辨率 • 192 KHz 采样率 • 支持多种模拟音频信号的输入/输出 应用场景 • 音频信号分析:幅值、频率、信噪比、THD、THDN 等指标 • 模拟音频测试:耳机、麦克风、扬声器测试,串扰测 音频分析仪 输入阻抗10 TΩ10 TΩ输入范围3…

第103天: 权限提升-Linux 系统辅助项目脏牛Dirty内核漏洞SUIDGUID

项目下载地址 综合类探针: https://github.com/liamg/traitor 自动化提权: https://github.com/AlessandroZ/BeRoot 信息收集: https://github.com/rebootuser/LinEnum https://github.com/sleventyeleven/linuxprivchecker 漏洞探针&#xf…

AI网络爬虫:批量爬取豆瓣图书搜索结果

工作任务:爬取豆瓣图书搜索结果页面的全部图书信息 在ChatGPT中输入提示词: 你是一个Python编程专家,要完成一个爬虫Python脚本编写的任务,具体步骤如下: 用 fake-useragent库设置随机的请求头; 设置chr…

【小程序】WXML模板语法

目录 数据绑定 数据绑定的基本原则 在data中定义页面的数据 Mustache语法的格式 Mustache语法的应用场景 事件绑定 什么是事件 小程序中常用的事件 事件对象的属性列表 target和currentTarget的区别 bindtap的语法格式 在事件处理函数中为data中的数据赋值 事件…

【linux】进程控制——进程创建,进程退出,进程等待

个人主页:东洛的克莱斯韦克-CSDN博客 祝福语:愿你拥抱自由的风 相关文章 【Linux】进程地址空间-CSDN博客 【linux】详解linux基本指令-CSDN博客 目录 进程控制概述 创建子进程 fork函数 父子进程执行流 原理刨析 常见用法 出错原因 进程退出 概…

【Linux】进程6——环境变量

1.什么是环境变量 环境变量(environment variables)一般是指在操作系统中用来指定操作系统运行环境的一些参数 比如:我们在编写C/C代码的时候,在链接的时候,从来不知道我们的所链接的动态静态库在哪里,但是照样可以链接成功&…