99 lines
3.1 KiB
C#
99 lines
3.1 KiB
C#
using System;
|
||
using DG.Tweening;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
public class TiShi : MonoBehaviour
|
||
{
|
||
public Button btn_Close;
|
||
public Button btn_QueRen;
|
||
public TextMeshProUGUI tmpText_TiShiLabel;
|
||
|
||
public Action action;
|
||
|
||
/// <summary>关闭事件回调(点确认或点X都会触发,用于恢复外部状态)</summary>
|
||
public Action onCloseAction;
|
||
|
||
private Vector3 _standardSize;
|
||
private bool _closing;
|
||
|
||
private void Awake()
|
||
{
|
||
_standardSize = transform.GetChild(0).localScale;
|
||
UIClickSound.AttachToAll(gameObject); // ⭐ 弹窗是独立预制体(非 UIBase 面板),这里手动补按钮点击音效
|
||
if (btn_Close != null)
|
||
btn_Close.onClick.AddListener(Close);
|
||
if (btn_QueRen != null)
|
||
btn_QueRen.onClick.AddListener(Confirm);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化提示文字,并播放弹窗出现动画。
|
||
/// </summary>
|
||
/// <param name="text">提示内容</param>
|
||
/// <param name="onConfirmAction">点击确认按钮时的回调(可选)</param>
|
||
/// <param name="showClose">是否显示关闭按钮(默认 true)</param>
|
||
public void Init(string text, Action onConfirmAction = null, bool showClose = true)
|
||
{
|
||
Init(text, onConfirmAction, null, showClose);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化提示文字,并注册关闭事件回调。
|
||
/// 关闭事件在点确认或点X时都会触发(先于销毁),用于恢复外部可弹窗状态。
|
||
/// </summary>
|
||
/// <param name="text">提示内容</param>
|
||
/// <param name="onConfirmAction">点击确认按钮时的回调(可选)</param>
|
||
/// <param name="onClose">关闭事件回调(点确认或点X都触发)</param>
|
||
/// <param name="showClose">是否显示关闭按钮(默认 true)</param>
|
||
public void Init(string text, Action onConfirmAction, Action onClose, bool showClose)
|
||
{
|
||
action = onConfirmAction;
|
||
onCloseAction = onClose;
|
||
if (tmpText_TiShiLabel != null)
|
||
tmpText_TiShiLabel.text = text;
|
||
if (btn_Close != null)
|
||
btn_Close.gameObject.SetActive(showClose);
|
||
PlayAppear();
|
||
}
|
||
|
||
private void PlayAppear()
|
||
{
|
||
var t = transform.GetChild(0);
|
||
t.localScale = Vector3.zero;
|
||
t.DOKill();
|
||
t.DOScale(_standardSize, 0.55f).SetEase(Ease.OutElastic, 0.7f, 0.35f);
|
||
}
|
||
|
||
/// <summary>确认按钮:触发回调后关闭</summary>
|
||
public void Confirm()
|
||
{
|
||
if (_closing) return;
|
||
_closing = true;
|
||
action?.Invoke();
|
||
onCloseAction?.Invoke();
|
||
PlayHideThenDestroy();
|
||
}
|
||
|
||
/// <summary>关闭按钮:直接关闭,触发关闭事件回调</summary>
|
||
public void Close()
|
||
{
|
||
if (_closing) return;
|
||
_closing = true;
|
||
onCloseAction?.Invoke();
|
||
PlayHideThenDestroy();
|
||
}
|
||
|
||
private void PlayHideThenDestroy()
|
||
{
|
||
var t = transform.GetChild(0);
|
||
t.DOKill();
|
||
t.DOScale(0, 0.3f).SetEase(Ease.InBack).onComplete += () =>
|
||
{
|
||
if (this != null && gameObject != null)
|
||
Destroy(gameObject);
|
||
};
|
||
}
|
||
}
|