- Unity Game Optimization
- Dr. Davide Aversa Chris Dickinson
- 178字
- 2021-06-24 12:13:05
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.
- Java程序設(shè)計(jì)(慕課版)
- 復(fù)雜軟件設(shè)計(jì)之道:領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)全面解析與實(shí)戰(zhàn)
- SoapUI Cookbook
- 測(cè)試驅(qū)動(dòng)開(kāi)發(fā):入門、實(shí)戰(zhàn)與進(jìn)階
- Apache Spark 2.x Machine Learning Cookbook
- React Native Cookbook
- Mastering Android Game Development
- Android玩家必備
- Test-Driven JavaScript Development
- Beginning C++ Game Programming
- Python函數(shù)式編程(第2版)
- 分布式架構(gòu)原理與實(shí)踐
- Learning NHibernate 4
- BackTrack 5 Cookbook
- Java與Android移動(dòng)應(yīng)用開(kāi)發(fā):技術(shù)、方法與實(shí)踐