UPF/Assets/Game/Scripts/LoadingScreen.cs
2022-12-14 01:49:10 +05:30

106 lines
3.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.Video;
public class LoadingScreen : MonoBehaviour
{
public static LoadingScreen instance;
CanvasGroup canvasGroup;
public Slider loadingProgress;
public TMPro.TMP_Text loadingProgressTxt;
public VideoPlayer loadingScreenVideo;
private static bool loading;
public static bool Loading => loading;
void Awake()
{
instance = this;
Application.backgroundLoadingPriority = ThreadPriority.Low;
canvasGroup = GetComponent<CanvasGroup>();
}
void Start()
{
DontDestroyOnLoad(gameObject);
}
public void LoadLevel(string levelName)
{
if (loading) { return; }
loading = true;
StartCoroutine(loadlLevel(levelName));
}
IEnumerator loadlLevel(string levelName)
{
AudioManager.instnace.SetMusic(-1);
TutorialManager.instance = null;
loading = true;
canvasGroup.alpha = 0;
canvasGroup.blocksRaycasts = true;
loadingScreenVideo.Play();
SetProgress(0);
while (canvasGroup.alpha < 1f)
{
canvasGroup.alpha += 0.03f;
yield return new WaitForFixedUpdate();
}
// if (levelName.ToLower().Contains("minigame") && !levelName.ToLower().Contains("tutorial"))
// {
// while (RegionManager.selectedServer == null && levelName.Contains("Minigame"))
// {
// loadingProgressTxt.text = "Preparing";
// yield return new WaitForSeconds(0.5f);
// }
// }
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(levelName);
// asyncOperation.allowSceneActivation = false;
while (!asyncOperation.isDone)
{
//Output the current progress
// m_Text.text = "Loading progress: " + (asyncOperation.progress * 100) + "%";
// Check if the load has finished
SetProgress(asyncOperation.progress);
yield return null;
}
Debug.Log("Loaded scene " + levelName);
yield return new WaitForSecondsRealtime(0.2f);
canvasGroup.blocksRaycasts = false;
while (canvasGroup.alpha > 0)
{
canvasGroup.alpha -= 0.03f;
yield return new WaitForSecondsRealtime(0.015f);
}
Debug.Log("Loading scene vanishing");
loading = false;
}
void SetProgress(float value)
{
loadingProgress.value = value;
loadingProgressTxt.text = (int)(value * 100) + "%";
}
public void DeleteRankedRooms(int port)
{
StartCoroutine(CloseRoomClient(port));
}
IEnumerator CloseRoomClient(int port)
{
Debug.Log("Clearing rooms for port " + port);
WWWForm form = new WWWForm();
form.AddField("port", port);
WWW req = new WWW(DBmanager.phpRoot + "clear_ranked_room.php", form);
yield return req;
Debug.Log(req.text);
}
}