| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- /// <summary>
- /// Manages the death screen UI display and respawn button.
- /// Shows random death messages and handles respawn button clicks.
- /// </summary>
- public class DeathScreenUI : MonoBehaviour
- {
- public static DeathScreenUI Instance { get; private set; }
- [Header("UI References")]
- public GameObject deathPanel;
- public TextMeshProUGUI deathMessageText;
- public Button respawnButton;
- public CanvasGroup canvasGroup;
-
- [Header("Death Messages")]
- public string[] deathMessages = new string[]
- {
- "Your party has been defeated...",
- "Darkness consumes your party...",
- "The dungeon claims another group of heroes...",
- "Your quest has come to a tragic end..."
- };
-
- [Header("Settings")]
- public float fadeInDuration = 1f;
- public bool randomizeMessage = true;
- #region Initialization
- private void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- return;
- }
- Instance = this;
-
- if (deathPanel != null)
- deathPanel.SetActive(false);
-
- if (canvasGroup != null)
- canvasGroup.alpha = 0f;
- }
- private void Start()
- {
- ValidateReferences();
- BindButton();
- }
- private void ValidateReferences()
- {
- if (deathPanel == null)
- Debug.LogWarning("[DeathScreenUI] Death Panel not assigned in Inspector");
-
- if (respawnButton == null)
- Debug.LogWarning("[DeathScreenUI] Respawn Button not assigned in Inspector");
- }
- private void BindButton()
- {
- if (respawnButton != null)
- {
- respawnButton.onClick.AddListener(OnRespawnButtonClicked);
- }
- }
- #endregion
- #region Show/Hide
- public void Show()
- {
- if (deathPanel != null)
- deathPanel.SetActive(true);
-
- if (deathMessageText != null && randomizeMessage)
- {
- string message = deathMessages[Random.Range(0, deathMessages.Length)];
- deathMessageText.text = message;
- }
-
- if (canvasGroup != null)
- {
- StartCoroutine(FadeIn());
- }
- }
- public void Hide()
- {
- if (canvasGroup != null)
- {
- canvasGroup.alpha = 0f;
- }
-
- if (deathPanel != null)
- deathPanel.SetActive(false);
- }
- private System.Collections.IEnumerator FadeIn()
- {
- float elapsed = 0f;
- while (elapsed < fadeInDuration)
- {
- elapsed += Time.deltaTime;
- canvasGroup.alpha = Mathf.Lerp(0f, 1f, elapsed / fadeInDuration);
- yield return null;
- }
- canvasGroup.alpha = 1f;
- }
- #endregion
- #region Button Handlers
- private void OnRespawnButtonClicked()
- {
- var gameManagerObj = GameObject.Find("GameManager");
- if (gameManagerObj != null)
- {
- gameManagerObj.SendMessage("RespawnParty", SendMessageOptions.DontRequireReceiver);
- }
- else
- {
- Debug.LogError("[DeathScreenUI] GameManager not found!");
- }
- }
- #endregion
- }
|