unity 3种message消息管理使用

BroadcastMessage 广播消息

function BroadcastMessage (methodName : string, parameter : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver) : void

描述 
在游戏物体每一个MonoBehaviour和它的全部子物体上调用名为methodName的方法。 
通俗的解释:BroadcastMessage朝物体和所有子物体发送消息。

对一个物体及其所有子物体,进行消息的广播,如果其中任何子物体上贴有脚本,而脚本中有相应的处理此消息的函数,则Invoke调用之。

接受消息的此方法(函数)可以通过设置没有引用参数表来忽略传过来的消息中的参数。如果选项Option中设置成了SendMessageOptions.RequireReceiver,那么当没有任何脚本组件接受此消息时,一个相应的错误会弹出来 
C# JavaScript using UnityEngine;

using System.Collections;

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);
}

SendMessage 发送消息

function SendMessage (methodName : string, value : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver) : void

描述 
SendMessage朝本级别物体的多个脚本发送信息,也就是向当前对象挂载的所有脚本上面发送消息。 
在游戏物体每一个MonoBehaviour上调用名为methodName的方法。

接受此消息的函数也可以没有参数。如果选项Option中设置成了SendMessageOptions.RequireReceiver,那么当没有任何脚本组件接受此消息时,一个相应的错误会弹出来

C# JavaScript using UnityEngine;

using System.Collections;

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);
}

SendMessageUpwards 向上发送消息

function SendMessageUpwards (methodName : string, value : object = null, options : SendMessageOptions = SendMessageOptions.RequireReceiver) : void

Description描述 
SendMessageUpwards朝物体和上级父物体发送信息。 
在游戏物体每一个MonoBehaviour和每一个behaviour的祖先上调用名为methodName的方法。

接受此消息的函数也可以没有参数。如果选项Option中设置成了SendMessageOptions.RequireReceiver,那么当没有任何脚本组件接受此消息时,一个相应的错误会弹出来

C# JavaScript using UnityEngine;

using System.Collections;

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.Collections;

public class xx1 : MonoBehaviour
{

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 100, 50), "发送1"))
        {//this gameobjec
            SendMessage("myTest");
        }
        if (GUI.Button(new Rect(10, 150, 100, 50), "发送2"))
        {//this gameobjec and it's childs
            BroadcastMessage("myTest");
        }

        if (GUI.Button(new Rect(10, 200, 100, 50), "发送3"))
        {//this gameobjec‘parent 
            SendMessageUpwards("myTest");
        }
    }
脚本2 接收消息

using UnityEngine;
using System.Collections;

public class XXX : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void OnDrag(Vector2 delta)
    {
        Debug.Log("-------OnDrag--------");
    }

   public void myTest() {
        Debug.Log("this is a methord:"+gameObject.name);   
    }
}


发送1按钮点击,sendMessage,运行结果

this is a methord:Plane1 
也就是只有plane1 收到消息

发送2按钮点击,broadCaseMessage运行结果 
 
发送3按钮点击,SendMessageUpwards运行结果

猜你喜欢

转载自blog.csdn.net/liu943367080/article/details/86312287
今日推荐