Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

PlayerPrefs.DeleteAll() not working on Android!

Discussion in 'Scripting' started by Reizla, Aug 22, 2016.

  1. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    I'm moving from UnityEngine.PlayerPrefs() to PreviewLabs.PlayerPrefs() because I'm in need of encoded PlayerPrefs in my Android game. Since it's an update of an existing game I'm using the following code (in my splash screen obviously) to get this done:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using PlayerPrefs = PreviewLabs.PlayerPrefs;
    4.  
    5.  
    6. public class SplashScreen : MonoBehaviour {
    7.     static public string MyUserName { get; set; }
    8.     static public string MyUserHash { get; set; }
    9.     static public int MyGameSettings { get; set; }
    10.     static public string MyDateCreated { get; set; }
    11.  
    12.     // Use this for initialization
    13.     void Awake () {
    14.         #if !UNITY_EDITOR
    15.         PlayerPrefs.EnableEncryption(true);         // Use encryption for PlayerPrefs()
    16.         #else
    17.         PlayerPrefs.EnableEncryption(false);         // DO NOT use encryption for PlayerPrefs() in the editor!!!
    18.         #endif
    19.         if (UnityEngine.PlayerPrefs.HasKey("UserName")) {         // Conversion of the old PlayerPrefs() only. There's a check if PlayerPrefs() are stored in ReadGameData.cs
    20.             // Read the old PlayerPrefs()
    21.             MyDateCreated=UnityEngine.PlayerPrefs.GetString("DateCreated");
    22.             MyGameSettings=UnityEngine.PlayerPrefs.GetInt("GameSettings");
    23.             MyUserHash=UnityEngine.PlayerPrefs.GetString("UserHash");
    24.             MyUserName = UnityEngine.PlayerPrefs.GetString ("UserName");
    25.             UnityEngine.PlayerPrefs.DeleteAll();
    26.             // Create the new (on Android encrypted) PlayerPrefs()
    27.             PlayerPrefs.SetString("DateCreated", MyDateCreated);
    28.             PlayerPrefs.SetInt("GameSettings", MyGameSettings);
    29.             PlayerPrefs.SetString("UserHash", MyUserHash);
    30.             PlayerPrefs.SetString("UserName", MyUserName);
    31.             PlayerPrefs.Flush();
    32.         }
    33.     }
    34. }
    This piece of code works flawlessly in the editor, but when I run it under Android, the game keeps reverting back to the old (UnityEngine.)PlayerPrefs(), and thus keeps converting it.

    By now I've run the game about a dozen times on my (Android) phone, and the old PlayerPrefs are not deleted, despite the command in line 25. I have even added a line afterwards containing UnityEngine.PlayerPrefs.Save() (which I know is just plain silly) to no avail. In the end I've added a small UI thingy to my game where it displays:
    Code (csharp):
    1.         string tempText;
    2.         tempText="[";
    3.         tempText+=UnityEngine.PlayerPrefs.GetString("DateCreated")+"]\n[";
    4.         tempText+=UnityEngine.PlayerPrefs.GetString("UserHash")+"]\n\n[";
    5.         tempText+=PlayerPrefs.GetString("DateCreated")+"]\n[";
    6.         tempText+=PlayerPrefs.GetString("UserHash")+"]";
    7.         myText.text=tempText;
    and sadly ALL lines contain data, which indicates that the UnityEngine.PlayerPrefs.DeleteAll() is bugged under Android.

    By now I've taken things the other way around, where I check:
    Code (csharp):
    1. if (!PlayerPrefs.HasKey("UserName"))
    for line 19, which should be working fine (did not yet test it, but I suspect it'll be working flawlessly).

    Needless to say that in the Unity Engine it's working fine, and the Windows PlayerPrefs() are deleted normally...

    Running Unity 5.3.4f1, but I doubt this bug has been noticed (and reported) before.
     
  2. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    My workaround is working as it should...

    Also a note that PlayerPrefs.DeleteKey() is not working either on Android :(
     
unityunity