- 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.
- Facebook Application Development with Graph API Cookbook
- C程序設(shè)計(jì)簡(jiǎn)明教程(第二版)
- Building a RESTful Web Service with Spring
- Python自動(dòng)化運(yùn)維快速入門(mén)
- Vue.js 3.x從入門(mén)到精通(視頻教學(xué)版)
- C語(yǔ)言程序設(shè)計(jì)實(shí)踐教程
- Learning Apache Kafka(Second Edition)
- PHP+MySQL+Dreamweaver動(dòng)態(tài)網(wǎng)站開(kāi)發(fā)實(shí)例教程
- C++ 從入門(mén)到項(xiàng)目實(shí)踐(超值版)
- 低代碼平臺(tái)開(kāi)發(fā)實(shí)踐:基于React
- Protocol-Oriented Programming with Swift
- Python 3 Object:oriented Programming(Second Edition)
- UML基礎(chǔ)與Rose建模實(shí)用教程(第三版)
- 零基礎(chǔ)學(xué)SQL(升級(jí)版)
- Clojure for Finance