MessageTypes
MessageTypes are the cornerstone of GlobalMessage. You use them to distinguish between information that you want to communicate / receive. In general, there should be a unique MessageType for every piece of information you want to communicate.
- Receivers add themselves to specific MessageTypes to get informed whenever this MessageType is sent in the future.
- Senders send specific MessageTypes to inform the registered Receivers.
Note
In general, there should be a MessageType for every piece information you want to communicate. The MessageType defines the semantics. For example, if you want to communicate that the player reached the next level, you would create a unique OnPlayerLevelUp
MessageType.
Creating MessageTypes
Using the Editor
The recommended way is to create new MessageTypes via the context menu in the editor project view.
- in the project view: navigate to the folder where you want to create the new MessageType
- in the project view: right click inside the folder
- in the context menu: navigate to
Create > GlobalMessage > MessageType
- name the new file according to your new MessageType name
From Code
- create a new class that derives from
MessageType
Here is a complete example with optional EditorSettings
:
using MalteHusung.GlobalMessage;
[MessageTypeEditorSettings(Description = "This is my new MessageType.", UseEditorFeatures = true, UseEditorMessageObject = true)]
public class MyNewMessage : MessageType
{
}
Editor Message Object (EMO)
After creation of a new MessageType, GlobalMessage will automatically create an accompanying ScriptableObject: the Editor Message Object (EMO). The EMO is the Editor representation of the MessageType. It is used to quickly and easily reference the MessageType inside the Editor e.g. in custom scripts, GlobalMessage's MessageSender component or inside the Info Window.
The EMO is kept up to date automatically when you change the underlying MessageType and is recreated if the old EMO was deleted while its MessageType still exists (these updates take effect after script compilation). These automatic behaviors can be changed individually for each MessageType by applying MessageTypes Editor Settings.
MessageType Inheritance
Every valid MessageType has to inherit from MessageType
or MessageType<T>
.
While it is possible to create an inheritance hierarchy for your MessageTypes, GlobalMessage does not consider inheritance when sending or receiving Messages, meaning:
- Receivers for the base type do not get notified when a Message of a derived type is sent.
- Receivers for a derived type do net get notified when a Message of the base type is sent.
- When sending a Message, only the actual MessageType is considered.
Renaming MessageType and its EMO
To rename your MessageType, first save your project and the scene. Then, open the MessageType's C# script file and rename the class with your IDE's refactoring / renaming functionality. Save the change inside the IDE. Then, focus the Editor again to trigger script compilation. After scripts have compiled, the EMO is renamed automatically.
Note
EMOs can not be renamed on their own. They will always have their MessageTypes's name after script compilation.
Note
There is a rare bug that happens sometimes when renaming via Jetbrains Rider IDE and immediately focusing Unity without saving inside IDE first. When the bug happens, Unity displays fields that contain the renamed EMO as None
. This is fixed easily by closing and restarting Unity.