ArrayPrefs2

Author: Eric Haines

Description
Provides a way to save and load arrays of various types from PlayerPrefs, plus Vector2, Vector3, Quaternion, and Color. Also integrates boolean saving from BoolPrefs, with a bit of spiffing up. ArrayPrefs2 supersedes the original ArrayPrefs because it's generally faster, more robust, and usually takes less space when saving. (Except string arrays, but they're still more robust anyway.) Note that preferences saved with ArrayPrefs are not compatible with ArrayPrefs2. Requires Unity 3.0.

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


 * PlayerPrefsX.SetBool
 * PlayerPrefsX.GetBool
 * PlayerPrefsX.SetLong (fixme: C# only)
 * PlayerPrefsX.GetLong (fixme: C# only)
 * PlayerPrefsX.SetVector2
 * PlayerPrefsX.GetVector2
 * PlayerPrefsX.SetVector3
 * PlayerPrefsX.GetVector3
 * PlayerPrefsX.SetQuaternion
 * PlayerPrefsX.GetQuaternion
 * PlayerPrefsX.SetColor
 * PlayerPrefsX.GetColor
 * PlayerPrefsX.SetIntArray
 * PlayerPrefsX.GetIntArray
 * PlayerPrefsX.SetFloatArray
 * PlayerPrefsX.GetFloatArray
 * PlayerPrefsX.SetBoolArray
 * PlayerPrefsX.GetBoolArray
 * PlayerPrefsX.SetStringArray
 * PlayerPrefsX.GetStringArray
 * PlayerPrefsX.SetVector2Array
 * PlayerPrefsX.GetVector2Array
 * PlayerPrefsX.SetVector3Array
 * PlayerPrefsX.GetVector3Array
 * PlayerPrefsX.SetQuaternionArray
 * PlayerPrefsX.GetQuaternionArray
 * PlayerPrefsX.SetColorArray
 * PlayerPrefsX.GetColorArray

Vector2, Vector3, Quaternion, Color, Bool
GetVector2/SetVector2, plus Vector3, Quaternion, Color, and Bool, work pretty much like they would if they were part of PlayerPrefs. Namely, setting a value with a key stores that value, which can be retrieved later.

Save a string: explicitly declare the string data type or you may get missing method exception error (u3d 4.1)

The functions return true if they succeeded, or false if a PlayerPrefsException occurred (like trying to save more than 1MB of data when using the web player). So you might want to check the return value.

When reading keys, they return the default values for the appropriate type if the key doesn't exist.

If you'd rather have a different default, you can specify that instead:

Arrays
You can also save arrays of int, float, boolean, String, Vector2, Vector3, Quaternion, and Color. Saving works the same as above when setting variables:

Again, you can check the return value to see if the save succeeded. Note that these functions work with built-in arrays rather than Lists, but you can convert Lists to built-in arrays easily enough:

Loading variables is also basically the same:

There are a couple of differences. If the key doesn't exist, then an empty array of the appropriate type is returned. In the example above, if "Numbers" doesn't exist, then numberArray will be the same as if you did "var numberArray = new int[0];". You can specify a default value for the array if the key doesn't exist, but since it's an array, you also have to specify how many entries it should have as the third parameter:

In this case, if "Numbers" doesn't exist, then numberArray will be an int array with 10 elements, and each element will contain -1.

Note: when saving string arrays, each string can have a maximum of 255 characters. Also, the type of the array is saved along with the array, so trying to do "PlayerPrefsX.GetFloatArray ("Numbers")" won't work, assuming you used an int array when saving to "Numbers".

ShowArrayType
In the event that you have a saved array and you're not sure what type it is, you can use ShowArrayType and it will tell you:

Usage Note
Even though these functions make it easy to save arrays, PlayerPrefs is more suitable for small amounts of data, like locally-stored top 10 lists and that sort of thing. PlayerPrefsX isn't intended to turn PlayerPrefs into a database system. If you have large amounts of data, you should use standard disk IO functions instead.

How it Works
PlayerPrefsX is built on PlayerPrefs.SetString, which is really the only way in Unity to save a preferences entry as an array (specifically, an array of chars). So, when saving arrays of floats and ints, each number is converted to 4 bytes and stored in a byte array. The total byte array is then converted to base64, and saved as a string. When saving a boolean array, each boolean is converted to a single bit in a byte array, which again is converted to a base64 string and saved. With string arrays, first an index array of bytes is built, where each byte contains the length of the respective string entry from the string array. This is converted to a base64 string, then all the entries in the string array are smooshed into a single string and appended to the index array. Vector2, Vector3, Quaternion, and Color are built on PlayerPrefsX.SetFloatArray.

Even though System.BitConverter is not endian-aware, PlayerPrefsX is. If you load a preferences file from a big-endian system into a little-endian system (or vice versa), it will still work. While mostly theoretical, this is actually a real, if small, possibility, such as someone upgrading from an old PPC Mac to a newer Intel Mac. Anyway, you don't have to worry about it, but that's what all the funky stuff in the code with endianDiff and byteBlock is all about.

EditorPrefsX
This script will also work for saving arrays to the EditorPrefs. Take the latest copy of PlayerPrefsX below and do a search and replace for all instances of "PlayerPrefs", renaming them to "EditorPrefs". Also, because only one Enum can exist in the project with one name, do a search and replace "ArrayType" with "EditorArrayType" (be careful not to rename the function 'ShowArrayType' near the bottom). Save the file as EditorPrefsX.js and place in Editor folder.

C# - PlayerPrefsX.cs
For those who prefer C# I've transcribed it over. There may be some transcription errors in there as I haven't tested all code paths, but what I have tested does work.