Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Strangerweather · Jan 17, 2016 at 05:56 PM · playerprefsserialization

Serializing PlayerPrefs

Hi guys

I saw this script here (http://forum.unity3d.com/threads/c-serialization-playerprefs-mystery.72156/) and even though it is old, I was wondering whether it could still work in your opinion:

 using UnityEngine;
 using System;
 using System.IO;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
  
 public class PlayerPrefsSerializer
 {
     public static BinaryFormatter bf = new BinaryFormatter ();
        // serializableObject is any struct or class marked with [Serializable]
     public static void Save (string prefKey, object serializableObject)
     {
         MemoryStream memoryStream = new MemoryStream ();
         bf.Serialize (memoryStream, serializableObject);
         string tmp = System.Convert.ToBase64String (memoryStream.ToArray ());
         PlayerPrefs.SetString ( prefKey, tmp);
     }
    
     public static object Load (string prefKey)
     {
         string tmp = PlayerPrefs.GetString (prefKey, string.Empty);
         if (tmp == string.Empty )
             return null;
         MemoryStream memoryStream = new MemoryStream (System.Convert.FromBase64String (tmp));
         return bf.Deserialize (memoryStream);
     }
 }

The reason I ask is that I am trying to serialize my PlayerPrefs, but when I use this, I see no difference in how the strings look in Registry. Any help would be welcome!

Alternatively, how do you guys serialize your PlayerPrefs if at all?

Cheers! :-)

Comment
Add comment · Show 1
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 brunocoimbra · Jan 18, 2016 at 10:10 PM 1
Share

There is no thing like "serializing player prefs", you can serialize your data THEN save it to the disk using player prefs...

About the code part, I used that same code as a base for $$anonymous$$e, with some improvements for generics and auto-casting to the correct value type, also improvements for saving other data structures.

If what you want is a better way to save you data, like hidding more your data to be more difficult to change it by hand, this code should handle good part of it.

1 Reply

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

Answer by LordDarkon76 · Jan 18, 2016 at 11:22 PM

If you need that for your save.

I'm currently using unitys JsonUtility.ToJson to make my stats class a json after a little codification(give some fun for the save hackers) save the string as a player prefs.

For loading I just use JsonUtility.FromJsonOverwrite and everything is ok.

http://docs.unity3d.com/ScriptReference/JsonUtility.html

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

37 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 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

Save/load playerprefs 2 Answers

Saving players progress in ScriptableObject asset 1 Answer

How to add strings in a list AND be able to save it and load it? Using playerpref OR serialization 2 Answers

How can I save a series of instantiated objects from an array? 2 Answers

PlayerPrefs v. XML File for Saving Data? 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