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
1
Question by Long Fall Games · Feb 20, 2014 at 01:53 AM · serializationloadingsavingpersistentdatapath

Issue loading serialized config.dat from persistant Data

So Im trying to save a config file to persistantData im not getting any errors but my loading is returning "null" and im not sure why. Im new to serializing. I have 3 classes.

  1. config Data Class

  2. Static Dictionary

  3. Loading Class

Heres my data and dictionary

 public class config : MonoBehaviour 
 {
     public int id;
     public string textVal;
     public int intVal;
     public float floatVal;
     public bool boolVal;
 }
 
 //this class stores all the data
 public static class ConfigData 
 {
     public static SortedDictionary<string ,config> configData = new SortedDictionary<string , config>();
 }

compling with no errors. Then theres my saving and file check methods.

 //check if file exists and preform appropriate action
 public UILabel txt;
     // Use this for initialization
     void Start () 
     {        
         configFile=ConfigData.configData;
         if(!File.Exists(Application.persistentDataPath + "/config.dat"))
         {
             Debug.Log("config file does not exist, It will be created");
             txt.text = "Creating Config file";    
             createConfig();        
         }
         else
         {
             txt.text = "Loading Config File";    
             loadConfig();
             //Binary formatter for loading back
             
         }
     }

again no errors and it identifies if the file exists correctly. I save as follows.

 //create the config file
     void createConfig()
     {
         var b=new BinaryFormatter();
         var f=File.Create(Application.persistentDataPath + "/config.dat");
         createData();
         b.Serialize(f,ConfigData.configData);
         f.Close();
     }    
 
     //create the data
     void createData()
     {
         config tempconfig=new config();
         tempconfig.id=0;
         tempconfig.textVal="0.0.1";
         ConfigData.configData.Add("Version" , tempconfig);
     }

and finally here's loading

 void loadConfig()
     {
         var b = new BinaryFormatter();
         //Get the file
         var f = File.Open(Application.persistentDataPath + "/config.dat", FileMode.Open);
         ConfigData.configData = b.Deserialize(f) as SortedDictionary<string, config>;
         f.Close();
         Debug.Log(ConfigData.configData);
     }

I checked the config.dat file and I think its saving properly

 ÿÿÿÿ  verson 0.0.1

but i cant be 100% certain as its serialized. but this tell me it has to be in how im loading the data. Any Help is greatly appreciated!

Comment
Add comment · Show 2
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 whydoidoit · Feb 20, 2014 at 04:49 AM 0
Share

If you deserialize to an object - is it null? If not, what type is it?

    var o = b.Deserialize(f);
    Debug.Log(o.GetType());

Is there a good reason to use a SortedDictionary? - those things suck at performance...

avatar image whydoidoit · Feb 20, 2014 at 04:51 AM 0
Share

You can also use my Unity Serializer to do this kind of stuff, the basic part of it is a BinaryFormatter replacement that handles some Unity objects better - it can deserialize any IDictionary derivative.

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

Saving and Loading Various Things 1 Answer

Serializing a graph 2 Answers

how to properly save an int value ? 2 Answers

I am getting a serialization exception error when trying to save and load in Unity? 2 Answers

How to deserialize after I've added more members to a class 0 Answers


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