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

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.

主站蜘蛛池模板: 巴林左旗| 云阳县| 靖远县| 舞钢市| 丁青县| 哈密市| 西吉县| 马山县| 全椒县| 上杭县| 梨树县| 蒲城县| 双牌县| 怀远县| 淳安县| 周至县| 南丰县| 津南区| 江安县| 新乐市| 沾化县| 张掖市| 舞钢市| 新郑市| 普兰县| 宁阳县| 丘北县| 海城市| 凭祥市| 班戈县| 大化| 潜江市| 工布江达县| 许昌县| 武冈市| 六盘水市| 阿拉善右旗| 涡阳县| 台中县| 当涂县| 德惠市|