wirm/Game/Assets/Scripts/CanvasFade.cs

53 lines
1.2 KiB
C#
Raw Normal View History

/*
Author : Wai Lam
2025-02-13 15:11:41 +08:00
Date : 13/2/2025
Description : Counting days
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
2025-02-13 15:11:41 +08:00
using TMPro;
using System;
public class CanvasFade : MonoBehaviour
{
2025-02-13 15:11:41 +08:00
public CanvasGroup dayPanel; // Assign the UI Panel
public TextMeshProUGUI dayText; // Assign the TextMeshPro UI
public static event Action OnDayPanelHidden;
private void Start()
{
2025-02-13 15:11:41 +08:00
if (GameManager.Instance != null)
{
2025-02-13 15:11:41 +08:00
int dayNumber = GameManager.Instance.currentDay;
dayText.text = "Day " + dayNumber;
}
2025-02-13 15:11:41 +08:00
StartCoroutine(FadeOutPanel());
}
2025-02-13 15:11:41 +08:00
private IEnumerator FadeOutPanel()
{
2025-02-13 15:11:41 +08:00
dayPanel.alpha = 1f;
dayPanel.gameObject.SetActive(true);
2025-02-11 15:31:52 +08:00
2025-02-13 15:11:41 +08:00
yield return new WaitForSeconds(3f); // Display for 3 seconds
2025-02-11 15:31:52 +08:00
2025-02-13 15:11:41 +08:00
float fadeDuration = 1.5f;
float elapsedTime = 0f;
2025-02-11 15:31:52 +08:00
2025-02-13 15:11:41 +08:00
while (elapsedTime < fadeDuration)
{
elapsedTime += Time.deltaTime;
dayPanel.alpha = Mathf.Lerp(1f, 0f, elapsedTime / fadeDuration);
yield return null;
}
2025-02-13 15:11:41 +08:00
dayPanel.gameObject.SetActive(false);
OnDayPanelHidden?.Invoke();
2025-02-11 15:31:52 +08:00
}
}