Sending Messages via EMO Reference
Using EditorMessageObjects (EMOs) to send Messages gives you the most flexibility and control at the same time. This workflow allows you to send Messages from custom scripts while the MessageType is selected from the Editor. This enables you to write scripts in a general way and set up the sent MessageType later in the Editor according to the specific context.
To use an EMO in your script, you have to expose a field of its type or a base type to the Editor. This can be done by declaring it as public
or using the SerializeReference
attribute on a private property.
Note
In most cases you want your exposed property to be of the base types MessageType
or MessageType<T>
. This way, you can select any of the derived MessageTypes in the Editor, giving you the most flexibility.
To send the Message using the EMO from your script, call Send
on your property.
The following code sends the Message whenever the spacebar is pressed down:
using MalteHusung.GlobalMessage;
using UnityEngine;
public class SendingViaEmo : MonoBehaviour
{
public MessageType MessageToSend;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
MessageToSend.Send(this);
}
}
}
Don't forget to assign your EMO in the inspector.