using System; using TMPro; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; /// /// 通用确认弹窗组件 /// 用于二次确认操作,如删除、修改等重要操作 /// public class ConfirmDialog : BasePanel { [Header("UI组件")] public GameObject DialogRoot; // 弹窗根对象 public TextMeshProUGUI TitleText; // 标题文本 public TextMeshProUGUI MessageText; // 消息内容文本 public Button ConfirmButton; // 确认按钮 public Button CancelButton; // 取消按钮 public TextMeshProUGUI ConfirmButtonText; // 确认按钮文本 public TextMeshProUGUI CancelButtonText; // 取消按钮文本 [Header("样式设置")] public Color WarningColor = Color.red; // 警告色 public Color NormalColor = Color.white; // 正常色 public Color DangerButtonColor = Color.red; // 危险按钮颜色 public Color NormalButtonColor = Color.green; // 正常按钮颜色 private Action _onConfirm; private Action _onCancel; private GameObject _modalBlocker; public override void Init() { InitializeButtons(); // HideDialog(); } protected override void Awake() { base.Awake(); } private void OnDestroy() { DestroyModalBlocker(); } private void InitializeButtons() { if (ConfirmButton != null) { ConfirmButton.onClick.AddListener(OnConfirmClicked); } if (CancelButton != null) { CancelButton.onClick.AddListener(OnCancelClicked); } } /// /// 显示确认对话框 /// /// 标题 /// 消息内容 /// 确认回调 /// 取消回调 /// 确认按钮文本 /// 取消按钮文本 /// 是否为警告类型 /// 是否显示取消按钮 public static void Show(string title, string message, Action onConfirm, Action onCancel = null, string confirmText = "确认", string cancelText = "取消", bool isWarning = false, bool showCancelButton = true) { if (CustomKeyboardManager.Instance != null && CustomKeyboardManager.Instance.IsKeyboardShowing()) { CustomKeyboardManager.Instance.HideKeyboard(); } // 使用新的UI管理器显示对话框 var dialog = EnhancedUIManager.Instance.ShowPanel(UIPanelType.Dialog, true, false); if (dialog != null) { dialog.ShowDialog(title, message, onConfirm, onCancel, confirmText, cancelText, isWarning, showCancelButton); } else { Debug.LogError("Failed to create ConfirmDialog!"); } } private void ShowDialog(string title, string message, Action onConfirm, Action onCancel, string confirmText, string cancelText, bool isWarning, bool showCancelButton) { _onConfirm = onConfirm; _onCancel = onCancel; // 设置文本内容 if (TitleText != null) { TitleText.text = title; TitleText.color = isWarning ? WarningColor : NormalColor; } if (MessageText != null) { MessageText.text = message; } if (ConfirmButtonText != null) { ConfirmButtonText.text = confirmText; } if (CancelButtonText != null) { CancelButtonText.text = cancelText; } // 控制取消按钮的显示/隐藏 if (CancelButton != null) { CancelButton.gameObject.SetActive(showCancelButton); } // 设置按钮样式 if (ConfirmButton != null) { var buttonImage = ConfirmButton.GetComponent(); if (buttonImage != null) { buttonImage.color = isWarning ? DangerButtonColor : NormalButtonColor; } } // 显示弹窗 EnsureModalBlocker(); transform.SetAsLastSibling(); if (DialogRoot != null) { DialogRoot.SetActive(true); DialogRoot.transform.SetAsLastSibling(); } else { gameObject.SetActive(true); } } private void EnsureModalBlocker() { if (_modalBlocker != null) { _modalBlocker.SetActive(true); _modalBlocker.transform.SetAsFirstSibling(); return; } _modalBlocker = new GameObject("ModalBlocker", typeof(RectTransform), typeof(Image), typeof(EventTrigger)); Transform blockerParent = transform.parent != null ? transform.parent : transform; _modalBlocker.transform.SetParent(blockerParent, false); _modalBlocker.transform.SetSiblingIndex(Mathf.Max(0, transform.GetSiblingIndex())); var rectTransform = _modalBlocker.GetComponent(); rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMax = Vector2.one; rectTransform.offsetMin = Vector2.zero; rectTransform.offsetMax = Vector2.zero; var image = _modalBlocker.GetComponent(); image.color = new Color(0f, 0f, 0f, 0.35f); image.raycastTarget = true; var trigger = _modalBlocker.GetComponent(); var entry = new EventTrigger.Entry { eventID = EventTriggerType.PointerClick }; entry.callback.AddListener(_ => Debug.Log("ConfirmDialog: 模态确认框打开中,已拦截背景点击")); trigger.triggers.Add(entry); } private void OnConfirmClicked() { _onConfirm?.Invoke(); CloseDialog(); } private void OnCancelClicked() { _onCancel?.Invoke(); CloseDialog(); } private void CloseDialog() { // 清理回调 DestroyModalBlocker(); EnhancedUIManager.Instance.HidePanel(true); _onConfirm = null; _onCancel = null; } private void DestroyModalBlocker() { if (_modalBlocker == null) { return; } Destroy(_modalBlocker); _modalBlocker = null; } private void HideDialog() { if (_modalBlocker != null) { _modalBlocker.SetActive(false); } if (DialogRoot != null) { DialogRoot.SetActive(false); } else { gameObject.SetActive(false); } // 清理回调 _onConfirm = null; _onCancel = null; } /// /// 显示删除确认对话框 /// public static void ShowDeleteConfirm(string itemName, Action onConfirm, Action onCancel = null) { Show($"删除确认", $"确定要删除 '{itemName}' 吗?\n此操作无法撤销。", onConfirm, onCancel, "删除", "取消", true); } /// /// 显示保存确认对话框 /// public static void ShowSaveConfirm(string message, Action onConfirm, Action onCancel = null) { Show("保存确认", message, onConfirm, onCancel, "保存", "取消", false); } /// /// 显示操作确认对话框 /// public static void ShowActionConfirm(string action, string target, Action onConfirm, Action onCancel = null) { Show($"{action}确认", $"确定要{action} '{target}' 吗?", onConfirm, onCancel, action, "取消", true); } }