Click to download the unitypackage file
Explanation
Level grouping is a new feature introduced in Mad Level Manager 2.0.0. It’s easy to add levels into groups, but it is a little tricky to create a group select screen and then count all stars from nested levels to unlock the next group available. This page will explain to you how this demo has been created.
The setup
To display groups in the level select screen I had to create levels that only purpose is to load group level select screens.
Level names are here the group names to make things simpler. Here’s how the level properties looks like:
As arguments I am passing name of the level that is level select screen for given group. Then in this level I am executing this script (GroupLoader.cs):
using MadLevelManager;
public class GroupLoader : MonoBehaviour {
void Start() {
MadLevel.LoadLevelByName(MadLevel.arguments);
}
}
It will load level select screen given as arguments immediately.
Unlocking groups
In order to unlock groups you have to count stars for each group. Group Select Screen level contains GroupUnlocker.cs scrtips:
1 using MadLevelManager;
2
3 // this script should be placed in group select screen and will unlock group icons when needed
4 public class GroupUnlocker : MonoBehaviour {
5
6 void Start() {
7 string[] groups = MadLevel.GetAllLevelNames(MadLevel.Type.Level, MadLevel.defaultGroupName);
8
9 for (int i = 1; i < groups.Length; ++i) {
10 string prevGroup = groups[i - 1];
11 string group = groups[i];
12
13 int acquired = StarsUtil.CountAcquiredStars(prevGroup);
14 if (acquired >= 6) {
15 if (MadLevelProfile.IsLocked(group)) {
16 MadLevelProfile.SetLocked(group, false);
17 MadLevel.ReloadCurrent();
18 }
19 }
20 }
21 }
22
23 }
In line 7 I am getting the names of groups (levels that are named as groups to be precise). In line 13 I am counting number of start acquired in previous group, and when the number is equal or grater 6 (line 14) I am unlocking the level that will allow the player to access the next group on line 16.
Here how the StarsUtils class looks like:
using MadLevelManager;
public class StarsUtil : MonoBehaviour {
#region Public Static Methods
public static int CountAvailableStars(string groupName) {
var levels = MadLevel.GetAllLevelNames(MadLevel.Type.Level, groupName);
return levels.Length * 3;
}
public static int CountAcquiredStars(string groupName) {
var levels = MadLevel.GetAllLevelNames(MadLevel.Type.Level, groupName);
int collectedStars = 0;
for (int i = 0; i < levels.Length; ++i) {
if (MadLevelProfile.GetLevelBoolean(levels[i], "star_1")) collectedStars++;
if (MadLevelProfile.GetLevelBoolean(levels[i], "star_2")) collectedStars++;
if (MadLevelProfile.GetLevelBoolean(levels[i], "star_3")) collectedStars++;
}
return collectedStars;
}
#endregion
}