Files
5.24BuYu/Assets/MyGame/aa/UI/DefaultPanel/TiShi/TiShi.cs
T
2026-07-10 20:01:07 +08:00

76 lines
1.9 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;
private Vector3 _standardSize;
private bool _closing;
private void Awake()
{
_standardSize = transform.GetChild(0).localScale;
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>
public void Init(string text, Action onConfirmAction = null)
{
action = onConfirmAction;
if (tmpText_TiShiLabel != null)
tmpText_TiShiLabel.text = text;
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();
PlayHideThenDestroy();
}
/// <summary>关闭按钮:直接关闭,不触发回调</summary>
public void Close()
{
if (_closing) return;
_closing = true;
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);
};
}
}