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

Implementing custom messages

We've created the Messaging System, but an example of how to use it would help us 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 notify other parts of our codebase 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.

主站蜘蛛池模板: 宜兰县| 西藏| 观塘区| 和平区| 武隆县| 辽中县| 武山县| 华蓥市| 塔河县| 和平县| 迁安市| 山西省| 湖口县| 太白县| 顺平县| 池州市| 平潭县| 虞城县| 宜州市| 城市| 平乡县| 庄河市| 永泰县| 宁城县| 利辛县| 江阴市| 新河县| 庐江县| 鄂托克旗| 平定县| 阜新| 城固县| 贺兰县| 西乌| 京山县| 鄄城县| 项城市| 黎城县| 文昌市| 霍山县| 册亨县|