Files
MinFt/Client/Assets/Scripts/UI/LoadingScreen.cs
2026-04-27 12:07:32 +08:00

269 lines
6.2 KiB
C#

using System.Collections;
using UnityEngine;
using GameCore;
using asap.core;
using Game;
using TMPro;
using System.Threading.Tasks;
public class LoadingScreen : MonoBehaviour
{
public LoadingProgressBar progressBar;
public TMP_Text downloadSzie;
public TMP_Text loadingInfo;
public TMP_Text versionInfo;
static LoadingScreen _instance;
public static async Task<LoadingScreen> Show(float progress = 0)
{
GameDebug.Log("[LoadingScreen]Show");
await ShowInit(progress);
return _instance;
}
private static async Task ShowInit(float progress = 0)
{
SetLoadingInfo("");
if (UIManager.Instance != null)
{
if (_instance == null)
{
GameObject loading = await UIManager.Instance.GetUIAsync(UITypes.FishingLoginPanel);
if (loading != null)
{
_instance = loading.GetComponent<LoadingScreen>();
}
}
if (_instance != null)
{
ShowProgressBar();
SetProgress(progress);
UIManager.Instance.SetAlwaysTopSibling(_instance.transform);
_instance.gameObject.SetActiveAsNeed(true);
}
else
{
GameDebug.LogError("[LoadingScreen]Show: Fail to get loading screen!");
}
}
else
{
GameDebug.LogError("[LoadingScreen]Show: UIManager doesn't exist, fail to get loading screen!");
}
}
public static void Hide()
{
GameDebug.Log("[LoadingScreen]Show");
if (_instance != null)
{
_instance.StopAllCoroutines();
if (UIManager.Instance != null)
{
UIManager.Instance.HideUI(UITypes.FishingLoginPanel);
}
}
}
public static bool isShowing()
{
bool is_showing = false;
if (_instance != null && _instance.gameObject.activeSelf)
{
is_showing = true;
}
return is_showing;
}
public static bool isProgressShowing()
{
bool is_showing = false;
if (_instance != null &&
_instance.gameObject.activeSelf &&
_instance.progressBar.gameObject.activeSelf)
{
is_showing = true;
}
return is_showing;
}
public static bool isComplete()
{
bool is_complete = true;
if (_instance != null)
{
if (Mathf.Approximately(_instance.progressBar.progress, 1f))
{
is_complete = true;
}
else
{
is_complete = false;
}
}
return is_complete;
}
public static void SetProgress(float progress, float tween_sec = 0)
{
//GameDebug.LogError($"SetProgress {progress}, {tween_sec}");
if (_instance != null && _instance.progressBar != null)
{
_instance.progressBar.SetProgressValue(progress, tween_sec);
}
}
public static void SetSubProgress(float progress, string info)
{
//GameDebug.LogError($"SetProgress {progress}, {tween_sec}");
if (_instance != null && _instance.downloadSzie != null)
{
_instance.downloadSzie.text = info;
}
}
public static void SetVersionInfo(string version)
{
if (_instance != null && _instance.versionInfo != null)
{
_instance.versionInfo.text = version;
}
}
public static float GetProgress()
{
float progress = 0f;
if (_instance != null)
{
progress = _instance.progressBar.progress;
}
return progress;
}
public static void SetLoadingInfo(string info)
{
if (_instance != null && _instance.loadingInfo != null)
{
_instance.loadingInfo.text = info;
}
}
private Coroutine MoveTowardsCoroutine;
public static void MoveTowards(float p1, float p2, float speed)
{
StopMoveTowards();
_instance.MoveTowardsCoroutine = _instance.StartCoroutine(_instance.ProcessMoveTowards(p1, p2, speed));
}
public static void StopMoveTowards()
{
if (_instance.MoveTowardsCoroutine != null)
{
_instance.StopCoroutine(_instance.MoveTowardsCoroutine);
_instance.MoveTowardsCoroutine = null;
}
}
public static void ShowProgressBar()
{
GameDebug.Log("[LoadingScreen]ShowProgressBar");
if (_instance != null)
{
_instance._ShowProgressBar();
}
}
public void _ShowProgressBar()
{
if (progressBar != null)
{
progressBar.gameObject.SetActiveAsNeed(true);
}
}
public static void ShowSubProgressBar()
{
GameDebug.Log("[LoadingScreen]ShowSubProgressBar");
if (_instance != null)
{
_instance._ShowSubProgressBar();
}
}
public void _ShowSubProgressBar()
{
if (downloadSzie != null)
{
downloadSzie.gameObject.SetActiveAsNeed(true);
}
}
public static void HideProgressBar()
{
GameDebug.Log("[LoadingScreen]HideProgressBar");
if (_instance != null)
{
_instance._HideProgressBar();
}
}
private void _HideProgressBar()
{
progressBar.gameObject.SetActiveAsNeed(false);
SetLoadingInfo("");
}
public static void HideSubProgressBar()
{
GameDebug.Log("[LoadingScreen]HideSubProgressBar");
if (_instance != null)
{
_instance._HideSubProgressBar();
}
}
private void _HideSubProgressBar()
{
downloadSzie.gameObject.SetActiveAsNeed(false);
}
private IEnumerator ProcessMoveTowards(float pp1, float pp2, float speed)
{
var p1 = pp1;
var p2 = pp2;
while (Mathf.Abs(p1 - p2) > Mathf.Epsilon)
{
p1 = Mathf.MoveTowards(p1, p2, speed * Time.unscaledDeltaTime);
SetProgress(p1);
yield return null;
}
SetProgress(p2);
MoveTowardsCoroutine = null;
}
void OnDestroy()
{
//_instance = null;
}
}