Advertisement
Guest User

Untitled

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