- Home /
PlayerPrefs - Can I save more than one?
Hey Unity Answers,
I am a c# dev that has recently started working with a small local indie game company. They have brought me on at the end of their game dev. cycle to help them shore up a few things, as the entire game was made without having a programmer on board.
The game currently tracks all the variables in playerprefs and saves them on exit, resets them on new game, and loads the previous game sate on load. I need to know if it is possible to save this file into a specific location, separate from appdata, and if I can save separate playerprefs and load them independently. Please let me know if this is possible b/c I would rather not have to rework their code to serialize all the game object s and create game saves that way. Thanks!
A bit more specific info would help, ie how it "saves them on exit".
Apart from that with Application.dataPath or persistentDataPath you can get drive locations, and from there perhaps navigate around. You would have to nosey around the $$anonymous$$icrosoft File Documentation for that.
For my Project I had to make a fairly extensive system for detecting, showing and loading all the txt files in a single folder, it can be a challenge.
Have you checked out the tutorial on saving and persistent data?
A bit more specific info would help, ie how it "saves them on exit".
So currently the game saves all variables to playerprefs, and our OnExit() script calls PlayerPrefs.Save(). I know this is not good practice, as it is very slow, and I have suggested changing this. But for the time now I need to create multiple save states using player prefs.
Have you checked out the tutorial on saving and persistent data?
Thanks for pointing this out. I have looked at this before, but I finally sat down to watch the video, and this is the approach I would have used from the beginning as I am familiar with serialization.
As for now I am looking for solutions that can read playerprefs, so I do not have to change the rest of the games's code.
Answer by rutter · Aug 18, 2014 at 10:35 PM
The answer to your immediate question is no: there is exactly one PlayerPrefs file, and you cannot control its location.
However, there are other solutions. You could write an abstraction layer. Any script that currently gets or sets values from PlayerPrefs could instead go through some class you write.
Maybe that class adds an extra prefix to each PlayerPrefs key, simulating multiple profiles:
public static class MultiProfile {
private static string currentProfile = "default";
public static int GetInt(string key, int defaultValue) {
return PlayerPrefs.GetInt(currentProfile + key, defaultValue);
}
}
Or, maybe your class serializes its data to some other format, like XML, JSON, or SQL, then reads and writes a file you control directly. Don't forget that you have the full capabilities of the .NET framework -- many powerful serialization libraries exist!
If you're on mobile or web, your options are a bit more limited. Unity iOS doesn't support the use of JIT-compiled code, which knocks out a lot of reflection-based libraries. If you're on web, you don't have direct file access and your PlayerPrefs size limit is much lower.
Thanks for the quick response Rutter. This is what I was afraid of. I have not worked with unity before, and I assumed you could not do this since I found no other solutions online.
I will look into a possible abstraction. Thanks for the suggestions.