Main Menu with loading screen
This commit is contained in:
104
Assets/Scripts/LoadingScreen.cs
Normal file
104
Assets/Scripts/LoadingScreen.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEngine.UI;
|
||||
public class LoadingScreen : MonoBehaviour
|
||||
{
|
||||
public static LoadingScreen instance {get; private set;}
|
||||
private IProgressBar progress;
|
||||
[SerializeField]private Text loadingProgressTxt;
|
||||
[SerializeField]private Transform progressBar;
|
||||
|
||||
void Awake(){
|
||||
if(instance != null){Destroy(gameObject);return;}
|
||||
instance =this;
|
||||
canvasGroup = GetComponent<CanvasGroup>();
|
||||
|
||||
Application.targetFrameRate = 60;
|
||||
|
||||
progress = progressBar.GetComponent<IProgressBar>();
|
||||
if(progress==null){Debug.LogError("NO IProgressBar interface found on " + progressBar.name);}
|
||||
}
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
public static void LoadLevel(string levelName){
|
||||
if(instance==null){Debug.LogError("No loading screen found, Raw load"); SceneManager.LoadScene(levelName); return;}
|
||||
|
||||
instance.loadLevel(levelName);
|
||||
}
|
||||
|
||||
public void loadLevel(string levelName){
|
||||
if (loading) { return; }
|
||||
loading = true;
|
||||
StartCoroutine(_loadlLevel(levelName));
|
||||
}
|
||||
|
||||
public static bool loading {get; private set;}
|
||||
private CanvasGroup canvasGroup;
|
||||
IEnumerator _loadlLevel(string levelName)
|
||||
{
|
||||
// AudioManager.instnace.SetMusic(-1);
|
||||
loading = true;
|
||||
canvasGroup.alpha = 0;
|
||||
canvasGroup.blocksRaycasts = true;
|
||||
|
||||
SetProgress(0);
|
||||
while (canvasGroup.alpha < 1f)
|
||||
{
|
||||
canvasGroup.alpha += 0.03f;
|
||||
yield return new WaitForFixedUpdate();
|
||||
}
|
||||
|
||||
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(levelName);
|
||||
|
||||
while (!asyncOperation.isDone)
|
||||
{
|
||||
SetProgress(asyncOperation.progress);
|
||||
yield return null;
|
||||
}
|
||||
Debug.Log("Loaded scene " + levelName);
|
||||
// yield return new WaitForSecondsRealtime(2f);
|
||||
while(progress.Progress < 1){
|
||||
SetProgress( progress.Progress+0.01f);
|
||||
yield return new WaitForSeconds(0.1f);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
// progre.fillAmount = value;
|
||||
progress.SetProgress(value);
|
||||
|
||||
if(loadingProgressTxt!=null)loadingProgressTxt.text = (int)(value * 100) + "%";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public interface IProgressBar{
|
||||
|
||||
public void SetProgress(float progress){
|
||||
}
|
||||
|
||||
public float Progress {get;}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user