using UnityEngine; using System.Collections; // 'Coroutine' 기능을 쓰기 위해 꼭 필요해요! public class AreaTrigger : MonoBehaviour { [Header("AreaBanner에 추가한 Canvas Group을 연결하세요")] public CanvasGroup bannerCanvasGroup; [Header("설정")] public float fadeDuration = 1.0f; // 나타나는 데 걸리는 시간 (초) public float displayTime = 3.0f; // 다 나타난 뒤 유지되는 시간 private bool isShown = false; // 한 번만 나타나게 하고 싶을 때 사용 private void OnTriggerEnter(Collider other) { // 플레이어가 들어왔고, 아직 보여준 적이 없다면 if (other.CompareTag("Player") && !isShown) { isShown = true; // 다시 안 나오게 설정 (원치 않으면 이 줄 삭제) StartCoroutine(FadeInEffect()); } } // 서서히 나타나게 하는 특수 함수 (Coroutine) IEnumerator FadeInEffect() { // 1. 초기 투명도를 0으로 설정하고 오브젝트를 켭니다. bannerCanvasGroup.alpha = 0; bannerCanvasGroup.gameObject.SetActive(true); // 2. 투명도(Alpha)를 0에서 1까지 서서히 올립니다. float timer = 0f; while (timer < fadeDuration) { timer += Time.deltaTime; bannerCanvasGroup.alpha = Mathf.Lerp(0, 1, timer / fadeDuration); yield return null; // 다음 프레임까지 대기 } bannerCanvasGroup.alpha = 1; // 확실하게 1로 고정 // 3. 지정된 시간만큼 대기합니다. yield return new WaitForSeconds(displayTime); // 4. (보너스) 다시 서서히 사라지게 하기 (페이드 아웃) timer = 0f; while (timer < fadeDuration) { timer += Time.deltaTime; bannerCanvasGroup.alpha = Mathf.Lerp(1, 0, timer / fadeDuration); yield return null; } bannerCanvasGroup.alpha = 0; bannerCanvasGroup.gameObject.SetActive(false); } }