|
|
|
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine.SceneManagement; |
|
|
|
|
|
|
|
[CreateAssetMenu(fileName = "sceneDB", menuName = "Scene Data/Database")] |
|
|
|
public class ScenesData : ScriptableObject |
|
|
|
{ |
|
|
|
public List<Level> levels = new List<Level>(); |
|
|
|
public List<Menu> menus = new List<Menu>(); |
|
|
|
using System.Collections.Generic; |
|
|
|
using UnityEngine; |
|
|
|
using UnityEngine.SceneManagement; |
|
|
|
|
|
|
|
[CreateAssetMenu(fileName = "sceneDB", menuName = "Scene Data/Database")] |
|
|
|
public class ScenesData : ScriptableObject |
|
|
|
{ |
|
|
|
public List<Level> levels = new List<Level>(); |
|
|
|
public List<Menu> menus = new List<Menu>(); |
|
|
|
public List<AsyncOperation> scenesToLoad = new List<AsyncOperation>(); |
|
|
|
|
|
|
|
|
|
|
|
//List of the scenes name to load and unload
|
|
|
|
public List<AsyncOperation> scenesToLoad = new List<AsyncOperation>(); |
|
|
|
|
|
|
|
//List of the scenes names to load and unload
|
|
|
|
public List<string> scenesToUnLoadNames = new List<string>(); |
|
|
|
|
|
|
|
/* |
|
|
|
* Levels |
|
|
|
*/ |
|
|
|
|
|
|
|
//Load a scene with a given index
|
|
|
|
public void LoadLevelWithIndex(int index) |
|
|
|
{ |
|
|
|
public List<string> scenesToUnLoadNames = new List<string>(); |
|
|
|
|
|
|
|
/* |
|
|
|
* Clear |
|
|
|
*/ |
|
|
|
|
|
|
|
//Clear Scenes to load and unload list
|
|
|
|
|
|
|
|
public void ClearScenesLists() |
|
|
|
{ |
|
|
|
scenesToLoadNames.Clear(); |
|
|
|
scenesToUnLoadNames.Clear(); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Levels |
|
|
|
*/ |
|
|
|
|
|
|
|
//Load a scene with a given index
|
|
|
|
public void LoadLevelWithIndex(int index) |
|
|
|
{ |
|
|
|
if (index >= levels.Count) |
|
|
|
if (index >= levels.Count) |
|
|
|
{ |
|
|
|
index = 1; |
|
|
|
CurrentLevelIndex = index; |
|
|
|
|
|
|
scenesToLoadNames.Add("Level" + index.ToString()); |
|
|
|
scenesToLoadNames.Add(levels[index].sceneName); |
|
|
|
|
|
|
|
scenesToUnLoadNames.Add(levels[index - 1].ToString()); |
|
|
|
scenesToUnLoadNames.Add(levels[index - 1].sceneName); |
|
|
|
//Unload level (This needs to be here, in case we need to unload MM)
|
|
|
|
//Unload previous level if it exists
|
|
|
|
if(scenesToUnLoadNames == null) |
|
|
|
{ |
|
|
|
return; |
|
|
|
|
|
|
UnloadScenes(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public void LoadScenes() |
|
|
|
|
|
|
//Add the scene to the list of scenes to load asynchronously in the background
|
|
|
|
scenesToLoad.Add(SceneManager.LoadSceneAsync(scenesToLoadNames[i], LoadSceneMode.Additive)); |
|
|
|
//Remove scene from list of scenes to load
|
|
|
|
//scenesToUnLoadNames.Add(scenesToLoadNames[i]);
|
|
|
|
scenesToLoadNames.Remove(scenesToLoadNames[i]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
{ |
|
|
|
for (int i = 0; i < scenesToUnLoadNames.Count; ++i) |
|
|
|
{ |
|
|
|
//Add the scene to the list of scenes to unload asynchronously in the background
|
|
|
|
//Unload the scene asynchronously in the background
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//Check if a scene is already loaded
|
|
|
|
if (SceneManager.sceneCount > 0) |
|
|
|
{ |
|
|
|
for (int i = 0; i < SceneManager.sceneCount; ++i) |
|
|
|
{ |
|
|
|
Scene scene = SceneManager.GetSceneAt(i); |
|
|
|
if (scene.name == sceneName) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
if (SceneManager.sceneCount > 0) |
|
|
|
{ |
|
|
|
for (int i = 0; i < SceneManager.sceneCount; ++i) |
|
|
|
{ |
|
|
|
Scene scene = SceneManager.GetSceneAt(i); |
|
|
|
if (scene.name == sceneName) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//Start next level
|
|
|
|
public void NextLevel() |
|
|
|
{ |
|
|
|
CurrentLevelIndex++; |
|
|
|
LoadLevelWithIndex(CurrentLevelIndex); |
|
|
|
} |
|
|
|
//Restart current level
|
|
|
|
public void RestartLevel() |
|
|
|
{ |
|
|
|
LoadLevelWithIndex(CurrentLevelIndex); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
* Menus |
|
|
|
*/ |
|
|
|
|
|
|
|
//Load main Menu
|
|
|
|
public void LoadMainMenu() |
|
|
|
{ |
|
|
|
scenesToLoadNames.Add("MainMenu"); |
|
|
|
LoadScenes(); |
|
|
|
//Add it to the scenes to unload when the level starts
|
|
|
|
scenesToUnLoadNames.Add("MainMenu"); |
|
|
|
} |
|
|
|
//Load Pause Menu
|
|
|
|
public void LoadPauseMenu() |
|
|
|
{ |
|
|
|
//Add later if needed
|
|
|
|
} |
|
|
|
} |
|
|
|
//Start next level
|
|
|
|
public void NextLevel() |
|
|
|
{ |
|
|
|
CurrentLevelIndex++; |
|
|
|
LoadLevelWithIndex(CurrentLevelIndex); |
|
|
|
} |
|
|
|
//Restart current level
|
|
|
|
public void RestartLevel() |
|
|
|
{ |
|
|
|
LoadLevelWithIndex(CurrentLevelIndex); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* Menus |
|
|
|
*/ |
|
|
|
|
|
|
|
//Load main Menu
|
|
|
|
public void LoadMainMenu() |
|
|
|
{ |
|
|
|
//scenesToLoadNames.Add("MainMenu");
|
|
|
|
scenesToLoadNames.Add(menus[(int)Type.Main_Menu].sceneName); |
|
|
|
LoadScenes(); |
|
|
|
} |
|
|
|
//Load Pause Menu
|
|
|
|
public void LoadPauseMenu() |
|
|
|
{ |
|
|
|
//Add later if needed
|
|
|
|
} |
|
|
|
} |