105 lines
2.5 KiB
C#
Raw Normal View History

2026-04-16 14:57:19 +08:00
using UnityEngine;
namespace Kill.UI.Components
{
/// <summary>
/// 全局Loading组件
/// 调用 LoadingUI.Show() 显示LoadingUI.Hide() 隐藏
/// </summary>
public class LoadingUI : MonoBehaviour
{
public static LoadingUI Instance { get; private set; }
[Header("UI引用")]
public GameObject loadingPanel;
public Animation loadingAni;
2026-05-18 08:42:33 +08:00
float waitTime=0;
2026-04-16 14:57:19 +08:00
private int showCount = 0;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
Hide();
}
/// <summary>
/// 显示Loading
/// </summary>
public static void Show()
{
if (Instance == null) return;
Instance.showCount++;
if (Instance.showCount == 1)
{
Instance.loadingPanel.SetActive(true);
if (Instance.loadingAni != null)
Instance.loadingAni.Play();
}
}
/// <summary>
/// 隐藏Loading
/// </summary>
public static void Hide()
{
if (Instance == null) return;
Instance.showCount = Mathf.Max(0, Instance.showCount - 1);
if (Instance.showCount == 0)
{
if (Instance.loadingAni != null)
Instance.loadingAni.Stop();
Instance.loadingPanel.SetActive(false);
}
}
/// <summary>
/// 强制隐藏(动画回调用)
/// </summary>
public void OnHideAnimationComplete()
{
if (showCount == 0)
loadingPanel.SetActive(false);
}
/// <summary>
/// 强制隐藏
/// </summary>
public static void ForceHide()
{
if (Instance == null) return;
Instance.showCount = 0;
Instance.loadingPanel.SetActive(false);
}
2026-05-18 08:42:33 +08:00
private void Update()
{
if(showCount>0)
{
waitTime+=Time.deltaTime;
}
else
{
waitTime=0;
}
if(waitTime>=10)
{
ForceHide();
waitTime=0;
}
}
2026-04-16 14:57:19 +08:00
}
}