using UnityEngine; using UnityEngine.UI; using TMPro; 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; 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() { } private void BindButton() { if (respawnButton != null) { respawnButton.onClick.AddListener(OnRespawnButtonClicked); } } 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; } private void OnRespawnButtonClicked() { var gameManagerObj = GameObject.Find("GameManager"); if (gameManagerObj != null) { gameManagerObj.SendMessage("RespawnParty", SendMessageOptions.DontRequireReceiver); } } }