using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Collections; public class PortalController : MonoBehaviour { [Header("¿¬°áÇÒ UIµé")] public CanvasGroup fKeyCanvasGroup; // FŰÀÇ Åõ¸íµµ Á¶Àý¿ë public Image fadeImage; // °ËÀº ÆÐ³Î public Text loadingText; // ·Îµù ±ÛÀÚ [Header("¼³Á¤°ª")] public string nextSceneName = "Stage1"; // À̵¿ÇÒ ¾À À̸§ public float fadeSpeed = 1.0f; // ÆäÀÌµå ¼Óµµ private bool isPlayerNearby = false; private bool isLoadingStarted = false; void Start() { // ½ÃÀÛÇÒ ¶§ ¸ðµç UI Åõ¸íÇÏ°Ô ÃʱâÈ­ fKeyCanvasGroup.alpha = 0; fadeImage.color = new Color(0, 0, 0, 0); loadingText.color = new Color(1, 1, 1, 0); } // Ç÷¹À̾ ±Ùó¿¡ ¿À¸é FŰ ½º¸£¸¤ ³ªÅ¸³»±â private void OnTriggerEnter(Collider other) { if (other.CompareTag("Player") && !isLoadingStarted) { isPlayerNearby = true; StopAllCoroutines(); StartCoroutine(FadeUI(fKeyCanvasGroup, 1f, 0.5f)); } } // Ç÷¹À̾ ¸Ö¾îÁö¸é FŰ ½º¸£¸¤ »ç¶óÁö±â private void OnTriggerExit(Collider other) { if (other.CompareTag("Player")) { isPlayerNearby = false; StopAllCoroutines(); StartCoroutine(FadeUI(fKeyCanvasGroup, 0f, 0.5f)); } } void Update() { // Á¶°ÇÀÌ ¸ÂÀ¸¸é ½ºÅ×ÀÌÁö À̵¿ ½ÃÀÛ! if (isPlayerNearby && Input.GetKeyDown(KeyCode.F) && !isLoadingStarted) { StartCoroutine(TransitionSequence()); } } // È­¸é ¾ÏÀü -> ·Îµù -> À̵¿ ¿¬Ãâ IEnumerator TransitionSequence() { isLoadingStarted = true; // 1. FŰ ¸ÕÀú »ç¶óÁü yield return StartCoroutine(FadeUI(fKeyCanvasGroup, 0f, 0.3f)); // 2. È­¸éÀÌ Á¡Á¡ °Ë°Ô º¯ÇÔ (ÆäÀÌµå ¾Æ¿ô) float timer = 0; while (timer < 1f) { timer += Time.deltaTime * fadeSpeed; fadeImage.color = new Color(0, 0, 0, timer); yield return null; } // 3. ·Îµù ±ÛÀÚ ¼­¼­È÷ ³ªÅ¸³² timer = 0; while (timer < 1f) { timer += Time.deltaTime * 2f; loadingText.color = new Color(1, 1, 1, timer); yield return null; } // 4. Àá±ñ ´ë±â (·ÎµùÇϴ ô!) yield return new WaitForSeconds(1.5f); // 5. ´ÙÀ½ ½ºÅ×ÀÌÁö·Î ½¹! SceneManager.LoadScene(nextSceneName); } // UIÀÇ Åõ¸íµµ¸¦ ºÎµå·´°Ô ¹Ù²ãÁÖ´Â À¯Æ¿¸®Æ¼ ÄÚ·çÆ¾ IEnumerator FadeUI(CanvasGroup cg, float targetAlpha, float duration) { float startAlpha = cg.alpha; float time = 0; while (time < duration) { time += Time.deltaTime; cg.alpha = Mathf.Lerp(startAlpha, targetAlpha, time / duration); yield return null; } cg.alpha = targetAlpha; } }