Skip to content

MuharremTozan/Agentic-Duel / .agent/skills/unity-event-bus

unity-event-bus

Verified

Advanced code-driven event bus with reflection-based bootstrapping. Provides zero-setup global messaging.

assets/code/EventBusBootstrapper.cs.txt

View on GitHub
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Standard.Events.Bus {
    /// <summary>
    /// Bootstraps the event bus system on game start.
    /// Finds all event types and ensures buses are initialized and cleared properly.
    /// </summary>
    public static class EventBusBootstrapper {
        private static List<Type> _eventTypes;

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
        public static void Initialize() {
            _eventTypes = new List<Type>();
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();
            
            foreach (var assembly in assemblies) {
                var types = assembly.GetTypes();
                foreach (var type in types) {
                    if (typeof(IEvent).IsAssignableFrom(type) && !type.IsInterface) {
                        _eventTypes.Add(type);
                    }
                }
            }
            
            Debug.Log($"[EventBus] Status: Success | Initialized {_eventTypes.Count} event buses.");
        }

#if UNITY_EDITOR
        [InitializeOnLoadMethod]
        private static void EditorInitialize() {
            EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
        }

        private static void OnPlayModeStateChanged(PlayModeStateChange state) {
            if (state == PlayModeStateChange.ExitingPlayMode) {
                ClearAllBuses();
            }
        }
#endif

        public static void ClearAllBuses() {
            if (_eventTypes == null) return;

            foreach (var type in _eventTypes) {
                var busType = typeof(EventBus<>).MakeGenericType(type);
                var clearMethod = busType.GetMethod("Clear", BindingFlags.Static | BindingFlags.Public);
                clearMethod?.Invoke(null, null);
            }
            
            Debug.Log("[EventBus] Status: Success | Cleared all event buses.");
        }
    }
}
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall