Hi, is there any way to include some default player prefs with your app when it’s installed on the device? I know I caould ad them through code when the app starts, but there’s 130 of them and and it causes a noticable hiccup
Thanks
Hi, is there any way to include some default player prefs with your app when it’s installed on the device? I know I caould ad them through code when the app starts, but there’s 130 of them and and it causes a noticable hiccup
Thanks
Do you need all the values initialized right away? If not, you could only write default values when each individual key is read.
PlayerPrefs already does this:
var musicVolume = PlayerPrefs.GetFloat("MusicVolume", .5);
If the key doesn’t exist, then .5 is used in this case.
–Eric
Yeah, they all have to be there. It is for OpenFeints online save features and it crashes if one of the player prefs isnt there. And you pass it a predefined array of strings
Could you show us how you are handling the OF save that’s causing it to crash out? I’m not exactly sure why Eric’s suggestion of PlayerPrefs.GetFloat(“MusicVolume”, .5); wouldn’t work for you.
Well, I decided to axe the online save feature. It’n not that big of a deal. OpenFeint has two functions to download and upload player prefs
OpenFeint.UploadBlobFromPlayerPrefs(“PlayerStats”, blobDataKeys);
OpenFeint.DownloadBlobFromPlayerPrefs(“PlayerStats”, blobDataKeys);
Well, I decided to axe the online save feature. It’n not that big of a deal. OpenFeint has two functions to download and upload player prefs
OpenFeint.UploadBlobFromPlayerPrefs(“PlayerStats”, blobDataKeys);
OpenFeint.DownloadBlobFromPlayerPrefs(“PlayerStats”, blobDataKeys);
don’t see what prevents you fromt storing the “blob” right before you upload it there actually as that will be a non-realtime situation any (ie stuttering is of no importance)
also I thought that OF had a direct string - hashtable upload feature
Those are the only two functions for uploading and downloading from the player prefs. And when I do it I get a stutter cause there’s about 130 of them. What do you mean by sore the blob before upload?
Well I was talking more about real functionality, not preferences sharing.
PlayerPrefs and the underlying NSUserDefaults of iOS are for preferences as their name implies, a “few” keys of limited size. Not for game saves or at worst multi user profiles with stored game saves.
They aren’t optimal nor recommendable for storing large datablobs (you will find out more sooner than later why, at latest when you get your first 1 star rating due to data lose or data fuckups), thats what files are for (System.IO.File ReadAllBytes / ReadAllText and the corresponding writes), to be used with either the applications Documents or Caches folder
wait, so your saying I shouldn’t be using playerPrefs for my game saves? I have 120 levels and when I unlock a level I set an int to 1 in the player prefs with the level name. Am I doing this wrong? Why would it cause data loss?
If there’s is a better way or more proper way to do this, please share/elaborate. I’m unfamiliar with the methods you mentioned above. Are you saying I should be saving this info in a file? What type of file would I be putting it in? Like just a text file or something?
Thanks
I’ve been using playerPrefs to save game progress for a couple of years and never had any trouble… that is until recently. I have heard a couple people report a lost save in our most recent release, Battleheart, which saves quite a lot of data in the prefs. Of course its never failed for us or 99.9% of our users, but even one case of a lost save I find a bit alarming, so I too would be interested in people’s take on an alternative.
How would one go about writing and reading a simple text file on iOS in Unity? I’m assuming this would require some Objective-C shenanigans.
No need for object c shaningans, actually not even favorable if you ask me as you would need to do the same work twice.
The only thing where ObjC comes in is to reliably get the documents / cache folder - i posted the function to do so somewhen last year or so.
All you need is System.IO.File.ReadAllText / System.IO.File.WriteAllText and something and a function that serializes the objects you want to store / retrieve to the format you want to store them in.
For XML, if you use system.xml, it would be extremely easy, for other formats it depends on how you approach it.
Generally if it goes to text data in its serialization, it will save and restore without problems (playerprefs → nsuserdefaults goes to plist / xml)
The following 2 static functions which I use in the current project for webservice communication and local data caching allow you to get an xml representing an object or get an object from its xml representation with a single function call:
/// <summary>
/// Method to convert a custom Object to XML string
/// </summary>
/// <param name="pObject">Object that is to be serialized to XML</param>
/// <returns>XML string</returns>
public static String SerializeObject(System.Object pObject)
{
try
{
String _XmlizedString = null;
MemoryStream _memoryStream = new MemoryStream();
XmlSerializer _xs = new XmlSerializer(pObject.GetType());
XmlTextWriter _xmlTextWriter = new XmlTextWriter(_memoryStream, Encoding.GetEncoding("ISO-8859-1") );
_xs.Serialize(_xmlTextWriter, pObject);
_memoryStream = (MemoryStream)_xmlTextWriter.BaseStream;
_XmlizedString = ByteArrayToString(_memoryStream.ToArray());
return _XmlizedString;
}
catch (Exception e)
{
System.Console.WriteLine(e);
return null;
}
}
/// <summary>
/// Method to reconstruct an Object from XML string
/// </summary>
/// <param name="pXmlizedString"></param>
/// <returns></returns>
public static T DeserializeObject<T>(String pXmlizedString)
{
XmlSerializer _xs = new XmlSerializer(typeof(T));
MemoryStream _memoryStream = new MemoryStream(StringToByteArray(pXmlizedString));
//XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.GetEncoding("ISO-8859-1") );
return (T)_xs.Deserialize(_memoryStream);
}
if you search the web for the SerializableDictionary class you can even store full dictionaries in a single go.
Through usage of the attributes XmlArray and XmlArrayItem you can push through pretty complex data structures to xml and get it back from them.
Thanks for your explanation. Too bad we need to use Objective C save something on an iPhone in a proper way. I hope this will be fixed in a next build of Unity; they should just serialize the PlayerPrefs hashmap and save it into a file (or provide an other cross platform way of saving data).