| License |
|---|
Hassle-free event system for Unity projects. Shuttle around arbitrary data; use the Inspector as much or as little as you want.
- Baloney-free experience.
- Integrates with Timeline.
- Debug window to track event messages.
Just call Message.Send and pass in an "address" and optionally an object.
using Extra.Postage;
using UnityEngine;
public class Sender : MonoBehaviour
{
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
Message.Send("key down", KeyCode.Space);
}
}Then receive it elsewhere by subscribing a method to the same "address."
using Extra.Postage;
using UnityEngine;
public class Receiver : MonoBehaviour
{
private void OnEnable()
{
// The type parameter filters received messages.
// Only messages with data of type KeyCode will invoke PrintKeyPressed.
Message.Subscribe<KeyCode>("key down", PrintKeyPressed);
}
private void OnDisable()
{
Message.Unsubscribe<KeyCode>("key down", PrintKeyPressed);
}
private void PrintKeyPressed(KeyCode code)
{
Debug.Log($"{code} was pressed!");
}
}Postage is super compact, but there are several convenience features for cleaner and/or more Inspector-forward usage.
using Extra.Postage;
using UnityEngine;
// MessageBehaviour takes care of unsubscribing for you.
// In the Subscribe method, simply call subscribe on the MessageBus parameter
// instead of Message.Subscribe().
public class FancyReceiver : MessageBehaviour
{
// You can use a string or an AddressSO ScriptableObject as an address.
// AddressField lets you toggle between the two in the Inspector.
// Just pass it in like a normal address.
[SerializeField] private AddressField address;
protected override void Subscribe(MessageBus bus)
{
// Subscribe with the type parameter "object"
// to receive all messages with data of any type.
bus.Subscribe<object>(address, PrintKeyPressed);
}
private void PrintKeyPressed(object data)
{
if (data is KeyCode code)
Debug.Log($"{code} was pressed!");
else
Debug.Log($"Received data: {data}");
}
}It's easy!
- Open the Unity Editor. On the top ribbon, click Window > Package Manager.
- Click the + button in the upper left corner, and click "Add package from git url..."
- Enter "https://github.com/bgsulz/Postage-for-Unity.git"
- Enjoy!
- Click the big green "Code" button and click Download ZIP.
- Extract the contents of the .zip into your Unity project's Assets folder.
- Enjoy!