Skip to content

Instantly share code, notes, and snippets.

@hybridherbst
Last active November 26, 2021 16:12
Show Gist options
  • Save hybridherbst/90d554dfb6791517acb2d75af6baf516 to your computer and use it in GitHub Desktop.
Save hybridherbst/90d554dfb6791517acb2d75af6baf516 to your computer and use it in GitHub Desktop.
Unity PlayerPref & EditorPref hash handling
// Unity hashes PlayerPref names to make them case-sensitive
// (Windows Registry isn't case sensitive otherwise)
private static string ToKey(string prefName)
{
return prefName + "_h" + Hash(prefName);
}
// djb2-xor hash method
private static uint Hash(string str)
{
uint hash = 5381;
if (string.IsNullOrEmpty(str)) return hash;
foreach (var c in str)
hash = hash * 33 ^ c;
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment