【Unity】API学习-->消息系统

发送消息

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

    • 传给自身及自身组件
    • methodName 字符串 要调用方法的名称
    • value 要传递的参数
    • options 如果目标对象上不存在该方法,是否应该引发错误?
  • BroadcastMessage (string methodName, object parameter= null, SendMessageOptions options= SendMessageOptions.RequireReceiver)

    • 广播消息 向下按层级发送,包括自己
  • SendMessageUpwards (string methodName, object value= null, SendMessageOptions options= SendMessageOptions.RequireReceiver);

    • 向上按层级发送消息,包括自己

父类子类接受消息顺序

在这里插入图片描述
父类子类接受消息顺序

    void Start() {
    
    
    	//发送消息
        SendMessage("GetMeg",100.0f);
    }
	//接受消息
    void GetMeg(float num) {
    
    
        Debug.Log("接收到了消息:"+num);
    }

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ainklg/article/details/129773455