2
\$\begingroup\$

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

public void SetUsername (string s) {
    username = s;
    print(s);
    PlayerPrefs.SetString ("username", 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?

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Nice information to have, thanks for sharing this. \$\endgroup\$ – John Hamilton Nov 16 '17 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.