- Unity 2017 Game Optimization(Second Edition)
- Chris Dickinson
- 174字
- 2021-07-02 23:21:09
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.
- 精通Nginx(第2版)
- PHP 7底層設計與源碼實現
- PostgreSQL技術內幕:事務處理深度探索
- Learning Laravel 4 Application Development
- Podman實戰
- Java Web開發詳解
- Advanced Express Web Application Development
- 零基礎學C語言程序設計
- Hadoop大數據分析技術
- Natural Language Processing with Python Quick Start Guide
- 交互設計師成長手冊:從零開始學交互
- Visual FoxPro程序設計實驗教程
- Mastering VMware Horizon 6
- Augmented Reality for Developers
- Developing Multi:Platform Apps with Visual Studio Code