- Home /
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
Answer by Cherno · Jul 12, 2015 at 10:11 AM
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.
Saving the path to the prefab was the way I solved the problem (with my own implementation though).
Answer by Chris333 · Jan 24, 2015 at 01:47 PM
Hi,
did you already looked into this tutorial about saving GameData?
Hi and thanks for the answer. Unfortunately the video doesn't really contain any new information for me. I got a decent solution by stuffing my prefabs in to one folder and hard coding the path to my loader. After that it's just a matter of calling Resources.Load(path + "/" + prefabName)
.
Hi, i am sorry I can't help you, im in the same fix..but in correspondence to your last comment, I understand that you figured how to do it and so I ask can you share your info? $$anonymous$$y problem is saving anything the player does in the level editor. - hope u reply and thank you!
Answer by starikcetin · Jul 11, 2015 at 09:40 PM
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.