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?

Might help someone else if the above isn’t working…

If you had a previous build of the game that saved the playerprefs, your new build is probably picking that up from the Windows registry where player prefs are stored. It probably does something similar on Mac.


For example: this is where they are stored in the Windows registry for a game called “BTG 0.5”:
Computer\HKEY_CURRENT_USER\Software\DefaultCompany\BTG 0.5\

When I go there, I see keys for all my player prefs. You can then delete everything under the “BTG 0.5” key. If you are not comfortable operating on Windows registry, for safety, export a backup of the registry prior to changing anything.


The good news is that someone that has never run your build before won’t have this problem.

Hope it helps.

@Designosaurus

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");
	}
}

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;
}