29631人加入学习
(126人评价)
Unity API常用方法和类详细讲解(基于Unity5.6)
价格 免费
承诺服务

GameObject.BroadcastMessage

 
public void BroadcastMessage(string methodName, object parameter = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

Description

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

The receiving method can choose to ignore parameter by having zero parameters. If options is set to SendMessageOptions.RequireReceiver an error is printed when the message is not picked up by any component.

using UnityEngine;

public class Example : MonoBehaviour
{
    void Start()
    {
        /// Calls the function ApplyDamage with a value of 5
        /// // Every script attached to the game object and all its children
        // that has a ApplyDamage function will be called.
        gameObject.BroadcastMessage("ApplyDamage", 5.0);
    }
}



public class Example2 : MonoBehaviour
{
    public void ApplyDamage(float damage)
    {
        print(damage);
    }
}

 

GameObject.SendMessage

public void SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

Parameters

methodName The name of the method to call.
value An optional parameter value to pass to the called method.
options Should an error be raised if the method doesn't exist on the target object?

Description

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

The receiving method can choose to ignore the argument by having zero parameters. If options is set to SendMessageOptions.RequireReceiver an error is printed if the message is not picked up by any component.

Note that messages will not be sent to inactive objects (ie, those that have been deactivated in the editor or with the SetActive function).

using UnityEngine;

public class Example : MonoBehaviour
{
    void Start()
    {
        // Calls the function ApplyDamage with a value of 5
        // Every script attached to the game object
        // that has an ApplyDamage function will be called.
        gameObject.SendMessage("ApplyDamage", 5.0);
    }
}

public class Example2 : MonoBehaviour
{
    public void ApplyDamage(float damage)
    {
        print(damage);
    }
}

 

GameObject.SendMessageUpwards

public void SendMessageUpwards(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

Parameters

methodName The name of the method to call.
value An optional parameter value to pass to the called method.
options Should an error be raised if the method doesn't exist on the target object?

Description

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

The receiving method can choose to ignore the argument by having zero parameters. If options is set to SendMessageOptions.RequireReceiver an error is printed when the message is not picked up by any component.

Note that messages will not be sent to inactive objects (ie, those that have been deactivated in the editor or with the SetActive function).

using UnityEngine;

public class Example : MonoBehaviour
{
    void Start()
    {
        // Calls the function ApplyDamage with a value of 5
        gameObject.SendMessage("ApplyDamage", 5.0);
    }
}

public class Example2 : MonoBehaviour
{
    public void ApplyDamage(float damage)
    {
        print(damage);
    }
}

 

 

 

 

 

[展开全文]

游戏物体间消息的发送和接收

通过对象来调用的方法 (public Functions)

BroadcastMessage:广播一个消息,调用方法

自身和其子方法都会调用

public class MessageCeS : MonoBehaviour {
	public GameObject target;
	// Use this for initialization
	void Start () {
		//括号中写具体的方法名,首字母要带大写,BroadcastMessage调用带有Attack方法的物体与其子物体
		target.BroadcastMessage("Attack", null, SendMessageOptions.DontRequireReceiver);
		
	}
	// Update is called once per frame
	
}
public class Cube : MonoBehaviour {

	// Use this for initialization
	void Attack()
	{
		Debug.Log(this.gameObject+"正在攻击");
	}
}

SendMessage:针对某个目标直接发送消息

不会影响其子物体

target.SendMessage("Attack", null, SendMessageOptions.DontRequireReceiver);

SendMessageUpwards:向上发送,调用当前物体与其父级(他的父级,他父级的父级.....)

target.SendMessageUpwards("Attack", null, SendMessageOptions.DontRequireReceiver);

 

 

 

[展开全文]

GameObject中需要对象访问的方法:

GetComponent类(获取组件)

SendMessage类(消息发送/接收类)

SetActive

CompareTag

消息类:

BroadCastMessage(方法名)

注:自身和子物体的所有该方法都会被调用。减少引用,减少耦合性。

SendMessage(方法名)

注:只会搜寻自身的该方法,不会搜寻子物体上是否有该方法。

SendMessageUpWord(方法名)

注:自身+父物体。

[展开全文]

游戏物体之间消息发送。

对象方法

SetActive 

Message.

SendMessage,

BroadCastMessage("方法名");调用方法。所有方法名这个,所有物体都进行使用。降低耦合性。互相调来调去。需要有接受者。

作用范围:当前物体以及所有孩子

target. BroadCastMessage("方法名",null,SendMessageOptions.DontRequireReceiver);规定后面是否必须要接受者。

接受的话需要状态处于激活。

target.SendMessage(“方法名”);不会去调用孩子。

只调用当前物体上的方法。

只针对某一物体上所有物体有这个方法就会调用。

target. BroadCastMessageUpWord 作用范围:当前物体以及所有父类。

Component,

[展开全文]

//发送该节点的所有子节点

GameObject.BroadcastMessage(string methodName, object parameter = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

 

//发送给物体本身的广播

GameObject.SendMessage(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

 

//发送给发节点的所有基(父)节点

GameObject.SendMessageUpwards(string methodName, object value = null, SendMessageOptions options = SendMessageOptions.RequireReceiver);

[展开全文]

1、调用自身以及子类存在的指定的方法:GameObject.BroadcastMessage("方法名",参数值,SendMessageOptions.是否要接收者)

2、调用自身存在的指定的方法:GameObject.SendMessage()

3、调用自身以及父类存在的指定的方法:GameObject.SendMessageUpwards()

[展开全文]

BroadcastMessage

SendMessage

SendMessageUpwards

[展开全文]

游戏物体GameObject的公有方法(函数)

通过对象名调用;

AddComponent():给游戏物体添加组件;

只要脚本上有方法名(“”)就可以调用;

BroadcastMessage(“”):广播消息,包括当前游戏物体和它的子物体

SendMessage(“”):只在某个游戏物体上发送消息不包括子物体;

 endMessageUpwards:广播消息,包括当前游戏物体和它的父物体;(每个游戏物体只能有一个父物体)

 

 

[展开全文]

sendMessageUpwards 查找本物体上包括父物体 脚本调用传递过来方法名的方法。

[展开全文]

游戏物体间消息的发送broadcastMessage和接受sendMessage可以避免耦合性:A引用B,B引用C,如果A改变就会影响B和C

[展开全文]

setactive激活禁用游戏物体

gameobject.BroadcastMessage()向自己和所有子物体发送信息命其调用方法

后面可以在第三个参数的位置选有没有接收者

gameobject.sendmessageupwards()向自己和所有父物体发送信息命其调用方法

gamepbject.sendmessage() 会向物体和其子物体发送

[展开全文]

授课教师

SiKi学院老师

课程特色

图文(1)
下载资料(2)
视频(71)