- Home /
Multidimensional PlayerPrefs
Is there a method for saving Multidimensional arrays as PlayerPrefs? (PlayerPrefsX only gives the option to save 1-dimensional array)
For that matter, any option for transfering multidemsional arrays between scenes would be nice (doesn't neceseraly need to be PlayerPrefs)
Any help would be greatly appreciated.
On PlayerPrefs, no there should not be a way to have 2-d arrays for one big reason: memory usage. On the webplayer, you only get to save ~1$$anonymous$$B of data into PlayerPrefs. A pure 2-d array of ints with absolutely no wasted room (pointers, $$anonymous$$eys, ways to actually find the data inside it, etc.) could only hold a 256X256 array. Useful for small applications, but not for anything major.
If you really want store large amounts of data between scenes, store the data in a GameObject, and somewhere in that GameObject, call
DontDestroyOnLoad(this.gameObject);
DontDestroyOnLoad will preserve the GameObject and all data inside of it across levels. Just be careful you don't end up with two of them if you reload the original level.
I don't know why you'd assume 2D arrays are going to be any larger than 1D arrays. They surely could be, but if you wanted to store a noughts and crosses game (for example), they'd only need to be 3 3 2 bytes.
Using DontDestroyOnLoad is far better though, if you don't need persistence between sessions.
Answer by syclamoth · Sep 19, 2011 at 10:57 AM
As per the comments above, you should only be using PlayerPrefs for saving basic configuration info etc. between sessions- if you have large data structures that you only need to keep between scenes, calling DontDestroyOnLoad on the gameObject to which those scripts are connected is a much cleaner way of stopping things from getting deleted.
If you want to save larger quantities of data, you should look into C#'s inbuilt XML serialization libraries- you can save detailed game information into an xml file in a known location and load from that.
Your answer
Follow this Question
Related Questions
Multidimensional PlayerPrefs? 1 Answer
Help with Load function on Button press 1 Answer
Multidimensional array trouble 2 Answers
2D Animation does not start 1 Answer
Want to make a JRPG. 1 Answer