ABOUT PORTFOLIO GAMES DEVLOG DOCS TOOLS CONTACT
HOME > DOCS > Unity / C# > 컴포넌트 간 결합도를 낮추는 제네릭 타입 기반 EventBus 시스템Generic Type-Based EventBus System for Component Decoupling
Unity / C# Intermediate Unity 2021.3+ / Unity 6

컴포넌트 간 결합도를 낮추는 제네릭 타입 기반 EventBus 시스템Generic Type-Based EventBus System for Component Decoupling

문자열 식별자나 직접 참조 없이 구조체 이벤트 타입만으로 구독과 발행을 처리하는 무할당(Zero-Allocation) 유니티 C# EventBus 아키텍처.A zero-allocation C# EventBus architecture in Unity that handles event subscription and publishing using struct types without string identifiers or direct references.

#Unity #C# #EventBus #Decoupling #Architecture
핵심 개념플레이어가 사망했을 때 UI, 사운드, 세이브, 업적 시스템이 서로를 직접 참조하면 컴포넌트 간 결합도가 높아집니다. PlayerDiedEvent라는 순수 데이터 구조체(struct)를 정의하고 이를 EventBus를 통해 브로드캐스트하면 송신자와 수신자가 명확히 분리(Decoupling)됩니다.
Core ConceptWhen UI, audio, save, and achievement systems directly reference one another upon player death, component coupling increases rapidly. Defining a pure data struct such as PlayerDiedEvent and broadcasting it through an EventBus cleanly decouples publishers from subscribers.

1. 타입 세이프 EventBus 구현1. Type-Safe EventBus Implementation

EventBus.cs
using System;
using System.Collections.Generic;

public interface IEvent { }

public static class EventBus<T> where T : struct, IEvent
{
    private static readonly HashSet<Action<T>> _listeners = new HashSet<Action<T>>();

    public static void Subscribe(Action<T> listener)
    {
        if (listener != null) _listeners.Add(listener);
    }

    public static void Unsubscribe(Action<T> listener)
    {
        if (listener != null) _listeners.Remove(listener);
    }

    public static void Raise(T evt)
    {
        foreach (var listener in _listeners)
        {
            listener.Invoke(evt);
        }
    }

    public static void Clear() => _listeners.Clear();
}

2. 이벤트 정의 및 사용법2. Defining and Using Events

GameplayEvents.cs
// 1. 이벤트 데이터 정의
public readonly struct PlayerDamagedEvent : IEvent
{
    public readonly int Amount;
    public readonly int CurrentHp;

    public PlayerDamagedEvent(int amount, int currentHp)
    {
        Amount = amount;
        CurrentHp = currentHp;
    }
}

// 2. 수신처 (UI 컴포넌트)
public class HealthBarUI : UnityEngine.MonoBehaviour
{
    private void OnEnable() => EventBus<PlayerDamagedEvent>.Subscribe(OnPlayerDamaged);
    private void OnDisable() => EventBus<PlayerDamagedEvent>.Unsubscribe(OnPlayerDamaged);

    private void OnPlayerDamaged(PlayerDamagedEvent e)
    {
        UpdateHpGauge(e.CurrentHp);
    }
}

// 3. 발송처 (플레이어 컴포넌트)
// EventBus<PlayerDamagedEvent>.Raise(new PlayerDamagedEvent(15, 85));
GameplayEvents.cs
// 1. Define event data
public readonly struct PlayerDamagedEvent : IEvent
{
    public readonly int Amount;
    public readonly int CurrentHp;

    public PlayerDamagedEvent(int amount, int currentHp)
    {
        Amount = amount;
        CurrentHp = currentHp;
    }
}

// 2. Subscriber (UI component)
public class HealthBarUI : UnityEngine.MonoBehaviour
{
    private void OnEnable() => EventBus<PlayerDamagedEvent>.Subscribe(OnPlayerDamaged);
    private void OnDisable() => EventBus<PlayerDamagedEvent>.Unsubscribe(OnPlayerDamaged);

    private void OnPlayerDamaged(PlayerDamagedEvent e)
    {
        UpdateHpGauge(e.CurrentHp);
    }
}

// 3. Publisher (Player component)
// EventBus<PlayerDamagedEvent>.Raise(new PlayerDamagedEvent(15, 85));

3. 실무 주의점3. Practical Considerations

  • 구독 해제 누락 방지: OnEnable에서 구독했다면 반드시 OnDisable에서 해제해야 합니다. 해제하지 않으면 이미 파괴된 컴포넌트의 델리게이트가 남아 MissingReferenceException이 발생합니다.
  • 구조체(struct) 이벤트 사용: 이벤트를 클래스가 아닌 readonly struct로 선언하여 힙 할당(Garbage Collection)을 0으로 유지합니다.
  • Prevent Missing Unsubscriptions: Any subscription registered in OnEnable must be removed in OnDisable. Failing to unsubscribe leaves delegates pointing to destroyed components, resulting in a MissingReferenceException.
  • Use Struct Events: Declare events as readonly struct rather than classes to avoid heap allocations and prevent garbage collection overhead.

제네릭 구조체 기반의 EventBus를 도입하면 런타임 가비지 컬렉션 부담 없이 시스템 간 의존성을 낮추고 모듈 단위의 유지보수를 수행할 수 있습니다.

Adopting a generic struct-based EventBus reduces inter-system dependencies and supports modular maintenance without introducing runtime garbage collection overhead.

참고자료 & 관련 링크