Files
beyond/Assets/Scripts/UI/LoadingScreen.cs
2024-11-20 15:21:28 +01:00

76 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Beyond
{
public class LoadingScreen : MonoBehaviour
{
public GameObject m_bar;
public int m_levelToLoadID = 1;
AsyncOperation m_async;
public bool m_loadOnStart = true;
// Start is called before the first frame update
void LoadScene(int lvelID)
{
m_async = SceneManager.LoadSceneAsync(lvelID);
m_async.allowSceneActivation = false;
m_bar.transform.localScale = new Vector3(m_async.progress, 0.9f, 1);
}
IEnumerator LoadSceneCooroutine()
{
// The Application loads the Scene in the background as the current Scene runs.
// This is particularly good for creating loading screens.
// You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
// a sceneBuildIndex of 1 as shown in Build Settings.
//yield return new WaitForSeconds(1f);
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(m_levelToLoadID);
asyncLoad.allowSceneActivation = false;
// Wait until the asynchronous scene fully loads
while (!asyncLoad.isDone)
{
m_bar.transform.localScale = new Vector3(asyncLoad.progress, 0.9f, 1);
if (asyncLoad.progress >= 0.9f)
{
m_bar.transform.localScale = new Vector3(0.9f, 0.9f, 1);
asyncLoad.allowSceneActivation = true;
}
yield return null;
}
m_bar.transform.localScale = new Vector3(1.0f, 0.9f, 1);
yield return null;
}
void Start()
{
m_bar.transform.localScale = new Vector3(0.0f, 0.9f, 1f);
// LoadScene(m_levelToLoadID);
StartCoroutine(LoadSceneCooroutine());
}
// Update is called once per frame
void Update()
{
/*
if (m_loadOnStart)
{
m_loadOnStart = false;
LoadScene(m_levelToLoadID);
}
// Continue until the installation is completed
if (m_async != null && m_async.isDone == false)
{
m_bar.transform.localScale = new Vector3(m_async.progress, 0.9f, 1);
if (m_async.progress >= 0.9f)
{
m_bar.transform.localScale = new Vector3(1, 0.9f, 1);
m_async.allowSceneActivation = true;
}
}
*/
}
}
}