コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】シンプルなメッセージバス「BasicEventBus」紹介

はじめに

「BasicEventBus」を Unity プロジェクトに導入することで
シンプルなメッセージバスを使用できるようになります

使用例

送信側のスクリプト

using pEventBus;
using UnityEngine;

public struct PlayerRespawnSignal : IEvent
{
}

public class Player : MonoBehaviour
{
    private void Start()
    {
        EventBus<PlayerRespawnSignal>.Raise( new PlayerRespawnSignal() );
    }
}

受信側のスクリプト

using pEventBus;
using UnityEngine;

public class GameScene : MonoBehaviour, IEventReceiver<PlayerRespawnSignal>
{
    private void OnEnable()
    {
        EventBus.Register( this );
    }

    private void OnDisable()
    {
        EventBus.UnRegister( this );
    }

    public void OnEvent( PlayerRespawnSignal data )
    {
        Debug.Log( "プレイヤーが復活" );
    }
}