Saving a level with the help of prefabs

Hi,

I’m making a run-time level editor and have ran into some issues while implementing a save feature. I have a large amount of different GameObjects that can be added to the level. Instead of saving all of their components, I only need to save a few variables (for example the position). Instead of saving every GameObject separately, I’d simply like to save some sort of reference to the prefab they use.

When loading, I could just use Resources.Load(...) to load the prefab from the resources folder, saving me the trouble of having a reference to the prefab set up in the scene. Let’s say we have 1000 different prefabs. Maintaining a reference to them all manually from some collection in the scene wouldn’t be very plausible. I’d much rather load a GameObject only when it’s actually used in the level.

I tried looking around but apparently the paths to prefabs can only be found while in the Unity editor (using the PrefabUtility). What kind of a solution would let me get the prefab path of a GameObject so that I could use Resources.Load() during my level loading? Or is there perhaps some totally different way of approaching the problem that I haven’t thought about?

Thank you

Hi,

did you already looked into this tutorial about saving GameData?

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

I don’t understand why you need to save gameobjects as prefabs, an xml serialization formed like this would be fine for a runtime editor:

<gameobject prefabname="_name">
<Xpos>_pos</Xpos>
<Ypos>_pos</Ypos>
<Zpos>_pos</Zpos>
<Xscale>_scale</xScale>
.
.
.

Also, this will give players the opportunity to import and export their desings.

Take a look at my SerializeHelper here:

SerializeHelper - Free save and load utility. (De)Serialize all objects in your scene.

If you use it, you will be able to decode what kind of data is saved. For your case, I recommend ignoring the part the saves and loads component data, and instead add a string variable to the ObjectIdentifier script that holds the path to the prefab. Since this path will then get saved along side the other gameobject information, you can just use it to load and instantiate the gameObject after loading. Note that in it’s current form, the project creates a dictionary of all prefabs in the resources folder and uses that to instantiate gameObjects after loading. Feel free to ignore, or rather modify, that as well.