105 lines
2.9 KiB
C#
105 lines
2.9 KiB
C#
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;
|
|
}
|
|
} |