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!

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.