Advertisement
Guest User

Unity High score tracking

a guest
Feb 23rd, 2021
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. // by @kurtdekker - ultra simple best score backing store
  4.  
  5. public static partial class TheBest
  6. {
  7.     // for scores: higher is better
  8.     private static string s_HighScore = "HighScore";
  9.  
  10.     public static bool HaveBestScore
  11.     {
  12.         get
  13.         {
  14.             return PlayerPrefs.HasKey(s_HighScore);
  15.         }
  16.     }
  17.  
  18.     public static float BestScore
  19.     {
  20.         get
  21.         {
  22.             return PlayerPrefs.GetFloat(s_HighScore, 0);
  23.         }
  24.         private set     // make this public if you trust yourself
  25.         {
  26.             PlayerPrefs.SetFloat(s_HighScore, value);
  27.         }
  28.     }
  29.  
  30.     public static bool RecordScoreIfHigher(int score)
  31.     {
  32.         if (!HaveBestScore || (score > BestScore))
  33.         {
  34.             BestScore = score;
  35.             return true;
  36.         }
  37.         return false;
  38.     }
  39.  
  40.     public static void ClearBestScore()
  41.     {
  42.         if (HaveBestScore)
  43.         {
  44.             PlayerPrefs.DeleteKey(s_HighScore);
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement