unity 种message消息管理使用
unity 种message消息管理使用
在unit的Component类中,封装了种消息发送处理机制,接下来一起看一下他们的使用方法
function BroadcastMessage (methodame : string, parameter : object = null, opti : SendMessageOpti = SendMessageOpti.RequireReceiver) : void
描述
在游戏物体每一个MonoBehaviour和它的全部子物体上调用名为methodame的方法。
通俗的解释:BroadcastMessage朝物体和所有子物体发送消息。
对一个物体及其所有子物体,进行消息的广播,如果其中任何子物体上贴有脚本,而脚本中有相应的处理此消息的函数,则Invoke调用之。
接受消息的此方法(函数)可以通过设置没有引用参数表来忽略传过来的消息中的参数。如果选项Option中设置成了SendMessageOpti.RequireReceiver,那么当没有任何脚本组件接受此消息时,一个相应的错误会弹出来
C# JavaScript using UnityEngine;
using System.Collecti;public class example : MonoBehaviour {void ApplyDamage(float damage) {print(damage);}public void Awake() {BroadcastMessage(ApplyDamage, 5.0F);}
}// Calls the function ApplyDamage with a value of 5
//调用函数 ApplyDamage 值为5
BroadcastMessage (ApplyDamage, 5.0);// Every script attached to the game object and all its children
// that has a ApplyDamage function will be called.
//附加到游戏物体的每个脚本和全部的子物体,有一个ApplyDamage函数将被调用
function ApplyDamage (damage : float) {print (damage);
}
function SendMessage (methodame : string, value : object = null, opti : SendMessageOpti = SendMessageOpti.RequireReceiver) : void
描述
SendMessage朝本级别物体的多个脚本发送信息,也就是向当前对象挂载的所有脚本上面发送消息。
在游戏物体每一个MonoBehaviour上调用名为methodame的方法。
接受此消息的函数也可以没有参数。如果选项Option中设置成了SendMessageOpti.RequireReceiver,那么当没有任何脚本组件接受此消息时,一个相应的错误会弹出来
C# JavaScript using UnityEngine;
using System.Collecti;public class example : MonoBehaviour {void ApplyDamage(float damage) {print(damage);}public void Awake() {SendMessage(ApplyDamage, 5.0F);}
}// Calls the function ApplyDamage with a value of 5
//调用函数ApplyDamage 值为5
SendMessage (ApplyDamage, 5.0);// Every script attached to the game object
// that has a ApplyDamage function will be called.
//附加到游戏物体的每个脚本,有一个ApplyDamage函数将被调用
function ApplyDamage (damage : float) {
print (damage);
}
function SendMessageUpwards (methodame : string, value : object = null, opti : SendMessageOpti = SendMessageOpti.RequireReceiver) : void
Description描述
SendMessageUpwards朝物体和上级父物体发送信息。
在游戏物体每一个MonoBehaviour和每一个behaviour的祖先上调用名为methodame的方法。
接受此消息的函数也可以没有参数。如果选项Option中设置成了SendMessageOpti.RequireReceiver,那么当没有任何脚本组件接受此消息时,一个相应的错误会弹出来
C# JavaScript using UnityEngine;
using System.Collecti;public class example : MonoBehaviour {void ApplyDamage(float damage) {print(damage);}public void Awake() {SendMessageUpwards(ApplyDamage, 5.0F);}
}// Calls the function ApplyDamage with a value of 5
//调用函数ApplyDamage 值为5
SendMessageUpwards (ApplyDamage, 5.0);// Every script attached to the game object
// that has a ApplyDamage function will be called.
//附加到游戏物体的每个脚本,有一个ApplyDamage函数将被调用
function ApplyDamage (damage : float) {print (damage);
}
在unity中创建脚本,一个用于发送消息,一个用于接收消息
脚本1 发送消息
using UnityEngine;
using System.Collecti;public class xx1 : MonoBehaviour
{void OnGUI(){if (GUI.Button(new Rect(10, 10, 100, 50), 发送1)){//this gameobjecSendMessage(myTest);}if (GUI.Button(new Rect(10, 150, 100, 50), 发送2)){//this gameobjec and it s childsBroadcastMessage(myTest);}if (GUI.Button(new Rect(10, 200, 100, 50), 发送)){//this gameobjec‘parent SendMessageUpwards(myTest);}}}
脚本2 接收消息
using UnityEngine;
using System.Collecti;public class XXX : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}void OnDrag(Vector2 delta){Debug.Log(-------OnDrag--------);}public void myTest() {Debug.Log(this is a methord:); }}
发送1按钮点击,sendMessage,运行结果
this is a methord:Plane1
也就是只有plane1 收到消息
发送2按钮点击,broadCaseMessage运行结果
发送按钮点击,SendMessageUpwards运行结果
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 17 条评论) |
本站网友 一路畅通 | 22分钟前 发表 |
object = null | |
本站网友 哥哥gang | 13分钟前 发表 |
opti | |
本站网友 沈阳性病医院 | 27分钟前 发表 |
10 | |
本站网友 千锋网 | 18分钟前 发表 |
也就是向当前对象挂载的所有脚本上面发送消息 | |
本站网友 宝宝鼻塞 | 14分钟前 发表 |
void Description描述 SendMessageUpwards朝物体和上级父物体发送信息 | |
本站网友 子宫收缩乏力 | 16分钟前 发表 |
5.0F);} }// Calls the function ApplyDamage with a value of 5 //调用函数 ApplyDamage 值为5 BroadcastMessage (ApplyDamage | |
本站网友 皇帝故里 | 29分钟前 发表 |
Plane1 也就是只有plane1 收到消息 发送2按钮点击,broadCaseMessage运行结果 发送按钮点击,SendMessageUpwards运行结果 | |
本站网友 生殖整形医院 | 24分钟前 发表 |
100 | |
本站网友 白蔻仁 | 10分钟前 发表 |
parameter | |
本站网友 飞信发短信要钱吗 | 16分钟前 发表 |
void Description描述 SendMessageUpwards朝物体和上级父物体发送信息 | |
本站网友 苹果中国发布会 | 1分钟前 发表 |
200 | |
本站网友 俄克拉荷马州 | 7分钟前 发表 |
接受此消息的函数也可以没有参数 | |
本站网友 95kk | 8分钟前 发表 |
); }} 发送1按钮点击,sendMessage | |
本站网友 转基因食品的危害 | 11分钟前 发表 |
接受消息的此方法(函数)可以通过设置没有引用参数表来忽略传过来的消息中的参数 | |
本站网友 江湖餐厅 | 10分钟前 发表 |
对一个物体及其所有子物体,进行消息的广播,如果其中任何子物体上贴有脚本,而脚本中有相应的处理此消息的函数,则Invoke调用之 | |
本站网友 不良动漫 | 17分钟前 发表 |
100 |