Unchromed is a group project of a 3D puzzle platformer that has to be played with two people on the same keyboard.
Features:
Example code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class HighScoreManager : MonoBehaviour
{
//---------------
//PUBLIC VARIABLES
//---------------
//AMOUNT OF SCORES YOU WANT TO SAVE
public int AmountOfScores = 20;
//NAME OF THE CURRENT PLAYER
public string PlayerName = "Player";
//SCORE STRUCT THAT CONTAINS THE LEVELTIME AND PLAYERNAME
public struct Score
{
public float levelTime;
public string playerName;
}
//---------------
//PRIVATE VARIABLES
//---------------
//---------------
//PUBLIC METHODS
//---------------
//Get the name of the player
//no paramaters
public string GetPlayerName()
{
return PlayerName;
}
//Set the player name
//give the name as a paramater
public void SetPlayerName(string name)
{
PlayerName = name;
}
//Use this function to add a highscore to the playerprefs
//Give the levelName, time it took to complete the level and playerName
public void AddHighScore(string levelName, float levelTime, string playerName)
{
//add a score to the counter, initialize the key if there is none
if (!PlayerPrefs.HasKey(levelName))
{
PlayerPrefs.SetInt(levelName, 0);
}
//create a Score variable
Score inputScores;
inputScores.levelTime = levelTime;
inputScores.playerName = playerName;
//Create and Initialize array to put the highscores in
List<score> highScoreList = GetHighScores(levelName);
//loop through array and push the scores inbetween
bool notAssigned = true;
if (highScoreList.Count > 0)
{
for (int i = 0; i < highScoreList.Count; i++)
{
if (highScoreList[i].levelTime > levelTime)
{
highScoreList.Insert(i, inputScores);
notAssigned = false;
break;
}
}
}
else
{
highScoreList.Insert(0,inputScores);
notAssigned = false;
}
if (notAssigned)
{
highScoreList.Insert(highScoreList.Count, inputScores);
notAssigned = false;
}
//Assign the values to the playerprefs, only loop for the amount of scores there should be saved
for (int i = 0; i < highScoreList.Count; i++)
{
string scoreName = ("HighScore_" + levelName + "_" + i).ToString();
PlayerPrefs.SetString(scoreName + "_Value", highScoreList[i].levelTime.ToString("F1"));
PlayerPrefs.SetString(scoreName, highScoreList[i].playerName);
}
if (PlayerPrefs.HasKey(levelName))
{
PlayerPrefs.SetInt(levelName, PlayerPrefs.GetInt(levelName) + 1);
}
}
//Display the scores in a textbox
//Give the levelname and the place to put the text
public void DisplayHighScores(string levelName, UnityEngine.UI.Text textBox)
{
textBox.text = "";
var scores = GameManager.SingleTon().GetHighScoreManager().GetHighScores(levelName);
foreach (Score score in scores)
{
textBox.text += score.playerName + " : " + score.levelTime + "\n";
}
}
//---------------
//PRIVATE METHODS
//---------------
//Get a list with the highscores
//give the name as a parameter
private List<score> GetHighScores(string levelName)
{
List<score> highScoreList = new List<score>();
//initialize array
for (int i = 0; i < PlayerPrefs.GetInt(levelName); i++)
{
string scoreName = ("HighScore_" + levelName + "_" + i).ToString();
Score tempScore;
bool tryParse = true;
//Try to parse the value to a float
tryParse = float.TryParse(PlayerPrefs.GetString(scoreName + "_Value"), out tempScore.levelTime);
//if parsing doesn't work, put the value on 0
if (!tryParse)
{
tempScore.levelTime = 0.0f;
}
tempScore.playerName = PlayerPrefs.GetString(scoreName);
highScoreList.Add(tempScore);
}
return highScoreList;
}
}