using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public static class UISystemBootstrap { private const string defaultConfigPath = "UISoundConfig_Default"; [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] static void Init() { CreateRouterIfNeeded(); RegisterAll(); SceneManager.sceneLoaded += OnSceneLoaded; } static void OnSceneLoaded(Scene scene, LoadSceneMode mode) { RegisterAll(); } static void CreateRouterIfNeeded() { UISoundRouter router; if (UISoundRouter.Instance != null) { router = UISoundRouter.Instance; } else { GameObject go = new GameObject("UISoundRouter"); router = go.AddComponent(); Object.DontDestroyOnLoad(go); } var config = Resources.Load(defaultConfigPath); if (config != null) { router.SetConfig(config); } else { Debug.LogWarning("UISoundConfig not found in Resources at: " + defaultConfigPath); } } public static void RegisterAll() { Debug.Log("RegisterButtons Called"); // Find all Selectable objects (includes Button, Toggle, Slider, etc.) Selectable[] selectables = Object.FindObjectsByType(FindObjectsInactive.Include, FindObjectsSortMode.None); foreach (var sel in selectables) { Register(sel); } } public static void RegisterHierarchy(GameObject root) { if (root == null) return; Selectable[] selectables = root.GetComponentsInChildren(true); foreach (var sel in selectables) { Register(sel); } } public static void Register(Selectable sel) { if (sel == null) return; if (sel.GetComponent() == null) { sel.gameObject.AddComponent(); } } }