Advertisement
JonneOpettaja

Hiscores

Apr 19th, 2018
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class Hiscores : MonoBehaviour
  5. {
  6.     Dictionary<int, float> times = new Dictionary<int, float>();
  7.  
  8.     private void Start()
  9.     {
  10.         for (int level = 1; ; level++)
  11.         {
  12.             string key = "Time" + level;
  13.             if (!PlayerPrefs.HasKey(key))
  14.                 break;
  15.             times[level] = PlayerPrefs.GetFloat(key);
  16.         }
  17.     }
  18.  
  19.     public void StoreTime(int level, float time)
  20.     {
  21.         if (times.ContainsKey(level))
  22.         {
  23.             if (time < times[level])
  24.             {
  25.                 times[level] = time;
  26.             }
  27.             PlayerPrefs.SetFloat("Time" + level, time);
  28.         } else
  29.             {
  30.             times[level] = time;
  31.             }
  32.        
  33.     }
  34.  
  35.     public string GetTimes()
  36.     {
  37.         string timeString = "Best times:\n\n";
  38.         foreach (KeyValuePair<int, float> kvp in times)
  39.         {
  40.             timeString += "Level" + kvp.Key + ":" + kvp.Value.ToString("#.#") + "\n";
  41.         }
  42.         return timeString;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement