x


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).

more ▼

asked Jan 27 '10 at 10:53 AM

duck gravatar image

duck ♦♦
40.9k 92 148 415

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jan 27 '10 at 10:53 AM

duck gravatar image

duck ♦♦
40.9k 92 148 415

Looks handy. What about in the standalone versions?

Dec 01 '10 at 12:28 PM Bill 2

Works just the same in standalone builds!

Dec 01 '10 at 01:08 PM duck ♦♦

was just about to reply to my own comment and say the same thing. works great. Thanks

Dec 01 '10 at 01:23 PM Bill 2
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x435
x56

asked: Jan 27 '10 at 10:53 AM

Seen: 3804 times

Last Updated: Jan 27 '10 at 10:53 AM