- Home /
Is it possible to edit prefab in editor mode by script?
Is it possible to write script which will open prefab selected in Project tree (where all resources are available) do something with it's content (find and change GameObjects) and than save it.
Answer by Statement · Dec 23, 2013 at 10:46 AM
Yes, it's entirely possible.
I don't know exactly what you had in mind. Look at my answer at Generating a prefab from Audio Clip via script if you want to see one real example of what can be done.
The
UnityEditor
namespace contains API for writing editor scripts.Your Editor scripts should be placed in a folder that you create, called `Editor`.
If you are new to Editor Scripting, I'd suggest you start reading Extending the Editor. Then start looking at the Scripting Reference for all kinds of editor APIs that are available, to build yourself a rough sense of what is available. Not all of it will make sense to you at first glance, but it helps to have it in the back of your head as a vague imprint. If you look on the left side, you can set the dropdown to Editor Classes and the list below will populate with the documented API.
**PrefabUtility** is a class that has prefab helper methods.
**Selection** is a class that gives you easy access to whatever you have selected in the editor. See the link for details.
**AssetDatabase** is the main entry point to accessing assets in the project.
Thanx. I was to lazy to check myself.
I want to write a refactoring script which will update GameObjects with my components after I change some RequireComponent attributes.
I wonder if it is also possible to add some Components which will be omitted in final build (during publish) to store some metadata.
You could create a dummy game object that holds components (maybe put it as a child) and set the tag of that child object to "EditorOnly". Objects with this special tag are stripped of build.
This, in conjunction with hideflags so it's invisible to the level designers is a pretty neat way of ensuring scene-based metadata to persist, but not leak out into the final builds.
Otherwise you'd have to create a script that goes through your scenes, possibly creates a copy of them and on the copy you can safely mass delete stuff of a certain kind without risking ruining the real scene.
Answer by bansheesoft · Jan 05, 2017 at 06:00 PM
EditorUtility.SetDirty (GameObject); Did it for me in a script editing node editor. May help someone.
Answer by JamieFristrom · Nov 19, 2015 at 02:08 AM
It actually took me a couple of hours to figure out how to do it from the resources provided above, so here's what actually worked for me - this little example shows editing a Version prefab from script as a way of automating updating version numbers for a build script:
// get a reference to the prefab itself, not a clone or instantiation:
GameObject versionObject = AssetDatabase.LoadAssetAtPath("Assets/Resources/Version.prefab",
typeof(GameObject)) as GameObject;
// Edit it
versionObject.GetComponent().versionNumber = versionNumber;