Advertisement
Guest User

Unity best time tracking

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