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

目录

消息机制

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

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

父对象向子对象发送消息

​编辑

子对象向父对象发送消息


消息机制

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

基本语法如下:

cs 复制代码
void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

methodName: 要调用的方法的名称。

value: 可选参数,要传递给方法的参数。

options: 可选参数,用于指定如何处理未找到接收方的情况。

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

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

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

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

cs 复制代码
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);
    }
}
cs 复制代码
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()
    {
        
    }
}
cs 复制代码
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()
    {
        
    }
}
cs 复制代码
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);
    }
}

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

cs 复制代码
SendMessage("GetTestMsg",SendMessageOptions.DontRequireReceiver);

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

父对象向子对象发送消息

cs 复制代码
​​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");
    }
}
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

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

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

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

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

子对象向父对象发送消息

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

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

public class NO8_MessageTest : MonoBehaviour
{
    //向自身和上级发送消息,总当前的目录地位开始
    
    public void getMesage()
    {
        print("Test");
    }
}
cs 复制代码
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");
    }
}
cs 复制代码
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");
    }
}
相关推荐
Yasin Chen16 分钟前
C# Dictionary源码分析
算法·unity·哈希算法
阿蒙Amon2 小时前
C# Linq to SQL:数据库编程的解决方案
数据库·c#·linq
iCxhust5 小时前
c# U盘映像生成工具
开发语言·单片机·c#
emplace_back6 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
阿蒙Amon7 小时前
为什么 12 版仍封神?《C# 高级编程》:从.NET 5 到实战架构,进阶者绕不开的必修课
开发语言·c#
深海潜水员8 小时前
【Behavior Tree】-- 行为树AI逻辑实现- Unity 游戏引擎实现
游戏·unity·c#
开开心心_Every8 小时前
便捷的Office批量转PDF工具
开发语言·人工智能·r语言·pdf·c#·音视频·symfony
小码编匠10 小时前
C# 上位机开发怎么学?给自动化工程师的建议
后端·c#·.net
钢铁男儿11 小时前
C# 接口(什么是接口)
java·数据库·c#