Search Unity

Count the times a game has been launched

Discussion in 'Scripting' started by Simonxzx, Jun 13, 2015.

  1. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Hi everyone, I would like to save in PlayerPrefs the time my game has been launched by the user. Is there any function to do it ? Thank you very much.
     
  2. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    There are many ways to do this,
    here is a super simple method to keep check

    Code (CSHARP):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class GameLaunchCounter : MonoBehaviour
    5. {
    6.  int launchCount;
    7.  
    8.  void Awake()
    9.  {
    10.  // Check For 'TimesLaunched', Set To 0 If Value Isnt Set (First Time Being Launched)
    11.  launchCount = PlayerPrefs.GetInt ("TimesLaunched", 0);
    12.  
    13.  // After Grabbing 'TimesLaunched' we increment the value by 1
    14.  launchCount = launchCount + 1;
    15.  
    16.  // Set 'TimesLaunched' To The Incremented Value
    17.  PlayerPrefs.SetInt("TimesLaunched", launchCount);
    18.  
    19.  // Now I Would Destroy The Script Or Whatever You
    20.  // Want To Do To Prevent It From Running Multiple
    21.  // Times In One Launch Session
    22.  Destroy (this);
    23.  }
    24. }
     
    suIly and AlucardJay like this.
  3. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    http://docs.unity3d.com/ScriptReference/PlayerPrefs.HasKey.html
    http://docs.unity3d.com/ScriptReference/PlayerPrefs.GetInt.html
    http://docs.unity3d.com/ScriptReference/PlayerPrefs.SetInt.html


    Code (csharp):
    1. void Awake()
    2. {
    3.    IncrementLaunchCount();
    4. }
    5.  
    6. void IncrementLaunchCount()
    7. {
    8.    // check if the key exists.
    9.    // if so, add to count
    10.    // if not, first time launched, add key
    11.    if ( PlayerPrefs.HasKey( "LaunchCount" ) )
    12.    {
    13.      // get the current count
    14.      int lc = PlayerPrefs.GetInt( "LaunchCount" );
    15.      // increment the count
    16.      lc += 1;
    17.      // set to PlayerPrefs
    18.      PlayerPrefs.SetInt( "LaunchCount", lc );
    19.    }
    20.    else
    21.    {
    22.      // if not, first time launched, add key
    23.      PlayerPrefs.SetInt( "LaunchCount", 1 );
    24.    }
    25. }
    EDIT : whoops, sorry 5vStudios, was typing this up when you posted :)
     
  4. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
    Well, thank you very much guys ! :)
     
  5. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Haha, nice, it seems we both program similarly
     
    AlucardJay likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Yes, what all the good guys above said... the only thing I would add is this, in order to make sure you don't have typos in the key string:

    Code (csharp):
    1. const string s_LaunchCount = "LaunchCount";
    Then make every use of SetInt and GetInt and HasKey use s_LaunchCount instead of yet another string literal.

    That way a) you're immune to typos in the key string, which would introduce odd confusing behavior, and b) you can easily change the key name in the future by changing a single location.
     
  7. Simonxzx

    Simonxzx

    Joined:
    Feb 22, 2015
    Posts:
    129
  8. Burn0815

    Burn0815

    Joined:
    Jul 3, 2019
    Posts:
    11
    This is already be done by UNITY! I recognized this when I first used "BG Tools - Player Prefs Editor":
    PlayerPrefs.GetString("unity.player_session_count")
    Interesting to mention: PlayerPrefs.DeleteAll() will not reset this!

    this works on Unity Standalone AND also on Mobile (at least as I can say on Android)!
    (Unity 2019.4.15)
     
    MadMax-9877 and Joe-Censored like this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Today I learned.

    Well, I think I'd rather just track such a simple thing myself so when debugging I can easily reset it.

    Generally, here's a more-robust example of simple persistent loading/saving values using PlayerPrefs:

    https://pastebin.com/icJSq5zC

    Useful for a relatively small number of simple values.
     
    Joe-Censored likes this.