I’m doing a simulation where the user sets a number of parameters, and I want the user to be able to save those sets of parameters so they don’t have to set everything up again when they quit and restart the app. For each configuration there are 12 Vector3 and 13 float values that need to be saved.
What I’ve done is convert all the numbers to strings, concatenate them into one long string with delimiters, and save that string to PlayerPrefs. So each configuration is just one saved string, one key, that gets parsed when it is loaded in again. I thought this would be more efficient space-wise, since saving the 49 individual numbers would require each to have its own key. (Would be nice if there were a PlayerPrefs.SetVector3 function.)
But it seems to work inconsistently on iPad. I’m not sure if it’s the saving or the loading that’s the problem, but sometimes it works and sometimes it doesn’t. Seems to work consistently in Unity though.
Would I be better off saving the floats individually, so that each configuration has 49 separate keys? Or is there no advantage one way or the other?
Also, is there a limit to the size of a PlayerPrefs file on iPad. Do I need to limit the number of configurations that can be saved?
Check out PlayerPrefsX.js (now with C# version too)
// Site of this script: http://www.unifycommunity.com/wiki/index.php?title=ArrayPrefs
// Created by: Eric Haines (Eric5h5)
// Contribution (Set/Get Vector3) 03/2010: Mario Madureira Fontes (fontmaster)
So you’re saying I should save the individual values rather than a single string?
BTW, I saw ArrayPrefs in the wiki. Wasn’t sure I could use it from JS. Looks like it does the same thing I’m doing, converting the individual values to a string.
ArrayPrefs is JS, and all the examples are JS, so I’d guess it can be used from JS. Though someone added a C# version (which could also be used from JS, for that matter). It does convert values into strings, but it does it in functions that work like PlayerPrefs, so you don’t have to think about it. I don’t know about the iPad, but it works consistently on the iPhone. Setting values individually is slower and takes more space.
Maybe this should be another thread, but is there a way to get a list of the names of all saved keys? The user needs to be able to name their saved configurations, and I don’t know what those names will be. I need to get a list of all saved configuration on launch, and can’t use HastKey if I don’t know what the user called them. I tried just using a sequential numbering system for the key names, but that gets tricky when configurations are deleted by the user.
Yes, I was thinking I need a key key, a key with a list of keys.
Least of my problems now. Since I started messing with PlayerPrefs my app won’t even run on iPad anymore. It crashes after the Unity splash with a SIGABRT error. Can’t seem to get it back to the point where it ran. Of course, still works great in Unity.