|
|
|
|
|
|
[SerializeField] private BoolEventChannelSO _toggleLoadingScreen = default; |
|
|
|
[SerializeField] private VoidEventChannelSO _onSceneReady = default; |
|
|
|
|
|
|
|
private List<AsyncOperationHandle<SceneInstance>> _loadingOperationHandles = new List<AsyncOperationHandle<SceneInstance>>(); |
|
|
|
private AsyncOperationHandle<SceneInstance> _loadingOperationHandle; |
|
|
|
private GameSceneSO[] _scenesToLoad; |
|
|
|
private GameSceneSO[] _currentlyLoadedScenes = new GameSceneSO[] { }; |
|
|
|
private GameSceneSO _sceneToLoad; |
|
|
|
private GameSceneSO _currentlyLoadedScene; |
|
|
|
private bool _showLoadingScreen; |
|
|
|
|
|
|
|
private SceneInstance _gameplayManagerSceneInstance = new SceneInstance(); |
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// This function loads the location scenes passed as array parameter
|
|
|
|
/// </summary>
|
|
|
|
private void LoadLocation(GameSceneSO[] locationsToLoad, bool showLoadingScreen) |
|
|
|
private void LoadLocation(GameSceneSO locationToLoad, bool showLoadingScreen) |
|
|
|
_scenesToLoad = locationsToLoad; |
|
|
|
_sceneToLoad = locationToLoad; |
|
|
|
//In case we are coming from the main menu, we need to load the persistent Gameplay manager scene first
|
|
|
|
//In case we are coming from the main menu, we need to load the Gameplay manager scene first
|
|
|
|
StartCoroutine(ProcessGameplaySceneLoading(locationsToLoad, showLoadingScreen)); |
|
|
|
_gameplayManagerLoadingOpHandle = _gameplayScene.sceneReference.LoadSceneAsync(LoadSceneMode.Additive, true); |
|
|
|
_gameplayManagerLoadingOpHandle.Completed += OnGameplayMangersLoaded; |
|
|
|
UnloadPreviousScenes(); |
|
|
|
UnloadPreviousScene(); |
|
|
|
private IEnumerator ProcessGameplaySceneLoading(GameSceneSO[] locationsToLoad, bool showLoadingScreen) |
|
|
|
private void OnGameplayMangersLoaded(AsyncOperationHandle<SceneInstance> obj) |
|
|
|
_gameplayManagerLoadingOpHandle = _gameplayScene.sceneReference.LoadSceneAsync(LoadSceneMode.Additive, true); |
|
|
|
|
|
|
|
while (_gameplayManagerLoadingOpHandle.Status != AsyncOperationStatus.Succeeded) |
|
|
|
{ |
|
|
|
yield return null; |
|
|
|
} |
|
|
|
UnloadPreviousScenes(); |
|
|
|
UnloadPreviousScene(); |
|
|
|
private void LoadMenu(GameSceneSO[] menusToLoad, bool showLoadingScreen) |
|
|
|
private void LoadMenu(GameSceneSO menuToLoad, bool showLoadingScreen) |
|
|
|
_scenesToLoad = menusToLoad; |
|
|
|
_sceneToLoad = menuToLoad; |
|
|
|
_showLoadingScreen = showLoadingScreen; |
|
|
|
|
|
|
|
//In case we are coming from a Location back to the main menu, we need to get rid of the persistent Gameplay manager scene
|
|
|
|
|
|
|
|
|
|
|
UnloadPreviousScenes(); |
|
|
|
UnloadPreviousScene(); |
|
|
|
private void UnloadPreviousScenes() |
|
|
|
private void UnloadPreviousScene() |
|
|
|
for (int i = 0; i < _currentlyLoadedScenes.Length; i++) |
|
|
|
{ |
|
|
|
_currentlyLoadedScenes[i].sceneReference.UnLoadScene(); |
|
|
|
} |
|
|
|
if(_currentlyLoadedScene != null) |
|
|
|
_currentlyLoadedScene.sceneReference.UnLoadScene(); |
|
|
|
LoadNewScenes(); |
|
|
|
LoadNewScene(); |
|
|
|
/// Kicks off the asynchronous loading of an array of scenes, either menus or Locations.
|
|
|
|
/// Kicks off the asynchronous loading of a scene, either menu or Location.
|
|
|
|
private void LoadNewScenes() |
|
|
|
private void LoadNewScene() |
|
|
|
{ |
|
|
|
if (_showLoadingScreen) |
|
|
|
{ |
|
|
|
|
|
|
_loadingOperationHandles.Clear(); |
|
|
|
//Build the array of handles of the temporary scenes to load
|
|
|
|
for (int i = 0; i < _scenesToLoad.Length; i++) |
|
|
|
{ |
|
|
|
_loadingOperationHandles.Add(_scenesToLoad[i].sceneReference.LoadSceneAsync(LoadSceneMode.Additive, true, 0)); |
|
|
|
} |
|
|
|
|
|
|
|
StartCoroutine(LoadingProcess()); |
|
|
|
_loadingOperationHandle = _sceneToLoad.sceneReference.LoadSceneAsync(LoadSceneMode.Additive, true, 0); |
|
|
|
_loadingOperationHandle.Completed += OnNewSceneLoaded; |
|
|
|
private IEnumerator LoadingProcess() |
|
|
|
private void OnNewSceneLoaded(AsyncOperationHandle<SceneInstance> obj) |
|
|
|
bool done = _loadingOperationHandles.Count == 0; |
|
|
|
|
|
|
|
//This while will exit when all scenes requested have been unloaded
|
|
|
|
while (!done) |
|
|
|
{ |
|
|
|
for (int i = 0; i < _loadingOperationHandles.Count; ++i) |
|
|
|
{ |
|
|
|
if (_loadingOperationHandles[i].Status != AsyncOperationStatus.Succeeded) |
|
|
|
{ |
|
|
|
break; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
done = true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
yield return null; |
|
|
|
} |
|
|
|
|
|
|
|
_currentlyLoadedScenes = _scenesToLoad; |
|
|
|
_currentlyLoadedScene = _sceneToLoad; |
|
|
|
SetActiveScene(); |
|
|
|
|
|
|
|
if (_showLoadingScreen) |
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
{ |
|
|
|
//All the scenes have been loaded, so we assume the first in the array is ready to become the active scene
|
|
|
|
Scene s = ((SceneInstance)_loadingOperationHandles[0].Result).Scene; |
|
|
|
Scene s = ((SceneInstance)_loadingOperationHandle.Result).Scene; |
|
|
|
SceneManager.SetActiveScene(s); |
|
|
|
|
|
|
|
LightProbes.TetrahedralizeAsync(); |
|
|
|