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
2
Question by EvaXephon · Aug 31, 2013 at 12:56 AM · playerprefsint

How can I store a number larger than 2147483647 in PlayerPrefs?

I recently discovered that Unity refuses to save an int larger than 2,147,483,647 in PlayerPrefs. The reason why this occurs is well-documented, but what is a workaround for this problem?

I need to store a number that can become incredibly large - indeed, larger than two billion - in PlayerPrefs. How would I go about doing this?

Comment
Add comment · Show 3
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 robertbu · Aug 31, 2013 at 01:31 AM 0
Share

@Jamora's suggestion of converting the value to/from a string is probably the best one. For the sake of completeness, here is another solution. You could also convert a long/ulong into two integers and then put them back together. So you could save two integers in PlayerPrefs:

 ulong l =  335147483647;
  
 void Start () {
     Debug.Log (l);
     int iHigh = (int)((l & 0xFFFFFFFF00000000) >> 32);
     int iLow  = (int)(l & 0x00000000FFFFFFFF);
     Debug.Log (iHigh+","+iLow);
     l = ((ulong)iHigh << 32) | (ulong)iLow;
     Debug.Log(l);
 }
avatar image Eric5h5 · Aug 31, 2013 at 01:47 AM 0
Share

The only problem with splitting into two integers is that you need to save both of them separately using two keys, which can potentially be problematic to deal with.

avatar image robertbu · Aug 31, 2013 at 02:16 AM 0
Share

@Eric5h5 - Yea. The only benefit is size, and only then if you had enough entries to push the 1 meg limit.

3 Replies

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

Answer by Jamora · Aug 31, 2013 at 01:07 AM

You can store your large number as a string and then parse it into a long - or whatever type you are using - when needed.

Comment
Add comment · Show 3 · 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 EvaXephon · Aug 31, 2013 at 01:47 AM 0
Share

Both Jamora and meat5000 suggested a great solution! (Too bad I can't flag both of them as having the correct answer!)

Here it is in practice:

 import System.Convert;
 
 var $$anonymous$$yLongNumber : long;
 
 function Update()
 {
     if (Input.Get$$anonymous$$eyDown("1"))
     {
         $$anonymous$$yLongNumber++;
         PlayerPrefs.SetString("SavedNumber", "" + $$anonymous$$yLongNumber);
     }
 
     if (Input.Get$$anonymous$$eyDown("2"))
     {
         Debug.Log(GetNumber());
     }
 }
 
 function GetNumber()
 {
     return System.Convert.ToInt64(PlayerPrefs.GetString("SavedNumber"));
 }

Thank you so much for your help!

avatar image meat5000 ♦ · Aug 31, 2013 at 03:01 AM 0
Share

No probs. Good question :)

avatar image ArbreCasual · May 11, 2018 at 02:47 AM 0
Share

Great solution ;)

avatar image
4

Answer by Eric5h5 · Aug 31, 2013 at 01:44 AM

You could use the technique in ArrayPrefs2, namely converting the bytes of the long to a base64 string. This has the advantage of obfuscating the actual value to some extent, if you care about that, as well as being somewhat more compact than a straight ToString() conversion for very large numbers (but less compact for small numbers).

 function Start () {
     var x : long = 123456789123456;
     SetLong("foo", x);
     var y = GetLong("foo");
     Debug.Log (y);
 }
 
 function SetLong (key : String, x : long) {
     var bytes = System.BitConverter.GetBytes(x);
     PlayerPrefs.SetString(key, System.Convert.ToBase64String(bytes));
 }
 
 function GetLong (key : String) : long {
     var bytes = System.Convert.FromBase64String(PlayerPrefs.GetString(key));
     return System.BitConverter.ToInt64(bytes, 0);
 }


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
avatar image
2

Answer by meat5000 · Aug 31, 2013 at 12:58 AM

Tried declaring a long instead of an int?

Look under Predefined Types in this link

http://wiki.unity3d.com/index.php?title=Introduction

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

23 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

Related Questions

Using Player Prefs for a Leaderboard 1 Answer

Convert class of int vars into an array 0 Answers

PlayerPrefs / RegEdit 1 Answer

Store Current Level Using Preferances? 1 Answer

Converting from an Int to a Float and back with PlayerPrefs 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