一、GameObject发送消息的方法
GameObject类有三个方法可以实现发送消息,即SendMessage、BroadcastMessage和SendMessageUpwards。但是它们之间也是有区别的,如下:
假如cube0是cube1和cube2的父类一级,而cube1和cube2处于同一级。
- SendMessage只能向自身发送消息,和自身同级的物体不会接收到消息。cube1不能接收到cube2发送的消息。
- BroadcastMessage是向自身以及所有子类发送消息,和自身同级的物体不会收到消息。cube0、cube1和cube2可以接收cube0发送的消息。
- SendMessageUpwards向自身和其所有父类发送消息。和自身同级的物体不能接收到消息。cube0可以接收到cube1和cube2发送的消息,但是cube1和cube2不能接收到彼此发送的消息。
二、SendMessage方法
函数原型:
public void SendMessage(string methodName);
public void SendMessage(string methodName,object value);
public void SendMessage(string methodName,SendMessageOptions options);
public void SendMessage(string methodName,object value,SendMessageOptions options);
参数methodName为接收信息的方法名字,参数value为信息的内容,参数options为信息接收的方式,默认为SendMessageOpions.RequireReciver。另外还有一种接收信息的方式为SendMessageOptions.DontRequireReciver。前者要求信息的接收方式必须有接受信息的方法,否则程序会报错,而后者没有要求。
三、BroadcastMessage方法
函数原型:
public void BroadcastMessage(string methodName);
public void BroadcastMessage(string methodName,object value);
public void BroadcastMessage(string methodName,SendMessageOptions options);
public void BroadcastMessage(string methodName,object value,SendMessageOptions options);
参数解释同SendMessage。
四、SendMessageUpwards方法
函数原型:
public void SendMessageUpwards(string methodName);
public void SendMessageUpwards(string methodName,object value);
public void SendMessageUpwards(string methodName,SendMessageOptions options);
public void SendMessageUpwards(string methodName,object value,SendMessageOptions options);
参数解释同SendMessage。
五、演示
创建3个GameObject对象cube0、cube1、cube2,cube0是cube1和cube2的父类一级,而cube1和cube2处于同一级。按顺序分别绑定脚本BroadcastMessage_ts.cs、SendMessage_ts.cs和SendMessageUpwards_ts.cs,脚本代码如下:
1、BroadcastMessage_ts脚本
using UnityEngine;
using System.Collections; public class BroadMessage_ts : MonoBehaviour { // Use this for initialization
void Start () { gameObject.BroadcastMessage("GetParentMessage",gameObject.name+":use BroadcastMessage send!");
} public void GetParentMessage(string str)
{
Debug.Log(gameObject.name + "收到父类发送的消息: " + str);
} public void GetChildrenMessage(string str)
{
Debug.Log(gameObject.name + "收到子类发送的消息: " + str);
} // Update is called once per frame
void Update () { }
}
2、SendMessage_ts脚本
using UnityEngine;
using System.Collections; public class SendMessage_ts : MonoBehaviour { // Use this for initialization
void Start () { gameObject.SendMessage("GetSelfMessage", gameObject.name + "use SendMessage send!");
} public void GetSelfMessage(string str)
{
Debug.Log(gameObject.name + "收到自身发送的消息: " + str);
} public void GetParentMessage(string str)
{
Debug.Log(gameObject.name + "收到父类发送的消息: " + str);
} public void GetChildrenMessage(string str)
{
Debug.Log(gameObject.name + "收到子类发送的消息: " + str);
} // Update is called once per frame
void Update () { }
}
3、SendMessageUpwards_ts脚本
using UnityEngine;
using System.Collections; public class SendMessageUpwards_ts : MonoBehaviour { // Use this for initialization
void Start()
{ gameObject.SendMessageUpwards("GetChildrenMessage", gameObject.name + "use SendMessageUpwards send!");
} public void GetChildrenMessage(string str)
{
Debug.Log(gameObject.name + "收到子类发送的消息: " + str);
} public void GetParentMessage(string str)
{
Debug.Log(gameObject.name + "收到父类发送的消息: " + str);
} // Update is called once per frame
void Update () { }
}
4、运行结果