您的位置:首页 > 科技 > 能源 > Unity API学习之消息机制理论与应用

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

2024/11/13 15:58:56 来源:https://blog.csdn.net/qq_75073393/article/details/139574158  浏览:    关键词: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 updatevoid Start(){gameObject.SendMessage("Mesage");gameObject.SendMessage("Mesages", 1);}// Update is called once per framevoid 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 updatevoid Start(){print("其他组件的消息发送给Mesages组件");}// Update is called once per framevoid Update(){}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Mesage : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){print("其他组件的消息发送给Mesage组件");}// Update is called once per framevoid Update(){}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class example : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid 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 updatevoid Start(){gameObject.SendMessage("Mesage");gameObject.SendMessage("Mesages", 1);}// Update is called once per framevoid Update(){}//无参发出消息public void Mesage(){print("消息发送!!!");}//有参发出消息public void Mesages(int value){print("本组件接收到消息" + value);}public void getMesage(){print("Message");}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com