Search Unity

Detect if an app is run the first time?

Discussion in 'iOS and tvOS' started by Crazy Robot, Aug 26, 2009.

  1. Crazy Robot

    Crazy Robot

    Joined:
    Apr 18, 2009
    Posts:
    921
    Is there a way to detect if an app is run the first time?

    I have a setting that is saved as a player pref, the issue is when I install the app for the first time and run it, the value defaults to 0. Is there any way to detect if the app has not been run before? What way I can set the value initially.

    Thanks,

    JL
     
    guetta18 likes this.
  2. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    Use PlayerPrefs.

    Psuedo Code:
    Code (csharp):
    1.  
    2. int hasPlayed = PlayerPrefs.GetInt( "HasPlayed");
    3.  
    4. if( hasPlayed == 0 )
    5. {
    6.    // First Time
    7.    // Initialize...etc
    8.  
    9.    PlayerPrefs.PutInt( "HasPlayed", 1 );
    10. }
    11. else
    12. {
    13.    // Not First Time
    14. }
    15.  
    16.  
     
    katinas15, xliquid and eriakul like this.
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can also check PlayerPrefs.HasKey for one of the usual prefs, if you're saving anything else. If it exists, then the game has been played before and the preference saved.

    --Eric
     
  4. Milad-Fathi

    Milad-Fathi

    Joined:
    Nov 9, 2012
    Posts:
    32
    Hi, How can I detect First run on Android?
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    This thread already explains how. The code will work for both iOS and Android.
     
  6. Milad-Fathi

    Milad-Fathi

    Joined:
    Nov 9, 2012
    Posts:
    32
    I tried the code works on windows perfectly but it doesn't work for android.
    Did you try it ?
     
  7. Agent_007

    Agent_007

    Joined:
    Dec 18, 2011
    Posts:
    899
    What value does int hasPlayed = PlayerPrefs.GetInt( "HasPlayed"); return on first run?
     
  8. Milad-Fathi

    Milad-Fathi

    Joined:
    Nov 9, 2012
    Posts:
    32
    the returned value is 0 on all runs for android but for windows the value changes from 0 into 1 on first run.
     
  9. Agent_007

    Agent_007

    Joined:
    Dec 18, 2011
    Posts:
    899
  10. kayb14

    kayb14

    Joined:
    Jun 12, 2016
    Posts:
    10
    does this check if the game has ever run before, or if this specific .exe file has been executed before?
    I need to check wether the player has "reinstalled" the game, better said redoawnloaded.
     
  11. A_munim

    A_munim

    Joined:
    Mar 11, 2017
    Posts:
    5
    This code wold check this also but if the user uninstalled the game completely(even deleted the registry like I do) then you have no chance of chasing him
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    androids need .Save() explicitly called in my experience.
     
  13. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Then use a server and code all that. Otherwise no.
     
  14. lagzilajcsi

    lagzilajcsi

    Joined:
    Jan 19, 2020
    Posts:
    9
    Code (CSharp):
    1. public class LoadSaveManager : MonoBehaviour
    2. {
    3.     public int IsFirst;
    4.  
    5.     void Start ()
    6.     {
    7.         IsFirst = PlayerPrefs.GetInt("IsFirst") ;
    8.         if (IsFirst == 0)
    9.         {
    10.             //Do stuff on the first time
    11.             Debug.Log("first run");
    12.             PlayerPrefs.SetInt("IsFirst", 1);
    13.         } else {
    14.             //Do stuff other times
    15.             Debug.Log("welcome again!");
    16.         }
    17.     }
    18. }