- Home /
Does Unity have an equivalent to browser cookies or Flash sharedObjects to save data?
Can Unity webplayers save data to the user's local PC, in the same way that Flash SWFs can save data using the actionscript "sharedObject" class, and that browser-based javascript can save data using cookies?
(the functions used to do this in Unity have been mentioned in a number of other answers on this site, but not specifically in the context of their equivalence to the Flash and Browser cookie functions used to save data).
Answer by duck · Jan 27, 2010 at 10:53 AM
Yes, web-based Unity content can save data to the local machine using the PlayerPrefs class. Each webplayer url can save up 1 megabyte of data, consisting of either ints, floats or strings. For example:
// to save an integer value PlayerPrefs.SetInt("Player Score", 10);
// to retrieve the integer value print (PlayerPrefs.GetInt("Player Score"));
And the commands are very similar for floats and strings:
PlayerPrefs.SetFloat("Player Lap Time", 29.3); print (PlayerPrefs.GetFloat("Player Lap Time"));
PlayerPrefs.SetString("Player Name", "Duck");
print ("Welcome back, " + PlayerPrefs.GetString("Player Name") + "!");
You can also query the player prefs data to see whether a certain key has been set, using PlayerPrefs.HasKey, and delete keys using either PlayerPrefs.DeleteKey or PlayerPrefs.DeleteAll.
was just about to reply to my own comment and say the same thing. works great. Thanks