Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
4
Question by Designosaurus · Apr 17, 2016 at 11:36 PM · c#playerprefsdatastandalone

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image krisventure · Apr 15, 2017 at 03:06 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Toon_Werawat · Apr 18, 2016 at 08:57 PM

@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");
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image k_kuban · Feb 27, 2020 at 09:11 AM 0
Share

Thanks for this! But where do you call the script to make sure it is only called once after build?

avatar image
1

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;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

147 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Update and Get PlayerPrefs during runtime? 0 Answers

How to do + or - functions to PlayerPrefs.SetInt? 1 Answer

First Person Save And Load Rotation 0 Answers

How to save player1's stats separate from player2's i.e. Health, Damage, Etc.. 1 Answer

Continue from where you left with PlayerPRefs 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges