Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Serialize

Discussion in 'Scripting' started by sergirc88, Jun 2, 2015.

  1. sergirc88

    sergirc88

    Joined:
    Dec 10, 2013
    Posts:
    11
    Hi all,

    I'm working on my first project of unity and I have a lot of questions but the most important now is that I don't understand where is the file when serialize a class.

    I have the following code:

    XmlSerializer xmls = new XmlSerializer(typeof(Movement));
    StringWriter sw = new StringWriter();
    xmls.Serialize(sw, new Movement());
    string xml = sw.ToString();

    This code is correct and I serialize a object of the movement class, but, where is this file? Where can I get this file to validate the format?

    Thanks for all!
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    From a quick read, it doesn't look like that code writes a file, it looks like it serializes your object to a string variable. If you want to see the contents of that string, try adding "Debug.Log(xml);" to print it to the console.
     
  3. sergirc88

    sergirc88

    Joined:
    Dec 10, 2013
    Posts:
    11
    First of all, Thanks Antistone.

    I see it, I serialize the object though String, not file. Now I will find examples but if you know how can I serialize to a file I will be grateful :)
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
  5. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    To write a string to a file in C# is pretty simple:

    Code (csharp):
    1.  
    2.  
    3. using System.IO;
    4.  
    5. public void WriteStringToFile(string FilePathAndName, string FileData)
    6. {
    7.    FileStream fs = new FileStream(FilePathAndName, FileMode.OpenorCreate, FileAccess.ReadWrite);
    8.    StreamWriter sw = new StreamWriter(fs);
    9.    sw.Write(FileData);
    10.    sw.Flush();
    11.    sw.Close();
    12. }
    13.  
    14.  
    15.  
    To sort and write and random access multiple strings from a file can be a little more convoluted. Usually the easiest way is to just save all potential data you want over the file each time you save rather than reading and writing at random which (can) require some pretty complicated classes to keep it all organized and easy to access.
     
  6. sergirc88

    sergirc88

    Joined:
    Dec 10, 2013
    Posts:
    11
    Thanks!! I did it :) Was very easy but now I have a problem, how can I serialize a list of objects inside other class?

    [System.Serializable]
    public class Summon
    {
    public int identity;
    List<Skill> actives= new List<Skill>();
    }

    And the class Skill is:

    [System.Serializable]
    public class Skill
    {
    public int identity;
    public string descriptio{get;set;}


    My file don't have the Skill object.

    <?xml version="1.0" encoding="utf-8"?>
    <Summon xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <identity>0</identity>
    </Summon>

    How can I serialize all the object Summon?

    Thanks for all the help!