105 lines
2.5 KiB
C#
105 lines
2.5 KiB
C#
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>=30)
|
||
{
|
||
ForceHide();
|
||
waitTime=0;
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|