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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Serialization and list

Discussion in 'Scripting' started by Ninetitle, Aug 14, 2015.

  1. Ninetitle

    Ninetitle

    Joined:
    Jun 7, 2015
    Posts:
    12
    Hi I'm currently implementing an app dor android and IOS and I had some question for the community.

    From what I heard the system that unity uses is a little buggy and unreliable with complex data structure and custom classes, however some of the tutorial and documentation that I read seems a little out of date so I was wondering:

    1. Is [Serializable] still used (I can find only [System.serializable] from C# and [SerializableField] of Unity) or did I need to use only [SerializableField] for every field I want serialized ?

    2. Serialize a list of custom classes still does cause an infinite loop?

    3. If the serialize of unity doesn't work can you link a serialize tutorial or an article so I can implement my own methods ?

    Thanks for the help
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    [Serializable] is [System.Serializable], if you are including the System namespace with "using System;", which some people like to do. That will make it so that every public possible-to-serialize field will be serialized automatically, so no need for the [SerializeField] attribute (you only use that with private fields you also want to serialize). Serializing lists of custom classes if fine, provided that the classes in the list are marked as [Serializable] in their own scripts.

    I haven't noticed anything really buggy about Unity's serialization. It might not do everything you want it to do, and it's a long process to really understanding it, but it does the things it's supposed to. Not being able to use non-built-in structs is sort of annoying, but we all got over it pretty quick. There are situations where a struct is better than a class, but there aren't any situations where a struct is necessary.

    More information on serialization in general:
    http://blogs.unity3d.com/2014/06/24/serialization-in-unity/
    http://forum.unity3d.com/threads/serialization-best-practices-megapost.155352/

    And specifically for editor serialization, because the chart is sort of fantastic:
    http://forum.unity3d.com/threads/a-...elds-dont-go-null-on-assembly-reloads.276069/
     
    Last edited: Aug 14, 2015
    rakkarage likes this.
  3. Ninetitle

    Ninetitle

    Joined:
    Jun 7, 2015
    Posts:
    12
    Thanks for the fast response even today, will try and post my results.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Serialisable and SerialiseField are to different things for different jobs.

    Serialisable marks a Type as 'possible to serialise'.

    SerialiseField marks a field as 'unity should serialise this'

    You often need to use both to get the results you need.
     
  5. Ninetitle

    Ninetitle

    Joined:
    Jun 7, 2015
    Posts:
    12
    Ok serialized without a problem.
    Thanks to all the community for clearing my doubts.