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
0
Question by Gilead7 · Sep 29, 2016 at 11:35 PM · c#listbinaryformatterdeserialization

Binary file Deserialize to list?

In order to deserialize a binary file into a list would you have to have saved that list into a binary file?

 string Name=NameText.text;
 string Image=ImageText.text;
 //Grab the input from a form and save it as binary
         BinaryFormatter bf = new BinaryFormatter();
         FileStream fs = File.Create(Application.persistentDataPath + "/Name.dat");
         NameData nd= new NameData();
         nd.Name=Name;
         nd.Image= Image;
         
         bf.Serialize(fs, nd);
         fs.Close();
 //This part works just fine.

I want to load the data back in as a list.

 if(File.Exists(Application.persistentDataPath + "/Name.dat"))
         {
                 BinaryFormatter bf= new BinaryFormatter();
                 FileStream fs= File.Open(Application.persistentDataPath + "/Name.dat" , FileMode.Open);
                 OurNameList=(List<TheNameClass>)bf.Deserialize(fs) as List<TheNameClass>;
                 
                 fs.Close();
 
 //This throws an error
 

InvalidCastException: Cannot cast from source type to destination type. So do I need to save the list prior to loading it into a list or can I just load the data into a list? What am I missing in the loading part? I plan to take the list and loop through it instatiating a text prefab to show the two strings that make up the class. Thanks!

Comment
Add comment · Show 13
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 Cherno · Sep 30, 2016 at 12:16 AM 0
Share

You have to cast the data into the Type that you serialized it as first. You could also use an ISerializationSurrogate to control how certain Types are serialized and deserialized.

avatar image Gilead7 · Sep 30, 2016 at 07:50 PM 0
Share

Thanks Cherno, but I must be missing something...

 if(File.Exists(Application.persistentDataPath + "/Name.dat"))
         {
                 BinaryFormatter bf= new BinaryFormatter();
                             NameData nd= new NameData();
                 FileStream fs= File.Open(Application.persistentDataPath + "/Name.dat" , File$$anonymous$$ode.Open);
                 OurNameList=(List<Name>)bf.Deserialize(fs) as NameData;
             
                 fs.Close();
                 }

This doesn't work give me the error that it can't convert the list to the NameData. When I try it with the NameData nd= new NameData(); it claims there is no namespace name nd that could be found, even though it's defined right there. Any other thoughts?

avatar image Cherno Gilead7 · Oct 01, 2016 at 01:48 AM 0
Share
                  OurNameList=(List<Name>)bf.Deserialize(fs) as NameData;
 

You try to cast as a List but the deserialized object is of Type NameData (I assume from the "as NameData" part). Objects of Type NameData can't be implicitly cast as Lists. Either serialize a List and deserialize it, or deserialize as NameData and fill a List with the data manually in some way.

avatar image Gilead7 · Oct 01, 2016 at 05:13 PM 0
Share

So put a list in the serializable class ins$$anonymous$$d of the two strings I have now.

 [Serializable]
   public class NameData
   {
     //public string Name;
     //public string Image;
     public List<OurName> OurNameList;
   }

Then add the two strings to the list as they come in from the form. public string OurName{get; set;} public string Image{get; set;}

 public Name( string Name, string Image)
         {
             this.OurName=Name;
             this.Image=Image;
         }

Is that right? Just trying to learn this stuff...

avatar image Cherno Gilead7 · Oct 01, 2016 at 11:43 PM 0
Share

... It depends. At this point, it would help if you would provide as much relevant information as possible. The claees involved, what you want to do with them, and how they are (de)serialized).

avatar image Gilead7 · Oct 02, 2016 at 08:48 PM 0
Share

Hopefully this will give a clear picture of what I'm trying to do. Let me know if there is anything unclear.

[Serializable] public class SaveClass { public List OurNameList; What is the datatype I should use here? The class with the two strings or string? Previously, I had: public string Name; public string Image;

}

public class TheName : $$anonymous$$onoBehaviour { public string Name{get; set;} public string Image{get; set;}

public TheName( string name, string image) { this.Name=name; this.Image=image; } } Do I even need this class any more?

class $$anonymous$$ain$$anonymous$$anager { public List OurNameList; Which data type do I use here? The script or string since there are two strings? This is the main part of my question.

Start() { List OurNameList = new List();

}

public void ChangeInput() { string Name=nameText.text; string Image=imageText.text; OurNameList.Add(Name); OurNameList.Add(Image); Debug.Log(OurNameList.Count); for(int i=0; i

The data is then serialized and saved via a binary formatter. Then it is read and deserialized to be displayed on screen with an instantiated prefab with the two strings as a text object.

public void ShowTheName() {

         //Display The List Items
         for(int i = 0; i< OurNameList.Count; i++)
         {
             GameObject bnObj = Instantiate(NamePrefab); (Which has two text items)
             Name tmpName = OurNameList[i];
             bnObj.GetComponent<NameScript>().DisplayName(tmpName.Name, tmpName.Image);
             bnObj.transform.SetParent(Displayparent); 
         }
     }
     else
     {
         Debug.Log("File doesn't exist, please save one!");
     }
     
 }

public class NameScript : $$anonymous$$onoBehaviour { public GameObject Name; public GameObject Image;

 public void DisplayName(string name, string image ) 
 {
     this.Name.GetComponent<Text>().text=name;
     this.Image.GetComponent<Text>().text=image;
     
 }

}

avatar image Cherno Gilead7 · Oct 02, 2016 at 09:18 PM 0
Share

The Types depend on what you need to do, I can't help you with that, that's completely up to do.

Take a look at the syntax for the Generic List; they are commonly declared like this:

 public List<string> myList = new List<string>();
avatar image Gilead7 Cherno · Oct 03, 2016 at 04:00 PM 0
Share

Right. I also know that you can put a class in as a type. However when you are saving a binary file and put a list in the "save class" and you have two strings that go in it do you make a separate class and put it in there or do you, in my case, use string?

Show more comments
avatar image Gilead7 · Nov 28, 2016 at 11:49 PM 0
Share

I'm getting back to this project, so let me try to explain: This is supposed to take input from two fields, one for a name and one for image, both strings. That data is added to a list and saved as a binary file. I want to display that data with a prefab.

 [Serializable]
 public class NameData
 {
     //public string Name;
     //public string Image;
 
         public List<string> NameList = new List<string>(){"Name", "Image"};
 }
 
 
 public void LoadName()
     {
         if(File.Exists(Application.persistentDataPath + "/Name.dat"))
         {
                 BinaryFormatter bf= new BinaryFormatter();
                 FileStream fs= File.Open(Application.persistentDataPath + "/Name.dat" , File$$anonymous$$ode.Open);
                 NameList=(NameData)bf.Deserialize(fs);
                 fs.Close();
 
             }

So I want to take the NameData and deserialze it to a list. Does that make sense?

Show more comments

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Serializing/deserializing List of custom class. 0 Answers

A node in a childnode? 1 Answer

Save string and list to binary file 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