Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kiwasi · Aug 19, 2014 at 12:00 AM · playerprefs

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Aug 19, 2014 at 03:01 AM 0
Share

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.

avatar image Wicketd · Aug 19, 2014 at 01:04 PM 1
Share

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;
     }
 }



avatar image
0

Answer by Rachan · May 28, 2021 at 11:09 AM

$$anonymous$$ what is this?

it mean?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges