Create a gist now

Instantly share code, notes, and snippets.

const int
kPreLevelScenes = 2, // Number of scenes before the first level (splash, menu, etc.)
kPostLevelScenes = 0; // Number of scenes after last level (score, credits, whatever)
const string kLevelScorePrefix = "Score for level ";
// ...
void OnLevelBeat ()
{
if (Application.loadedLevel < Application.levelCount - kPostLevelScenes - 1)
{
Application.LoadLevel (Application.loadedLevel + 1);
}
else
{
Debug.Log ("You win!");
}
}
// ...
int GetScore (int level)
{
return PlayerPrefs.GetInt (kLevelScorePrefix + level, 0);
}
void SetScore (int level, int score)
{
PlayerPrefs.SetInt (kLevelScorePrefix + level, score);
}
int Level
{
get
{
return Application.loadedLevel - kPreLevelScenes;
}
}
// ...
void OnHighscoreGUI ()
{
int levelCount = Application.levelCount - kPreLevelScenes - kPostLevelScenes;
for (int level = 0; level < levelCount; ++level)
{
GUILayout.Label (string.Format (
"Level {0}{1}: {2}",
level + 1,
level == Level ? " (current)" : "",
GetScore (level)
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment