Search Unity

Saving a gameobject with all components

Discussion in 'Scripting' started by mattcscz, Aug 1, 2015.

  1. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    Hi,

    I've been looking around and can't find a definitive answer, so I decided to make a thread.

    Basically I have a game where you can create your own objects (similar to besiege) and I need an easy way to store your created objects, this includes all the components on the children. I've looked at quite a few assets on the store, however they only seem to store variables similar to playerprefs (Bool, Int, float, string etc).

    I've started looking into the Unity serializer but am not sure about it; ideally there would be a method which is as easy as simply creating a prefab, but afaik there isn't.

    Any help would be great!

    Thanks
     
  2. chronochris

    chronochris

    Joined:
    Jun 7, 2015
    Posts:
    61
    Why not just create a prefab, and a method to reload it?
     
  3. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    Sure, you can create a prefab at runtime, however once the game is exited or closed this prefab will revert to an empty prefab, simply because Unity can't do it :( Also the prefab thing is an UnityEditor function, so once built I don't think prefabs even have functionality.
     
  4. Zurusona-Kitsune

    Zurusona-Kitsune

    Joined:
    Mar 11, 2012
    Posts:
    94
    Have you checked binary serialization. This can save pretty much anything you want, not just variables that can be written as plain text.
     
  5. mattcscz

    mattcscz

    Joined:
    Mar 7, 2010
    Posts:
    411
    Could you kindly provide any examples you can recommend? I've never used the system before :(

    Thanks for the reply!
     
  6. Zurusona-Kitsune

    Zurusona-Kitsune

    Joined:
    Mar 11, 2012
    Posts:
    94
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For a long time whydoitdoit's unity serialiser was considered the definitive option for saving in Unity.

    I haven't checked it in a while, not sure if it got an update for unity 5. But it should still be usable.
     
  8. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    I had this problem a while ago and used UnitySerializer for a while. It is powerful, but also no longer supported and the website with all the Scripting API and forum full of helpful and sometimes critically important information is offline. Hence I dived into the fairly complex topic of serialization via BinaryFormatter and Reflection, and wrote my own framework for saving and loading a GameObject with all it's components. Currently, it can load and save any kind of data you want along with complete GameObjects with their Transform information as well as MonoBehaviour scripts on them. Support for other Unity-specific components like renderes etc. are not included as they are often just not needed; It's easy to write your own function for that, however. The asset is free and is described in more detail in this thread, and the scripts included are also commented comprehensively so it's easy to understand what's going on so users can modify it further.

    Link to "SerializeHelper":
    http://forum.unity3d.com/threads/se...e-serialize-all-objects-in-your-scene.338148/