2026-05-18 08:42:33 +08:00

105 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
float waitTime=0;
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);
}
private void Update()
{
if(showCount>0)
{
waitTime+=Time.deltaTime;
}
else
{
waitTime=0;
}
if(waitTime>=10)
{
ForceHide();
waitTime=0;
}
}
}
}