- Home /
Save And load Level Data in a game with one game scene and multiple levels saved as prefabs
hey, I'm trying to find the best way to control the level management of my game. i have only one game scene, and the levels are saved as prefabs (every prefab has inner gameobjects and a different design - not just a script) every level has some variables - isStar1Completed, isStar2Completed, bestTime, timeForStar and few more
i need to load the levels data from the level select scene, to show the user the level goals and his progress and when the user select a level to play, I need to pass the level id/name to the game scene and there to instantiate the chosen level prefab
is it bad to store a lot of prefabs in the resources folder? has anyone tried to use the Asset Bundles and can it be good for my purpose?
I would really appreciate any idea on how to approach this thanks
Answer by agray427 · Jan 15, 2018 at 07:56 PM
I've heard Resource Folder calls are bad, but I don't see the harm in using them for this. You will only be making one call at a time, so it's not bad. Here's what you need to do. You need to create a class called LevelData. Store the information about stars, time, and other stats like that onto it. You also want to store the relative path of the Prefab as a string in the class as well. You will then have a list of these that need to be saved and loaded from a GameManager class which inherits from MonoBehaviour. I think the easiest route for you is to have the GameManager class have a list of LevelData, make sure it is marked with [System.Serializable], and then you can edit these values to be whatever you want within the Inspector. GameManager will serialize all of this data, so that's not a problem, however these changes won't persist. To get the data to persist, you need to save these changes to a file in some directory; I recommend Application.persistentDataPath. Whenever you start the game, it should check to see if the file exists. If it does, you will load the list from the file. This will override all of the settings you had made in the editor, which is what you want. That means the data persisted. The data you create in the editor should only be the INITIAL list. You will have issues of adding new levels and stuff, but these can be avoided by creating a custom loader. For example, you could load the list that's in the file, check to see if the current list already has a reference to the same scene, and override it's values. That way, if you added something in the editor after already saving data that the change wasn't apart of, it won't override your new levels and stuff that you're trying to add to the game. I hope this helps, but I'm here if you have more questions!
For advanced serialization, you can always read over my blog post here: https://graymattertutorials.wordpress.com/2018/01/15/serialization-with-json-net-in-unity-3d/
Your answer

Follow this Question
Related Questions
How can i save the games inventory? 1 Answer
How do I make a basic easy Save and load function with JavaScript 1 Answer
Save/Load System not working 0 Answers
How do i change my save "Total Coin" everytime i get more coins. 0 Answers
Save Game Sceenes user quit aplication, Can I save the scene as a whole while leaving the game? 0 Answers