Search Unity

Any way to organize data like a table within Unity?

Discussion in 'Scripting' started by Webbstre, May 26, 2015.

  1. Webbstre

    Webbstre

    Joined:
    Mar 7, 2015
    Posts:
    41
    Hello! I've been looking hard at Array and Lists and am not sure if I'm on the right track for what I want to do. I want to be able to serialize data similarly to a table (sql, xml, etc) so I can save it to a save game file. I have a large variety of objects with different data for each kind of object, and I'd like to be able to organize that data similarly to a table, with columns and rows, or even like an xml file. What is the best way to do this? I'm totally lost at this point. Thanks in advance!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    This is my go-to guide for serializing data in Unity:


    I suspect it has the answers you need. It doesn't really describe organizing data into columns and rows or anything, but I suspect you asked about that as a means to an end, and this would be the end.
     
  3. Webbstre

    Webbstre

    Joined:
    Mar 7, 2015
    Posts:
    41
    Thank you! I just finished watching it. It does help with a lot of data concepts I could use in other places, but I'm not sure it will work as a different method to save my gameobjects. I'll try it out though. If anyone has suggestions for GameObject saving I'm open to them!
     
  4. ThatGuyFromTheWeb

    ThatGuyFromTheWeb

    Joined:
    Jan 23, 2014
    Posts:
    29
    As far as i know unity sucks at serializing collections (other than plain 1d-arrays or lists), polymorphic types, everything that derives from System.Object and a lot of other things. If you just want to write arbitrary objects to a file and maybe use it as asset, you may give this approach a try. It didn't look very difficult and may work for
    you, if you have no other special needs.



    Basically he is creating a single asset from a scriptable object and uses a .net serializer to write everything into a single string. He's using the .net binary formater, but i suppose you could also use one of the xml or json serializers flying around in the internet.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The tutorial I linked uses .NET's binary formatter, as well. It also describes how to use the XML formatter (which is built in to .NET alongside the binary one.)
     
  6. ThatGuyFromTheWeb

    ThatGuyFromTheWeb

    Joined:
    Jan 23, 2014
    Posts:
    29
    Doh. Of course you are right. I've watched that a while ago and it seems, i've forgotten almost everything. :D This leaves me wondering, what else Webbstre may be looking for.
     
  7. Webbstre

    Webbstre

    Joined:
    Mar 7, 2015
    Posts:
    41
    So I've been doing more research on this topic, and the best way to get better help seems to be explaining better. I'm making a puzzle game, which is split into several groups of objects (some made up of multiple game objects). I need to be able to save the entire collection of groups, and load each group separately at different points of the game. For example:


    Group 1 (Empty Object)
    --Prefab 1 (Just a prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab (These are the visual components that make up the object)
    ----Child Object 2, based on a prefab
    --Prefab 2 (Different prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab
    ----Child Object 2, based on a prefab
    ----Child Object 3, based on a prefab
    --Prefab 3 (Same as prefab 2 but the values below are different)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab
    ----Child Object 2, based on a prefab
    ----Child Object 3, based on a prefab
    --Prefab 4 (Just a prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab
    ----Child Object 2, based on a prefab


    Group 2 (Empty Object)
    --Prefab 1 (Just a prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab (These are the visual components that make up the object)
    ----Child Object 2, based on a prefab
    --Prefab 2 (Different prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab
    ----Child Object 2, based on a prefab
    ----Child Object 3, based on a prefab
    --Prefab 3 (Completely different prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab



    Group 3 (Empty Object)
    --Prefab 1 (Just a prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab (These are the visual components that make up the object)
    ----Child Object 2, based on a prefab
    --Prefab 2 (Different prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab
    ----Child Object 2, based on a prefab
    ----Child Object 3, based on a prefab
    --Prefab 3 (Same as prefab 2 but the values below are different)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab
    ----Child Object 2, based on a prefab
    ----Child Object 3, based on a prefab
    --Prefab 4 (Just a prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab
    ----Child Object 2, based on a prefab
    --Prefab 5 (Another completely different prefab)
    ----Several public or private types of data attached specifically to object (for example, HP, MP, momentum and location, in addition to scale and rotation)
    ----Child Object 1, based on a prefab


    I know how to do simple serialization, I have trouble getting into things this complicated.
     
    Last edited: Jun 6, 2015
  8. Webbstre

    Webbstre

    Joined:
    Mar 7, 2015
    Posts:
    41
    Oh, and I forgot to mention, I watched the Youtube video. It was interesting, but doesn't go advanced enough ;)
     
  9. Feartheway

    Feartheway

    Joined:
    Dec 12, 2017
    Posts:
    92