79 lines
2.0 KiB
C#
79 lines
2.0 KiB
C#
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<UISoundRouter>();
|
|
Object.DontDestroyOnLoad(go);
|
|
}
|
|
|
|
var config = Resources.Load<UISoundConfig>(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<Selectable>(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<Selectable>(true);
|
|
foreach (var sel in selectables)
|
|
{
|
|
Register(sel);
|
|
}
|
|
}
|
|
|
|
public static void Register(Selectable sel)
|
|
{
|
|
if (sel == null) return;
|
|
if (sel.GetComponent<UIButtonInteraction>() == null)
|
|
{
|
|
sel.gameObject.AddComponent<UIButtonInteraction>();
|
|
}
|
|
}
|
|
}
|