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

Question Saving complicated object hiearchy

Discussion in 'General Discussion' started by najongjine_unity, May 31, 2023.

  1. najongjine_unity

    najongjine_unity

    Joined:
    Oct 29, 2019
    Posts:
    19


    This game users can make their own building.
    The parts of building are formed by complicated object hiearchy.
    I want to save the building like making as prefab.
    Not just one single part of object. parent object, sub parent objects, and children objects with their positions and rotations.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    There are asset store tools like Easy Save or whatnot that could probably handle this.

    But in general it's not an easy to task to 'save out' all the Unity stuff, namely as anything inheriting from
    UnityEngine.Object
    cannot be directly serialised out.

    Usually these kinds of things are backed by a pure data representation, and the visuals just match to update the data. That way, the data can or should be easily saved out, and you don't need to bother saving any of the Unity stuff.
     
    Ryiah likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Basically the way you do it is you have the code that is responsible for placing and rotating all of this keep a record of everything relevant to creating your buildings, and then when you want to place down a prefab you just repeat those steps using the record.

    Speaking of which here is the relevant information for the easiest approach. Any data in your scripts that needs to be saved gets an attribute applied to it, and then you can just serialize the entire GameObject. The downside being that it will try to serialize everything on those GOs so if they have a ton of components it will store a ton of data.

    https://docs.moodkie.com/easy-save-3/es3-guides/saving-loading-gameobjects-prefabs/
     
    Last edited: May 31, 2023
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    This needs a data-driven design if you want to save it as data and load from data.

    Basically you need info like "Type: Wall, position: {}, scale: {}" and any other values that need to be saved and restored. By writing the entire layouter as a simple (possibly hierarchical) list of instances of such structs or classes, you can easily save them using any serialization means like the (now out of preview) Unity.Serialization v3.1 package.

    Once you have that data loaded back in, traverse it and for every type run the code that instantiates the type and updates its transform.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,317
    You cannot save prefabs at runtime. In your case you should save the state of your grid and reconstruct objects from it when you load.

    Not that you've obviously built walls out of scaled cubes, but in those kinds of application entire room is normally procedural geometry which is rebuilt when you change the room.
     
  6. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,754
    It would be perfect if only it could handle dictionaries natively. Still, great to see a high perf serialization API.
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Yeah what good is a serialization package if still has the same limitations as Unity's main serialisation...

    Can't see if it can serialise out references to Unity objects or not. If not, doesn't seem to have any advantages over the Odin serialiser.
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    I think it's just a case of them moving yet another internal system to a package. I can't find any info on it though and I didn't even know it was a thing until this thread.
     
  9. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    1,754
    Most serializers primarily rely on reflection to perform serialization and deserialization, including Odin serializer (correct me if I'm wrong). The new Serialization API has source generators which seem to work automatically with [GeneratePropertyBag] attribute. This should provide superior performance and the same workflow for all platforms i.e. no special AOT compatibility DLL needs to be generated. It also produces "clean" JSON without the extra metadata Odin adds. And the API has built in type migration and versioning out of the box.

    So there are several advantages as far as I understand.
     
    DragonCoder likes this.
  10. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,955
    If you have a system to allow users to create their own buildings, then you are mostly there already. When they place parts or edit them, just store that information. (location, scale, rotation, and what it is and what its parent is). You already have to set all that stuff, so just go from there and save it in a class or struct or other object. Then you will need serialize those and you can then rebuild them. Ideally you can use the same tools you let your users use. (or at least share functionality).
     
    Ryiah and angrypenguin like this.