The method of three kinds of the Message Unity API learning Notes (2) -GameObject of

Official Documents> GameObject

Firstly, the test object:

 

Add two scripts (GameObejctTest and Target) in the Father, they are used to send and receive Message Message:

Adding script called Target in other GameObject in.

 

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class GameObjectTest : MonoBehaviour
 6 {
 7     // Start is called before the first frame update
 8     public GameObject target;
 9     void Start()
10     {
11         // target.SendMessage("Hello");
12         // target.BroadcastMessage("Hello");
13         // target.SendMessageUpwards("Hello");
14     }
15     void Hello()
16     {
17         Debug.Log("In GameObjectTest:" + target.name);
18     }
19 }
 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class Target : MonoBehaviour
 6 {
 7     public GameObject target;
 8     void Hello()
 9     {
10         Debug.Log("In Target:" + target.name);
11     }
12 }

分别依次执行:

  •   SendMessage:Calls the method named methodName on every MonoBehaviour in this game object.

      

    也就是当前发送端(Father)所在GameObject下的组件都接收到了Message,但是Son没有接收到,Others没有接收到,Ancestor也没有。

 

  •   BroadcasMessage:Calls the method named methodName on every MonoBehaviour in this game object or any of its children.

    

    Here you can see all the subclasses include subclasses have subclasses to the Message received, to point out that the components (Component) also belong to the GameObject, it also receives Message.

  •   SendMessageUpwards:Calls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour.

    

    Its parent (the Ancestor) receives the message, including its own. (Here that is every parent can receive Message, meaning that if there is on Ancestor class, so they can receive the Message)

 

Not difficult to find by returning the results of these three methods will call the Message is sent to the current scope of the recipient (who is sent, it will give myself a copy of the message).

 

Guess you like

Origin www.cnblogs.com/braink-1400/p/11200028.html