Reset PlayerPrefs on Build?
So I have a proper "New game"/"Continue game" system working with playerprefs, but upon building the game any data I might have created while working in editor is saved with it.
When I make a new build of the game, I need the playerprefs data to either be deleted or reset to its default values. The data system is set up so that the player can close the game and reopen it to find any previous data they made still in place.
What's the easiest way I can do that?
I want the exact same thing. This looks like a Unity bug. PlayerPrefs directly transfers to Android SharedPrefs, something that most people want to be reset upon new installs, who would use it to transfer settings from Unity editor. Clearing it manually is not a safe solution, I may forget to clear it just in one game update and all my new users will inherit my last in-Unity settings. What a mess. This should be reset by Unity upon build by default.
Answer by Toon_Werawat · Apr 18, 2016 at 08:57 PM
These were a way to delete all playerPref
PlayerPrefs.DeleteAll();
And you can try redirect all your setting. by check if you are in editor. Like this
if (Application.isEditor) //Does application is running in editor?
{
PlayerPref.SetInt("Editor-someSetting", 0);
}
else //If not in editor
{
PlayerPref.SetInt("someSetting", 0);
}
But.... what if I prefer you to replace all your current PlayerPref with JSON?
Anyway. If you want it or not. I'll give you a look anyway.
Here is setting script (Don't attach this to GameObject)
using System.IO;
using UnityEngine;
public class UserSettings
{
public string setting1;
public int setting2;
public float setting3;
public UserSettings() //Initialize new Setting
{
setting1 = "New setting";
setting2 = 0;
setting3 = 0f;
}
//Save setting to json file
public static void Save(UserSettings info, string filename)
{
//Extent filename to actual path
if (!filename.StartsWith("/"))
{
filename.Insert(0, "/");
}
filename = filename.Insert(0, Application.persistentDataPath);
//Check if file exist or not. If it exist. Delte it. Because we about to save a new one.
if (File.Exists(filename))
{
File.Delete(filename);
}
//Turn all user setting to Json. (as a string json)
string todisk = JsonUtility.ToJson(info);
//Write json string to disk
using (FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.Write))
{
StreamWriter wt = new StreamWriter(fs);
wt.Write(todisk);
wt.Close();
}
}
//Load setting from json file
public static UserSettings Load(string filename)
{
//Extent filename to actual file
if (!filename.StartsWith("/"))
{
filename.Insert(0, "/");
}
filename = filename.Insert(0, Application.persistentDataPath);
//Check if that setting file was exist or not
if (File.Exists(filename))//If exist
{
//Read that file and return it
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
StreamReader rd = new StreamReader(fs);
string fromdisk = rd.ReadToEnd();
return JsonUtility.FromJson<UserSettings>(fromdisk);
}
}
else //If not exist
{
//Return new setting
return new UserSettings();
}
}
}
And here is how to use it. Attatch this script in scene
using UnityEngine;
public class MainGameClass : MonoBehaviour
{
//This is a setting collection variable;
public UserSettings currentSetting;
//Load setting when start game
void Awake()
{
if (Application.isEditor)
{
currentSetting = UserSettings.Load("Editor.json");
}
else
{
currentSetting = UserSettings.Load("user1.json");
}
}
//Use the setting
void UseSetting()
{
currentSetting.setting1 = "New setting apply...";
currentSetting.setting2 = 69;
currentSetting.setting3 = 6.9f;
}
//Reset setting
void ResetSetting()
{
currentSetting = new UserSettings();
UserSettings.Save(currentSetting,"user1.json");
}
//Save your setting
void SaveGameSetting()
{
UserSettings.Save(currentSetting, "user1.json");
}
}
Thanks for this! But where do you call the script to make sure it is only called once after build?
Answer by luislodosm · Apr 12, 2017 at 07:51 PM
bool firstPlay;
void Awake ()
{
if (Application.isEditor == false) {
if (PlayerPrefs.GetInt ("FirstPlay", 1) == 1) {
firstPlay = true;
PlayerPrefs.SetInt ("FirstPlay", 0);
PlayerPrefs.Save ();
} else
firstPlay = false;
}