- Home /
How to save generated objects?
I have a script that generates a level based on text information, using "room" prefabs I already have saved. That script, however, generates only empty stages I'd like to fill with objects later, on the editor. Is there any way I can save this code-generated object and open it on the editor later? Basically I'm just trying to speed up the level-building process...
alucardj please make your comment an answer, it's really not a comment :)
Answer by AlucardJay · Feb 20, 2013 at 04:54 AM
Information can be saved to PlayerPrefs, a text file or even an image. I think what you want is to execute code for building the skeleton of your scene, then saving that scene so it can be expanded on. Are you familiar with ContextMenu ? This way you can execute code without running the scene, then save the scene with all the modified data.
http://docs.unity3d.com/Documentation/ScriptReference/ContextMenu.html
For example, I wrote a script to find all my waypoints and re-parent them to another object. Just yesterday I had a terrain with no tree colliders, so I wrote a ContextMenu function to find all the trees, and place colliders for me! Run once, scene saved, trees now have colliders.
Here is an example script. Create a new scene, attach this script to an empty gameObject :
#pragma strict
#if UNITY_EDITOR
@ContextMenu( "Run From Context Menu" )
function RunFromContextMenu()
{
Debug.Log("Running from ContextMenu");
CreateCube();
}
#endif
public var cubePosition : Vector3 = Vector3.zero;
function CreateCube()
{
var go : GameObject = GameObject.CreatePrimitive( PrimitiveType.Cube );
go.transform.position = cubePosition;
}
Now look in the Inspector, right-click on the script where it says Scriptname (Script) : http://docs.unity3d.com/Documentation/Images/manual/Inspector-3.jpg
In the drop-down menu there should be Run From Context Menu , click on that. Now change the Vector cubePosition and use the context menu again !
Some things may take a while to compute, and yield does not work in edit mode, those are some main considerations. But again with the tree example, 6000 trees took 3 mins and it looked like unity froze, but it is thinking (executing all the code within a single loop), just leave it and wait for all the code to take effect. I mention this because you could be instantiating a large amount of objects, so just be patient =]
Answer by Schnodahipfe · Apr 15, 2013 at 12:21 PM
I had a similar problem and finally found this great project: http://whydoidoit.com/unityserializer/