Unity - PlayerPrefs.SetString appears to save garbage data

In Unity, I have an InputField calling this method for OnValueChanged:

public void SetUsername (string s) {
print(s);
}


print(s); succesfully prints the typed text, but PlayerPrefs.SetString ("username", s); is saving what looks like garbage data. After exiting the game, here's what the relevant line of the PlayerPrefs file looks like:

<pref name="username" type="string">ZHNmc2Rmc2FkZg==</pref>


ZHNmc2Rmc2FkZg== isn't the string I saved...

What's going on? What am I doing wrong?

This happens because Unity does not directly write the string to the PlayerPrefs file, instead, it serializes it into a format which can be saved without conflicting with the XML. For example, it would be bad if you tried to save the string </pref>.

This string will appear correctly once loaded with PlayerPrefs.GetString().

I figured this out while writing the question. I hope others can find this useful.

• Nice information to have, thanks for sharing this. Nov 16, 2017 at 20:31