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 /
avatar image
0
Question by Ian9921 · Sep 14, 2018 at 04:37 AM · javascriptplayerprefsresetdeleteclear

Is there a way to clear all the PlayerPrefs with names that start with a specific string?

Basically title. I'm making a sandbox game where I'm using PlayerPrefs to store the positions of individual blocks. The player has the option to "delete" any of their worlds, and when this happens, I need to clear out all the PlayerPrefs that start with the name of the world (which is how I keep the different world's prefs separate). Also note I am working in JavaScript

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Stratosome · Sep 14, 2018 at 06:12 AM

Hola!

Alright, so after a little research, I'm thinking there is no easy way to grab all of the PlayerPrefs that start with blank. I was initially thinking that if we could manage to get an array of all of the keys in PlayerPrefs, we could then loop through that, find the ones that started with the world name, and delete those. UNFORTUNATELY, there doesn't appear to be a way to really get all of the keys. The best way might be to somehow keep track of the PlayerPrefs settings you make and then reference that to delete all of the existing settings.


Here's an interesting idea. Let's say you have a couple worlds. You'd normally have a list of PlayerPrefs settings like this, right?

 PlayerPrefs ["FluffyForest_DeathCount", 7]
 PlayerPrefs ["FluffyForest_EnemyCount", 30]
 PlayerPrefs ["FluffyForest_TreeCount", 506]
 
 ...
 
 PlayerPrefs ["MurkyMarsh_DeathCount", 12]
 PlayerPrefs ["MurkyMarsh_EnemyCount", 18]
 PlayerPrefs ["MurkyMarsh_LostItems", 3]

WHAT IF, maybe you had a special PlayerPrefs variable to hold the other settings? So, somethin' like this for FluffyForest here:

 PlayerPrefs ["FluffyForest_Settings", 
     "DeathCount, EnemyCount, TreeCount"
 ]
 
 PlayerPrefs ["FluffyForest_DeathCount", 7]
 PlayerPrefs ["FluffyForest_EnemyCount", 30]
 PlayerPrefs ["FluffyForest_TreeCount", 506]

With this special PlayerPrefs thing, FluffyForest_Settings, every time we add or remove a new setting for FluffyForest, we can add add or remove the setting names from/to the FluffyForest_Settings variable. So if ya want to remove all of the FluffyForest settings, just grab all of the settings names from the FluffyForest_Settings variable like this:

 string targetWorld = "FluffyForest";
 string worldSettings = PlayerPrefs.GetString("FluffyForest_Settings", "");

Now with this string of settings, you can split it to get an array of the settings, and then loop through the array deleting the PlayerPrefs stuff like this:

 string[] settings = [Your Split String]; 
 
 for (int i = 0; i < settings.Length; i++) {
     PlayerPrefs.DeleteKey(targetWorld + settings[i]);
 }
 
 PlayerPrefs.DeleteKey(targetWorld + "_Settings"); 



Somethin' like that. Just an idea I had. Hopefully it kind of makes sense hah. That's about the best way I can think to solve this at the moment.

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

142 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

Related Questions

Deleting PlayerPrefs Data 1 Answer

problem incrementing player prefs 2 Answers

Playerprefs don't reset after reinstalling the build on my phone. 1 Answer

Programming A Reset Scene 0 Answers

How many variables can I store using PlayerPrefs 1 Answer


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