Skip to content
This repository has been archived by the owner on Oct 13, 2022. It is now read-only.

Latest commit

 

History

History
91 lines (65 loc) · 2.13 KB

README.md

File metadata and controls

91 lines (65 loc) · 2.13 KB

Telegraph

An event/message system for Unity

This library requires Odin Inspector

Define event types (if not present in the library)

public class FloatMessage : MessageBase<float> {}

[CreateAssetMenu(menuName = Telegraph.MENU_PATH + "Float", fileName = "Float Channel")]
public class FloatChannel : ChannelBase<FloatMessage, float> {}

Define your events in code

[Telegraph]
public static class Events
{
    [Telegram]
    public static Message StartGame => Telegraph.Get<Message>(nameof(StartGame));
    [Telegram]
    public static Message OnStartGame => Telegraph.Get<Message>(nameof(OnStartGame));
}

[Telegraph]
public static class PlayerEvents
{
    [Telegram]
    public static FloatMessage OnDamage => Telegraph.Get<FloatMessage>(nameof(OnDamage));
}

Register your methods to them

Events.StartGame.On += () => { Debug.Log("Game Started") }

Then you can fire them from code

Events.StartGame.Raise()
PlayerEvents.OnDamage.Raise(5f)

You can also create scriptable object assets to reference the ones defined in code: Create -> Red Owl -> Channel -> Void

And then you can use normal methods to work with scriptable objects and call the methods on them (or even load them via addressables)

Lastly you can use MessageListener component to easily create a listener that will trigger the attached UnityEvent.

You can event make your own listener's

public class SceneMessage : MessageBase<SceneMetadata> {}
    
[CreateAssetMenu(menuName = Telegraph.MENU_PATH + "Scene", fileName = "Scene Channel")]
public class SceneChannel : ChannelBase<SceneMessage, SceneMetadata> {}

public class SceneChannelListener : MonoBehaviour
{
    [Serializable]
    public class SceneUnityEvent : UnityEvent<SceneMetadata> {}
    
    public SceneChannel channel;

    public SceneUnityEvent On;

    private void OnEnable()
    {
        channel.On += Handler;
    }

    private void OnDisable()
    {
        channel.On -= Handler;
    }

    private void Handler(SceneMetadata metadata)
    {
        On?.Invoke(metadata);
    }
}

Everything is strongly typed!

Cheers