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

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.

主站蜘蛛池模板: 丹江口市| 碌曲县| 彭山县| 乐清市| 洪洞县| 平昌县| 桐乡市| 民县| 宿迁市| 沁水县| 临夏县| 图片| 海口市| 日土县| 华池县| 阿荣旗| 西昌市| 凤城市| 萨嘎县| 永年县| 东港市| 武威市| 且末县| 文化| 镇安县| 墨竹工卡县| 廉江市| 许昌市| 大洼县| 六枝特区| 泾阳县| 礼泉县| 恩平市| 奉贤区| 曲水县| 丹东市| 汉川市| 康保县| 竹山县| 高陵县| 马公市|