Wednesday, April 10, 2019

Managing data for large number of levels in Unity?


I am making a Unity game vaguely in the vein of Candy Crush Saga for a client and am moving in circles trying to decide how to manage the data for all of their levels.


In brief, they want to divide the game into multiple regions that each have 100 levels. For example, you might go to the world map, select Teddy Bear Islands, and then go to the winding-path style level select screen similar to King's "Saga" games. My goal is to have the levels be procedurally generated, so the data for each level will consist of dozen or so parameters instead of hundreds of variables. There will only be one scene, and the levels will be built by running the parameters through an algorithm.


The game isn't a match-3, but for comparison, imagine if your level data for a match-3 was just basic parameters like the number of each type of piece and the target score, and each time you started a level it would randomly arrange pieces based on the input parameters (instead of you manually declaring the position of each piece). The data for a level will look something like:


int targetScore
int levelType
float difficultyModifier
int numPieceA
int numPieceB
int numPieceC

int numPieceD

I haven't worked with such a large data set (in terms of the number of levels) in Unity before and am unsure how to structure it. In a less editor-driven engine, I would probably just store all of the levels in XML files. However, since this is a Unity project I feel like I should be making an editor-based solution that will be easier for the client to work with in the future.


With an XML-driven approach, I imagine that I would have an XML for each region containing all of the 100 levels in that region, and each location on the world map would be linked to one of the XML files. When the user selected a region, it would read the levels into an array, and link each of the 100 level buttons to an array index.


For a more editor-driven approach, I might create custom editor windows where the regions and levels can be defined, and then link the region and level buttons to the data from the editor window (I haven't done much with editor windows yet and am not entirely sure how to tackle this approach). Or I could create a serializable LevelData type and a simple MonoBehavior with a public array of LevelData so that the levels could be defined on GameObjects in the inspector.


Each solution has pros and cons, and I'm stuck on what to do. Is there a conventional approach for managing such a large number of levels in Unity? Should I try one of the above approaches, or is there a better solution?



Answer



(Sorry for poor formatting - I'm on my phone)


There are a lot of valid options. If I were tasked with this, I would probably do one of two things:


Save the level data in Google Sheets and use one of the google sheets assets available on the asset store to pull it into the game either at design time or run time.



Or:


Save the level data in ScriptableObjects, probably one per region. The simplest method would be to make each scriptableobject a list of LevelData objects and create a property drawer to improve handling and editing of the leveldata objects. To make things better from there you could:


Make the list a reorderable list (undocumented but if you search it's not hard to find)


Create custom level and region editor Windows


The second option is more "Unity" and the experience you get with editor scripting will make you more effective in future projects. The first option is probably quicker. Both are perfectly valid in my opinion.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...