官术网_书友最值得收藏!

Implementing custom messages

We've created the messaging system, but an example of how to use it would help us to wrap our heads around the concept. Let's start by defining a pair of simple classes that derive from Message, which we can use to create a new enemy, as well as to notify other parts of our code base that an enemy was created:

public class CreateEnemyMessage : Message {}

public class EnemyCreatedMessage : Message {

public readonly GameObject enemyObject;
public readonly string enemyName;

public EnemyCreatedMessage(GameObject enemyObject, string enemyName) {
this.enemyObject = enemyObject;
this.enemyName = enemyName;
}
}

CreateEnemyMessage is the simplest form of message that contains no special data, while EnemyCreatedMessage will contain a reference to the enemy's GameObject as well as its name. Good practice for message objects is to make their member variables not only public but also readonly. This ensures that the data is easily accessible but cannot be changed after the object's construction. This safeguards the content of our messages against being altered, as they're passed between one listener and another.

主站蜘蛛池模板: 壶关县| 巢湖市| 碌曲县| 洛扎县| 三明市| 泾阳县| 呼伦贝尔市| 盐城市| 丰县| 通辽市| 黎城县| 格尔木市| 芮城县| 西丰县| 临城县| 怀来县| 河间市| 从江县| 平果县| 雅安市| 东安县| 温宿县| 安阳市| 禹城市| 姚安县| 和龙市| 漾濞| 慈利县| 奎屯市| 龙海市| 北海市| 汉川市| 新乡县| 南通市| 盖州市| 广饶县| 南部县| 望城县| 山西省| 沙坪坝区| 乌鲁木齐县|