Hi, I’m a beginner in Unity and I want to make a loading scene for my game but I think my GameManager is awful.
But when I load my scene the canvas of the loading scene doesn’t appear.
If you can help me
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
// Game States
public enum GameState { INTRO, MAIN_MENU, PAUSED, GAME, CREDITS, HELP }
public delegate void OnStateChangeHandler();
public class SimpleGameManager: Singleton
{
public SimpleGameManager() {}
private static SimpleGameManager instance = null;
public event OnStateChangeHandler OnStateChange;
public GameState gameState { get; private set; }
public GameObject[] SystemPrefabs;
private List<GameObject> _instanciedSystemPrefabs;
AsyncOperation operation;
public GameObject loadingScreen;
public Text progressText;
public Slider slider;
List<AsyncOperation> _loadOperations;
private void Start()
{
_loadOperations = new List<AsyncOperation>();
_instanciedSystemPrefabs = new List<GameObject>();
InstantiateSystemPrefabs();
//if (GameObject.FindGameObjectWithTag("EcranDeChargement") != null)
//{
// loadingScreen = GameObject.FindGameObjectWithTag("EcranDeChargement");
// progressText = GameObject.FindGameObjectWithTag("progressText").GetComponent<Text>();
// slider = GameObject.FindGameObjectWithTag("Slider").GetComponent<Slider>();
// loadingScreen.SetActive(false);
// Debug.Log("1er");
//}
//Debug.Log("2eme");
InstantiateSystemPrefabs();
DontDestroyOnLoad(gameObject);
//DontDestroyOnLoad(SystemPrefabs[0]);
}
public void SetGameState(GameState state){
this.gameState = state;
OnStateChange();
}
public void loadScene(string namelevel)
{
StartCoroutine(LoadAsynchronously(namelevel));
}
IEnumerator LoadAsynchronously(string namelevel)
{
AsyncOperation async = SceneManager.LoadSceneAsync(namelevel);
async.completed += OnLoadOperationComplete;
_loadOperations.Add(async);
SystemPrefabs[0].SetActive(true);
while (!async.isDone)
{
float progress = Mathf.Clamp01(async.progress / 0.9f);
slider.value = progress;
progressText.text = (progress * 100).ToString("F0") + "%";
yield return null;
}
}
private void OnLoadOperationComplete(AsyncOperation ao)
{
Debug.Log("Load Compete.");
if (_loadOperations.Contains(ao) == true)
{
_loadOperations.Remove(ao);
}
}
private void UnLoadOperationComplete(AsyncOperation ao)
{
Debug.Log("Load Destroy.");
}
public void UnloadLevel(string levelName)
{
AsyncOperation ao = SceneManager.UnloadSceneAsync(levelName);
SystemPrefabs[0].SetActive(false);
ao.completed += UnLoadOperationComplete;
}
public void OnApplicationQuit(){
SimpleGameManager.instance = null;
}
void InstantiateSystemPrefabs()
{
GameObject prefabInstance;
for (int i = 0; i < SystemPrefabs.Length; ++i)
{
prefabInstance = Instantiate(SystemPrefabs*);*
_instanciedSystemPrefabs.Add(prefabInstance);
}
}
}