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

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.

主站蜘蛛池模板: 门源| 建昌县| 澳门| 麻城市| 茶陵县| 德江县| 嘉祥县| 萝北县| 岳普湖县| 合山市| 伊春市| 南充市| 大安市| 阳山县| 故城县| 安陆市| 定兴县| 辰溪县| 阿图什市| 蓝田县| 新余市| 陆良县| 周宁县| 临夏县| 泉州市| 开封市| 镇雄县| 涞水县| 麻阳| 崇文区| 滁州市| 洪湖市| 昭通市| 甘孜县| 和林格尔县| 大新县| 金溪县| 资溪县| 昭苏县| 南宁市| 威信县|