using UnityEngine;
namespace Kill.UI.Components
{
///
/// 全局Loading组件
/// 调用 LoadingUI.Show() 显示,LoadingUI.Hide() 隐藏
///
public class LoadingUI : MonoBehaviour
{
public static LoadingUI Instance { get; private set; }
[Header("UI引用")]
public GameObject loadingPanel;
public Animation loadingAni;
private int showCount = 0;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
Hide();
}
///
/// 显示Loading
///
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();
}
}
///
/// 隐藏Loading
///
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);
}
}
///
/// 强制隐藏(动画回调用)
///
public void OnHideAnimationComplete()
{
if (showCount == 0)
loadingPanel.SetActive(false);
}
///
/// 强制隐藏
///
public static void ForceHide()
{
if (Instance == null) return;
Instance.showCount = 0;
Instance.loadingPanel.SetActive(false);
}
}
}