Contributions

Description

Provides a way to save and load arrays from PlayerPrefs. (Note: You probably want to use ArrayPrefs2 instead, which is generally faster and better.)

Usage

Have this script somewhere in your project. Ideally it should be in the Scripts folder in Standard Assets; this way it can be called from C# and Boo scripts. Call it PlayerPrefsX, and then you can use the following functions:

PlayerPrefsX.SetVector3

PlayerPrefsX.GetVector3

PlayerPrefsX.SetIntArray

PlayerPrefsX.GetIntArray

PlayerPrefsX.SetFloatArray

PlayerPrefsX.GetFloatArray

PlayerPrefsX.SetStringArray

PlayerPrefsX.GetStringArray

SetVector3

static function SetVector3 (key : string, value : Vector3) : boolean

Description

Sets the value of the preference identified by key. The value in this case is a Vector3. Returns false if a PlayerPrefsException occured (for example, trying to save more than 1MB of data when using the web player), otherwise returns true.

// Try to save a player object positionvar player : GameObject;if(!PlayerPrefsX.SetVector3("PlayerPosition", player.transform.position))print("Can't save a player position!");

GetVector3

static function GetVector3 (key : string) : Vector3

Description

Returns the value corresponding to key in the preference file if it exists.

SetIntArray

static function SetIntArray (key : string, value : int[]) : boolean

Description

Sets the value of the preference identified by key. The value in this case is an array of integers (int[]). Returns false if a PlayerPrefsException occured (for example, trying to save more than 1MB of data when using the web player), otherwise returns true.

// Makes an array of integers from 1 through 10, then saves the array to a PlayerPrefs entry named "Scores"var myScores =new int[10];for(i =0; i < myScores.Length; i++)
myScores[i]= i+1;if(!PlayerPrefsX.SetIntArray("Scores", myScores))print("Can't save scores");

GetIntArray

static function GetIntArray (key : string) : int[]

Description

Returns the value corresponding to key in the preference file if it exists.

SetFloatArray

Sets the value of the preference identified by key. The value in this case is an array of floats (float[]). Returns false if a PlayerPrefsException occured (for example, trying to save more than 1MB of data when using the web player), otherwise returns true.

// Makes an array of floats from 1.5 through 10.5, then saves the array to a PlayerPrefs entry named "Coordinates"var myCoords =new float[10];for(i =0; i < myCoords.Length; i++)
myCoords[i]= i+1.5;if(!PlayerPrefsX.SetFloatArray("Coordinates", myCoords))print("Can't save coordinates");

GetFloatArray

static function GetFloatArray (key : string) : float[]

Description

Returns the value corresponding to key in the preference file if it exists.

SetStringArray

Sets the value of the preference identified by key. The value in this case is an array of strings (String[]). Returns false if a PlayerPrefsException occured (for example, trying to save more than 1MB of data when using the web player), otherwise returns true.

By default, a line break is used as a separator when storing array entries. If your string array contains strings that include line breaks, then reading the array back would fail. Therefore, you can choose to supply a different separator character.

If you specified a separator character when saving a string array, you can supply it when reading that array back, and if key doesn't exist, it will return a String array of size defaultSize, filled with value defaultValue.