using UnityEngine;
using UnityEngine.SceneManagement;
///
/// Disables HTrace WSGI while the additively-loaded Lobby scene is present, and re-enables it everywhere else
/// (Main-only, or the match map). HTraceWSGI lives permanently in the always-loaded Main scene, so it can't be
/// authored per-scene; this watcher toggles it from Unity's additive scene load/unload events (NGO loads the
/// gameplay scenes through the same SceneManager, so those events fire on every machine, host and client alike).
///
/// Authored on a plain GameObject in the always-loaded Main scene. It toggles the HTraceWSGI COMPONENT's `enabled`
/// (not the GameObject), so its own host stays alive and can turn the GI back on. HTraceWSGI.OnDisable/OnEnable
/// cleanly unregister/re-register the renderer feature, so flipping `enabled` fully stops and restarts the GI.
///
[DisallowMultipleComponent]
public class HTraceLobbyToggle : MonoBehaviour
{
[Tooltip("Name of the additively-loaded scene during which HTrace WSGI is disabled. Matches MatchManager.lobbySceneName.")]
[SerializeField] private string lobbySceneName = "Lobby";
private HTraceWSGI.Scripts.HTraceWSGI _gi;
private void Awake() => Resolve();
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.sceneUnloaded += OnSceneUnloaded;
Apply();
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
SceneManager.sceneUnloaded -= OnSceneUnloaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode) => Apply();
private void OnSceneUnloaded(Scene scene) => Apply();
private void Resolve()
{
// Include inactive: the HTrace object may have been toggled off by DirectionalLightForHTrace's one-frame
// re-init, and once we disable the component a plain search would still find it (component-level toggle),
// but caching once is cheaper than re-scanning per event.
if (!_gi) _gi = FindAnyObjectByType(FindObjectsInactive.Include);
}
private void Apply()
{
Resolve();
if (!_gi) return;
bool disableForLobby = IsLobbyLoaded();
if (_gi.enabled == !disableForLobby) return; // already in the desired state
_gi.enabled = !disableForLobby;
}
private bool IsLobbyLoaded()
{
for (int i = 0; i < SceneManager.sceneCount; i++)
{
Scene s = SceneManager.GetSceneAt(i);
if (s.isLoaded && s.name == lobbySceneName) return true;
}
return false;
}
}