- Home /
Saving and Loading an Object and it's Children in Javascript
SO I have an editor that allows you to place objects on another object. It attaches them with fixed joints. I need to be able to save the parent object and all it's children so that it can either be reloaded in the editor or loaded into the game later. each of the objects have scripts attached to them also. I have not yet delved into saving and loading files in unity and have had some difficulty in finding good tutorials explaining how to do this. If anyone can help it would be greatly appreciated. also I am operating in Javascript.
Ok, I have done a little looking into all this now (thank you for your input), I still honestly am not sure what all your talking about but I did find some things about PlayerPrefs which at least got me to thinking about what all I need to be saving out once I figure the process of actually doing that.
The way I figure it I need to save out at least these key pieces of information and then have a load function that takes in the info and basically rebuilds the stuff based on the data: The Object Type The Object Tag The Object Parent The Object x position The Object y position The Object z position
I figure by somehow using an array or arrays I can grab and organize all this data while exporting it (im going to try and use player prefs as it seems easy enough to me) and then reloading it and using a function to reload the prefabs, place the parts in the proper positions, organize the hierarchy, fixed joint the parts together and assign the proper tags.
I will be trying this later today and will let you know how it goes. Also any advice on doing this would be appreciated.
Answer by DaveA · Oct 24, 2011 at 07:58 PM
I've done that with X3D as the format.
Basically, you pick a format that works for you. X3D's spec allows for scripting, but it does not translate well to Unity, so what I did was use X3D's 'proto' nodes to save and load references to Unity prefab files (which can contain scripts, of course). I save the transforms and hierarchy, then reference to the prefab.
I've also done this using CSV as the file format.
Answer by jahroy · Oct 24, 2011 at 09:07 PM
The easiest way to read from a file is to use TextAssets. They make everything related to reading a text file EXTREMELY easy.
I'm pretty sure you have to use System.IO to write files (or some library).
Like DaveA said, it sounds like you will also probably need to use XML for what you're after.