using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class RandomSceneRouteManager : MonoBehaviour { public static RandomSceneRouteManager Instance; [Header("·£´ýÀ¸·Î À̵¿ÇÒ ¹æ Scene À̸§µé")] [SerializeField] private string[] roomSceneNames; [Header("¸ðµç ¹æ ¹æ¹® ÈÄ À̵¿ÇÒ ¸¶Áö¸· Scene À̸§")] [SerializeField] private string finalSceneName; private readonly HashSet visitedScenes = new HashSet(); private string nextSceneCode1 = ""; private string nextSceneCode2 = ""; private string nextSceneName1 = ""; private string nextSceneName2 = ""; private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); SceneManager.sceneLoaded += OnSceneLoaded; } else { Destroy(gameObject); } } private void OnDestroy() { if (Instance == this) { SceneManager.sceneLoaded -= OnSceneLoaded; } } private void OnSceneLoaded(Scene scene, LoadSceneMode mode) { string sceneName = scene.name.Trim(); if (IsRoomScene(sceneName)) { visitedScenes.Add(sceneName); Debug.Log("ÇöÀç ¹æ ¹æ¹® ó¸®: " + sceneName); } } public void PrepareNextSceneChoices() { string currentSceneName = SceneManager.GetActiveScene().name.Trim(); if (IsRoomScene(currentSceneName)) { visitedScenes.Add(currentSceneName); } List candidates = new List(); foreach (string sceneName in roomSceneNames) { if (string.IsNullOrWhiteSpace(sceneName)) continue; string cleanSceneName = sceneName.Trim(); // ÀÚ±â Àڽйæ Á¦¿Ü if (cleanSceneName == currentSceneName) continue; // ÀÌ¹Ì ¹æ¹®ÇÑ ¹æ Á¦¿Ü if (visitedScenes.Contains(cleanSceneName)) continue; candidates.Add(cleanSceneName); } Shuffle(candidates); nextSceneCode1 = ""; nextSceneCode2 = ""; nextSceneName1 = ""; nextSceneName2 = ""; if (candidates.Count >= 1) { nextSceneCode1 = candidates[0]; nextSceneName1 = GetDisplayName(nextSceneCode1); } if (candidates.Count >= 2) { nextSceneCode2 = candidates[1]; nextSceneName2 = GetDisplayName(nextSceneCode2); } // ³²Àº ¹æÀÌ Çϳªµµ ¾øÀ¸¸é ¸¶Áö¸· ¾ÀÀ¸·Î º¸³¿ if (candidates.Count == 0) { if (!string.IsNullOrWhiteSpace(finalSceneName)) { nextSceneCode1 = finalSceneName.Trim(); nextSceneName1 = GetDisplayName(nextSceneCode1); } else { Debug.LogWarning("À̵¿ °¡´ÉÇÑ ´ÙÀ½ ¾ÀÀÌ ¾ø½À´Ï´Ù."); } } // ³²Àº ¹æÀÌ Çϳª»ÓÀ̸é 1¹øÀº ³²Àº ¹æ, 2¹øÀº ¸¶Áö¸· ¾ÀÀ¸·Î °¡´É if (candidates.Count == 1) { if (!string.IsNullOrWhiteSpace(finalSceneName)) { nextSceneCode2 = finalSceneName.Trim(); nextSceneName2 = GetDisplayName(nextSceneCode2); } } Debug.Log("ÇöÀç ¾À: " + currentSceneName); Debug.Log("¹æ¹®ÇÑ ¾À ¼ö: " + visitedScenes.Count); Debug.Log("¼±ÅÃÁö 1: " + nextSceneName1 + " / " + nextSceneCode1); Debug.Log("¼±ÅÃÁö 2: " + nextSceneName2 + " / " + nextSceneCode2); } public string GetNextSceneName1() { return nextSceneName1; } public string GetNextSceneCode1() { return nextSceneCode1; } public string GetNextSceneName2() { return nextSceneName2; } public string GetNextSceneCode2() { return nextSceneCode2; } public string GetNextSceneName() { PrepareNextSceneChoices(); if (!string.IsNullOrEmpty(nextSceneCode1)) return nextSceneCode1; return string.Empty; } public void RequestRandomSceneChange() { string nextSceneName = GetNextSceneName(); if (string.IsNullOrEmpty(nextSceneName)) { Debug.LogWarning("À̵¿ÇÒ ´ÙÀ½ ¾À À̸§ÀÌ ºñ¾îÀÖ½À´Ï´Ù."); return; } SceneLoadManager loadManager = FindFirstObjectByType(); if (loadManager != null) { Debug.Log("·£´ý ¾À À̵¿ ¿äû: " + nextSceneName); loadManager.RequestSceneChange(nextSceneName); } else { Debug.LogWarning("SceneLoadManager°¡ ¾ø¾î¼­ ¹Ù·Î ¾À À̵¿ÇÕ´Ï´Ù: " + nextSceneName); SceneManager.LoadScene(nextSceneName); } } public void ResetRoute() { visitedScenes.Clear(); nextSceneCode1 = ""; nextSceneCode2 = ""; nextSceneName1 = ""; nextSceneName2 = ""; Debug.Log("·£´ý ¹æ ¹æ¹® ±â·Ï ÃʱâÈ­"); } private void Shuffle(List list) { for (int i = 0; i < list.Count; i++) { int randomIndex = Random.Range(i, list.Count); string temp = list[i]; list[i] = list[randomIndex]; list[randomIndex] = temp; } } private string GetDisplayName(string sceneName) { switch (sceneName) { case "blackjack": return "ºí·¢Àè"; case "CatsRoom": return "Ĺ·ë"; case "MazeRoom": return "¹Ì·Î¹æ"; case "Cave_Test_2": return "µ¿±¼¹æ"; case "Gepto first": return "Á¦ÆäÅä"; default: return sceneName; } } private bool IsRoomScene(string sceneName) { foreach (string roomSceneName in roomSceneNames) { if (string.IsNullOrWhiteSpace(roomSceneName)) continue; if (roomSceneName.Trim() == sceneName.Trim()) return true; } return false; } }