- Home /
Creating/Saving prefabs during runtime?
This questions regards events after a scene has started (after asset processing, start() or awake() functions).
Somewhat similar to saving a game state or player prefs, I'm looking for a way got save the state of a gameObject at some arbitrary time, having undergone arbitrary dynamic changes, and store it permanently.
For example, I'd maybe "grab gameObject A at Time=1000 seconds".
I'm asking specifically about prefabs, as I'd potentially like to include these in other scenes later on.
hi I have to crate a prefab at runtime and save it as we are making a application where a player can create object scale change texture draw lines created wall and I have to save it at runtime and load it .this cannot be done by by searlizable as each object has different name in the hirercy than in its actual prefab
what I am planning is to create a prefab at runtime and put all the object inside the prefab and save it when the player calls the load function we just have to load the prefab.
is this possible....
Answer by qJake · Jul 07, 2010 at 04:44 AM
As far as I know, you can't do this at runtime. In fact, it's really hard to try and save anything at runtime (as that's not how Unity's designed to function).
You can, however, do it when the game isn't running (with an Editor script, that goes in the /Editor/ folder), like this:
Use EditorUtility.CreateEmptyPrefab to create an empty prefab object, and then once you have that reference (as well as the game object that you want to save into it), you can use EditorUtility.ReplacePrefab to "save" the game object inside the prefab you just made. Be sure to name your prefab something unique so it doesn't get overwritten.
ah, ok. I think this might work for what I'm trying to do. Thanks!
Do you happen to know a place to learn about creating Editor scripts and how to implement them?
Answer by Tetrad · Jul 07, 2010 at 07:02 AM
While not specifically about saving prefabs, this question talks about saving the state of a game object during run time.
I believe there is a way to do this using ExecuteInEdit$$anonymous$$ode(). However, I can't quite seem to figure out how to merge meshes in Edit $$anonymous$$ode. ???
Answer by Joe-2 · Jan 28, 2011 at 08:37 PM
You can manually copy the gameobject you are editing at runtime, and paste it in the editor later. It is like creating a carbon copy. If it's easy to insert that cc back into your instance pool, then this should be useful to you. Be aware though that it will contain any runtime changes that you are doing, such as attaching scripts or changing names, etc.
Answer by Vectrex · Dec 08, 2012 at 05:14 AM
There's an asset store thing called 'PlayMode persist' I think. I use it, it works quite well.
Answer by drudiverse · Apr 02, 2014 at 10:25 AM
found this script": class CreatePrefabFromSelected its awesome apart from the generated mesh isnt in ther on that version