62 lines
2.0 KiB
C#
62 lines
2.0 KiB
C#
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public static class UI_SelectSong_AutoPlayToggleBinder
|
|
{
|
|
private const string TargetSceneName = "selectYourSongFirst";
|
|
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
|
|
private static void Init()
|
|
{
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
}
|
|
|
|
private static void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
if (scene.name != TargetSceneName)
|
|
return;
|
|
|
|
TryBind(scene);
|
|
}
|
|
|
|
private static void TryBind(Scene scene)
|
|
{
|
|
// Ensure we have the latest persisted value (important when entering from editor play).
|
|
GameConfig.LoadPrefs();
|
|
|
|
// Prefer the user's specified hierarchy if present (active only).
|
|
GameObject go =
|
|
SceneObjectLookupCache.Find("rightInfos/difficultyBtnInfos/difficultyBtnInfos/select_Auto") ??
|
|
SceneObjectLookupCache.Find("rightInfos/difficultyBtnInfos/select_Auto");
|
|
|
|
if (go == null)
|
|
{
|
|
// Fallback: search by name in this scene (includes inactive objects).
|
|
var transforms = Object.FindObjectsByType<Transform>(FindObjectsInactive.Include, FindObjectsSortMode.None);
|
|
for (int i = 0; i < transforms.Length; i++)
|
|
{
|
|
var t = transforms[i];
|
|
if (t == null) continue;
|
|
if (t.gameObject.scene != scene) continue;
|
|
if (t.name != "select_Auto") continue;
|
|
go = t.gameObject;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (go == null)
|
|
{
|
|
if (GameConfig.verboseLogs)
|
|
Debug.LogWarning("[AutoPlay] select_Auto button not found in selectYourSongFirst scene. Autoplay toggle UI will be unavailable.");
|
|
return;
|
|
}
|
|
|
|
var toggle = go.GetComponent<UI_SelectSong_AutoPlayToggle>();
|
|
if (toggle == null)
|
|
toggle = go.AddComponent<UI_SelectSong_AutoPlayToggle>();
|
|
|
|
toggle.TryAutoWire();
|
|
}
|
|
}
|