PFonseca

Developer

View site on GitHub Statuses Archives

How to persist simple data on Unity

There is a way to persist simple data like max score. It's possible to set, get and remove data.
Available Methods:

DeleteAllRemoves all keys and values from the preferences. Use with caution.
DeleteKeyRemoves key and its corresponding value from the preferences.
GetFloatReturns the value corresponding to key in the preference file if it exists.
GetIntReturns the value corresponding to key in the preference file if it exists.
GetStringReturns the value corresponding to key in the preference file if it exists.
HasKeyReturns true if key exists in the preferences.
SaveWrites all modified preferences to disk.
SetFloatSets the value of the preference identified by key.
SetIntSets the value of the preference identified by key.
SetStringSets the value of the preference identified by key.

How to use:
// get a value stored in a constante "score"
int scoreValue = PlayerPrefs.GetInt("score");

// increment score
scoreValue+=10;

// Store value on a constante "score"
PlayerPrefs.SetInt("score", scoreValue);

Source: http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

Published on 07/01/2015 at 02h26 by pfonseca, tags

If you liked this article you can follow me on Twitter

comment How to persist simple data on Unity

Trackbacks are disabled

Powered by Publify