using System; using System.Text.RegularExpressions; using Photon.Pun; using UnityEngine; namespace BlockSpace.Worlds { [DisallowMultipleComponent] public sealed class LobbyOnlyEnabler : MonoBehaviourPunCallbacks { [Tooltip("Objects that should only be enabled while in the Lobby world (Lobby or Lobby|###).")] [SerializeField] private GameObject[] targets; [Tooltip("If true, toggles this object's children too (but keeps this GameObject active so the script still runs).")] [SerializeField] private bool includeSelf; [Tooltip("World base name considered the lobby.")] [SerializeField] private string lobbyWorldName = "Lobby"; [Tooltip("How often to check the lobby status in seconds.")] [SerializeField] private float checkInterval = 0.5f; private void Awake() { InvokeRepeating("Apply", 0f, checkInterval); Apply(); } private void Apply() { bool active = IsInLobbyWorld(); if (includeSelf) { for (int i = 0; i < base.transform.childCount; i++) { Transform child = base.transform.GetChild(i); if (child != null) { child.gameObject.SetActive(active); } } } if (targets == null) { return; } for (int j = 0; j < targets.Length; j++) { GameObject gameObject = targets[j]; if (!(gameObject == null) && !(gameObject == base.gameObject)) { gameObject.SetActive(active); } } } private bool IsInLobbyWorld() { if (!PhotonNetwork.InRoom || PhotonNetwork.CurrentRoom == null) { return false; } string baseWorldName = GetBaseWorldName(PhotonNetwork.CurrentRoom.Name ?? string.Empty); string text = (lobbyWorldName ?? string.Empty).Trim(); if (string.IsNullOrEmpty(text)) { text = "Lobby"; } return string.Equals(baseWorldName, text, StringComparison.OrdinalIgnoreCase); } private static string GetBaseWorldName(string roomName) { string text = (roomName ?? string.Empty).Trim(); if (string.IsNullOrEmpty(text)) { return text; } int num = text.LastIndexOf('|'); if (num <= 0) { return text; } string text2 = text.Substring(num + 1); if (text2.Length == 3 && Regex.IsMatch(text2, "^[A-Za-z0-9]{3}$")) { return text.Substring(0, num); } return text; } } }