- Home /
Check the size of PlayerPrefs from web player
Is there any way to check the current size of PlayerPrefs from the webplayer?
I'm currently working on a web based game. The idea is to store some data locally to avoid frequent calls to the server. I can see some situations where multiple users on the same computer can overload the capacity of PlayerPrefs.
I realise I could just catch the error when it occurs, but I would prefer to know what space is available before the limit is reached.
Answer by Wicketd · Aug 19, 2014 at 12:45 AM
Depending on your data, it might be a better solution to store the data with an alternative method instead. The purpose of PlayerPrefs is self-describing: **player pref*erences*. This could be useful for, just throwing ideas out there, saving resolution and language preferences, for example. You could, however, utilise data serialisation. Here is a good start. If this doesn't suffice, you could look into saving and loading data to and from your server manually.
But, to answer your original question: This isn't possible out of the box as far as I know, at least not all that easily, and probably not all that efficiently. But, if you insist on it, you could create a helper class for managing PlayerPrefs and implement (static) methods for adding/removing/loading/... keys here and store them in something like a dictionary, so you can implement a method to loop through them and calculate a size total at any given time. This comes with one question, however: What IS the maximum size? Besides, I honestly doubt that if you are using PlayerPrefs reasonably, you'll have the issue you are describing.
I would take an alternate route if I were you.
Thanks for your thoughts. $$anonymous$$aybe it is a non issue and I'm over optimising to start with. I was going to try sneak some small textures in, that might well classify as unreasonable.
I'm reasonably familiar with serialisation and intend to use that to generate the data for playerprefs.
Your suggestion on a helper class pretty much aligns with what I was thinking. I was considering loading the entire playerprefs and checking against size there. I was hoping someone knew of an example somewhere that does the job.
I might just implement error catching and then fix it if it ever becomes an issue.
This right here is heavy pseudocode, but you get the idea. This way you can call PlayerPrefs$$anonymous$$anager.AddEntry without having to worry about exceeding the maximum size. This pseudocode is assu$$anonymous$$g an unknown byte size, because I do not know what the actual limit is. However, I can see numerous problems with this already. So, if I were you and it is not too messy for your sake, I would indeed stick to catching a potential exception, but still do so in a seperate class to avoid potential boilerplate code. Anyhow:
using UnityEngine;
using System.Collections.Generic;
public class PlayerPrefs$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public static int $$anonymous$$axSize { get; set; }
public static Dictionary<string, object> Entries { get; set; }
void Awake()
{
// random, nonsensical number
PlayerPrefs$$anonymous$$anager.$$anonymous$$axSize = 65536;
PlayerPrefs$$anonymous$$anager.Entries = new Dictionary<string, object>();
}
public static void AddEntry(string key, object value)
{
if (PlayerPrefs$$anonymous$$anager.CalculateCurrentSize() + sizeof(value) <= PlayerPrefs$$anonymous$$anager.$$anonymous$$axSize
&& ! PlayerPrefs$$anonymous$$anager.Entries.Contains$$anonymous$$ey(key))
{
PlayerPrefs$$anonymous$$anager.Entries.Add(key, value);
// check data type of value and call PlayerPrefs.SetFoo
}
}
public static void DeleteEntry(string key)
{
if (PlayerPrefs$$anonymous$$anager.Entries.Contains$$anonymous$$ey(key))
{
PlayerPrefs$$anonymous$$anager.Delete$$anonymous$$ey(key);
PlayerPrefs$$anonymous$$anager.Entries.Remove(key);
}
}
public static int CalculateCurrentSize()
{
int total = 0;
foreach ($$anonymous$$eyValuePair<string, object> entry in PlayerPrefs$$anonymous$$anager.Entries)
{
total += sizeof(entry.Value);
}
return total;
}
}
Your answer
Follow this Question
Related Questions
Where on iPhone editor are playerprefs stored. 1 Answer
PlayerPrefs.DeleteAll not working 1 Answer
PlayerPrefs saving 1 Answer
Guide in ArrayPrefs or PlayerPrefx? 1 Answer
Are PlayerPrefs the same on iPhone compared to my computer? 1 Answer