Hi,
I have a table (it can be csv, json, whatever) with data about my 100 levels in my game. There are following columns in my table: level_id, mission objectives, XYZ location of enemies, if the level is unlocked.
I would like to do 2 things with it:
- (in main menu scene)create a level selection menu (e.g. like this) dynamically - i.e. if a player have 10 levels unlocked I want to show only them.
- (in level scenes) load information about the levels to scenes.
What is the best approach for this in Unity?
Here’s a quick and dirty summary of how I’d do it:
First, create a class that represents your tabular data. Something like:
public class LevelInfo {
public string LevelId { get; set; }
public List<MissionObjective> Objectives { get; set; }
public List<Vector3> EnemyLocations {get; set; }
public bool IsUnlocked {get; set;}
}
Second, build a list of LevelInfo objects. How you drop this depends on where the data is coming from etc…
Make a UI prefab that can show a preview of the level based on a LevelInfo data and loads the level in response to button click. something like:
public class LevelPanel : MonoBehaviour {
public Text LevelNameLabel;
// etc...
LevelInfo myLevelInfo;
public void Init(LevelInfo info) {
myLevelInfo = info;
LevelNameLabel.text = info.LevelId;
// etc..
}
// Call this from UI button click
public void LoadLevel() {
GameManager.LoadLevel(myLevelInfo);
}
}
Make a scene for all the levels. In the scene, make an object which initializes the scene based on the LevelInfo object:
public class LevelInitializer : MonoBehaviour {
void Start() {
LevelInfo levelInfo = GameManager.LoadedLevel;
// rest of the code to set up the level from the data in LevelInfo
}
}
You’ll need a singleton DontDestroyOnLoad GameManager object that helps facilitate the transfer of the LevelInfo object from your UI prefab to your LevelInitializer.