Well my game has like 20+ levels… are you suggesting I load all my levels at the start of my application? Is the SceneManager going to handle the memory somehow? Why would I load scenes the player might not even access or have access to? Seems inefficient and cumbersome.
Sorry to ask u so much but couldn’t find any helpful resources regarding the SceneManager.
GetSceneAt is for selecting one of your already loaded scenes. The index is the index at which they’re loaded, not their index in the build settings.
I guess it could be used to iterate the currently loaded scenes? But if you have a game where you load a bunch of scenes simultaneously, you should probably have some kind of script managing the loaded scenes, and not get them by an index.
By the way, do you have an actual problem you need help with, or did you just need to rant?
Quite simple. A scene is not necessarily a level, but can be multiple things. Take for example a game (or level) which plays in a house. In that game (or level) the hallways and each room might be separate scenes where you transition seamlessly in. Advantage of that is that you can save heavily on memory usage, as you don’t need to load the entire environment in one go.
Yes I do actually have a problem lol. I’m trying to store a dictionary of <LevelData, string>, the string being the level scene’s name and the LevelData being some data from the level like high scores, whether it has been beaten or not, etc. I want my script to be able to add to the dictionary dynamically if new levels are added, but I can’t get the scene names from the SceneManager. I need only the levels specifically and not other scenes obviously…
Here’s what I tried:
int sceneCount = SceneManager.sceneCountInBuildSettings;
for (int i = 0; i < sceneCount; i++)
{
string sceneName = SceneManager.GetSceneAt(i).name;
if(sceneName.Contains("Level"))
{
levelDataHolder.Add(sceneName, new LevelData());
}
}
Obviously this didn’t work, so here I am complaining.
Edit: GetAllScenes() looks nice but it’s deprecated lol.
A problem with the design is that you can’t get a list of the scenes that exist, but are not loaded at runtime. That’s only available at editor time, through EditorSceneManager.
The best solution for you is probably to make levelDataHolder be lazy. So if you ask for the data for a scene, but that scene’s LevelData hasn’t been added, just add it then.
var currentScene = EditorApplication.currentScene;
foreach (var scene in EditorBuildSettings.scenes)
{
EditorApplication.OpenScene(scene.path);
//Do some stuff
EditorApplication.SaveScene();
}
EditorApplication.OpenScene(currentScene);