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");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值