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

  • 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.

主站蜘蛛池模板: 梁平县| 永登县| 石嘴山市| 尚志市| 富阳市| 弋阳县| 莱芜市| 瑞金市| 百色市| 探索| 山丹县| 三台县| 巨野县| 泰宁县| 南部县| 灌南县| 碌曲县| 康定县| 扎鲁特旗| 措美县| 安陆市| 峨山| 章丘市| 乡城县| 灵台县| 杭锦后旗| 西藏| 兴和县| 上饶县| 温宿县| 东乡县| 蒲城县| 敖汉旗| 高密市| 南乐县| 怀化市| 陵川县| 太湖县| 来安县| 义乌市| 南平市|