UPF/Assets/Game/Scripts/LoadingScreen.cs
2022-09-20 00:16:35 +05:30

68 lines
2.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadingScreen : MonoBehaviour
{
public static LoadingScreen instance;
CanvasGroup canvasGroup;
public Slider loadingProgress;
public TMPro.TMP_Text loadingProgressTxt;
private static bool loading;
public static bool Loading => loading;
void Awake(){
instance =this;
}
void Start()
{
DontDestroyOnLoad(gameObject);
canvasGroup = GetComponent<CanvasGroup>();
}
public void LoadLevel(string levelName){
if(loading){return;}loading=true;
StartCoroutine(loadlLevel(levelName));
}
IEnumerator loadlLevel(string levelName){
loading=true;
canvasGroup.alpha=0;
canvasGroup.blocksRaycasts=true;
SetProgress(0);
while(canvasGroup.alpha< 1f){
canvasGroup.alpha+=0.03f;
yield return new WaitForFixedUpdate();
}
while(RegionManager.selectedServer == null){
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;
}
yield return new WaitForSeconds(0.2f);
canvasGroup.blocksRaycasts=false;
while(canvasGroup.alpha> 0){
canvasGroup.alpha-=0.03f;
yield return new WaitForFixedUpdate();
}
loading=false;
}
void SetProgress(float value){
loadingProgress.value = value;
loadingProgressTxt.text = (int)(value*100) + "%";
}
}