- Unity 2017 Game Optimization(Second Edition)
- Chris Dickinson
- 423字
- 2021-07-02 23:21:10
Wrapping up the Messaging System
Congratulations are in order, as we have finally built a fully functional global Messaging System that any and all objects can interface with, and use it to send messages between one another. A useful feature of this approach is that it is Type-agnostic, meaning that the message senders and listeners do not even need to derive from any particular class in order to interface with the Messaging System; it just needs to be a class that provides a message type and a delegate function of the matching function signature, which makes it accessible to both ordinary classes and MonoBehaviours.
As far as benchmarking the MessagingSystem class goes, we will find that it is capable of processing hundreds, if not thousands, of messages in a single frame with minimal CPU overhead (depending on the CPU, of course). The CPU usage is essentially the same, whether one message is being distributed to 100 different listeners or 100 messages are distributed to just one listener. Either way, it costs about the same.
Even if we're predominantly sending messages during UI or gameplay events, this probably has far more power than we need. So, if it does seem to be causing performance problems, then it's far more likely to be caused by what the listener delegates are doing with the message than the Messaging System's ability to process those messages.
There are many ways to enhance the Messaging System to provide more useful features we may need in the future, as follows:
- Allow message senders to suggest a delay (in time or frame count) before a message is delivered to its listeners
- Allow message listeners to define a priority for how urgently it should receive messages compared to other listeners waiting for the same message type. This is a means for listeners to skip to the front of the queue if it was registered later than other listeners
- Implement some safety checks to handle situations where a listener gets added to the list of message listeners for a particular message while a message of that type is still being processed. Currently, C# will throw an EnumerationException at us since the delegate list will be changed by AttachListener(), while it is still being iterated through in TriggerEvent()
At this point, we probably explored the Messaging System enough, so these tasks will be left as an academic exercise for you to undertake, if you become comfortable using this solution in your games. Let's continue to explore more ways to improve performance through script code.
- C# Programming Cookbook
- 深入淺出Prometheus:原理、應(yīng)用、源碼與拓展詳解
- 數(shù)據(jù)庫系統(tǒng)原理及MySQL應(yīng)用教程
- Selenium Design Patterns and Best Practices
- R語言游戲數(shù)據(jù)分析與挖掘
- Mastering Linux Security and Hardening
- TMS320LF240x芯片原理、設(shè)計及應(yīng)用
- OpenCV with Python By Example
- RubyMotion iOS Develoment Essentials
- 區(qū)塊鏈架構(gòu)之美:從比特幣、以太坊、超級賬本看區(qū)塊鏈架構(gòu)設(shè)計
- Deep Learning for Natural Language Processing
- Arduino Electronics Blueprints
- 產(chǎn)品架構(gòu)評估原理與方法
- C# 10核心技術(shù)指南
- Learning Node.js for Mobile Application Development