Unity3d下SendMessage、SendMessageUpwards、BroadcastMessage的区别与使用
Unity提供了几种消息推送机制以方便多个物体间的消息传递与接收,分别是SendMessage、SendMessageUpwards、BroadcastMessage。
函数原型
三个函数的参数相似,都是方法名+方法的参数+额外信息选项组成。
public void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);
public void SendMessageUpwards(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);
public void BroadcastMessage(string methodName, object parameter = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);
SendMessageOptions
- SendMessageOptions.RequireReceiver //如果没有找到相应函数,会报错(默认是这个状态)
- SendMessageOptions.DontRequireReceive