Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. public class HighScoreManager : MonoBehaviour {
  8.  
  9.     private static readonly string CSV_HIGHSCORE_KEY = "highScores";
  10.  
  11.  
  12.     public static void SaveHighscore(int score, string name){
  13.         int insertIndex = 0;
  14.         bool addToEndOfFile = true;
  15.         List<string> highscoreList = LoadHighscores ();
  16.         List<string> tempList = new List<string> ();
  17.         StringBuilder sb = new StringBuilder ();
  18.  
  19.         //If  the highscores contains previous scores
  20.         if (highscoreList.Count != 0) {
  21.  
  22.             //For each score value in the highscore list
  23.             for (int i = 1; i < highscoreList.Count; i += 2){
  24.  
  25.                 //If score is higher than selected value
  26.                 if (int.Parse(highscoreList[i]) < score) {
  27.  
  28.                     //set insert index
  29.                     insertIndex = i;
  30.  
  31.                     //set addToEndOfFile to false
  32.                     addToEndOfFile = false;
  33.                 }
  34.             }
  35.         }
  36.  
  37.         //If addToEndOfFile is true
  38.         if (addToEndOfFile) {
  39.  
  40.             //Add score to end of the list
  41.             highscoreList.Add (name);
  42.             highscoreList.Add (score.ToString ());
  43.         } else {
  44.  
  45.             //Move data over to a temporary list, this is faster than using insert
  46.             for (int i = 0; i < highscoreList.Count; i++) {
  47.  
  48.                 //If the current index is where we have to insert the new score
  49.                 if (i == insertIndex) {
  50.  
  51.                     //Insert the player score and name
  52.                     tempList.Add (name);
  53.                     tempList.Add (score.ToString ());
  54.                 }
  55.  
  56.                 //Add the value from the old list to the end of the temp list
  57.                 tempList.Add (highscoreList [i]);
  58.             }
  59.             //Set highscoreList to the values of tempList
  60.             highscoreList = tempList;
  61.         }
  62.  
  63.         //Create a string with all the data from the List, with a comma (',') seperating each array item
  64.         for (int i = 0; i < highscoreList.Count; i++){
  65.             if (i == 0) {
  66.                 sb.Append (highscoreList [i]);
  67.             }else{
  68.                 sb.Append (",");
  69.                 sb.Append (highscoreList [i]);
  70.             }
  71.         }
  72.  
  73.         //Save the string to player prefs
  74.         SaveToPlayerPrefs(sb.ToString());
  75.  
  76.     }
  77.  
  78.     private static void SaveToPlayerPrefs(string csv){
  79.         //Set the playerPrefs value to the input string
  80.         PlayerPrefs.SetString(CSV_HIGHSCORE_KEY, csv);
  81.     }
  82.  
  83.     public static List <string> LoadHighscores(){
  84.  
  85.         //If the playerPrefs exists
  86.         if (PlayerPrefs.HasKey (CSV_HIGHSCORE_KEY)) {
  87.  
  88.             //Get the CSV string from playerPrefs
  89.             string csv = PlayerPrefs.GetString (CSV_HIGHSCORE_KEY);
  90.  
  91.             //Return a List of strings, seperating the CSV by commas
  92.             return csv.Split (',').ToList ();
  93.         } else {
  94.             //Return blank List
  95.             return new List<string> ();
  96.         }
  97.     }
  98.     public static void ShowHighscores(){
  99.         List<string> hs = LoadHighscores();
  100.         for(int i = 1; i < hs.Count; i += 2){
  101.             Debug.Log(hs[i] + ":" + hs[i + 1]);
  102.                 }
  103.                 }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement