- Home /
How do I programmatically assign a GameObject to a prefab?
When importing a mesh, I want to automatically create a prefab for that mesh, and assign that mesh to the new prefab. I have something like this:
public class MyAssetPostprocessor : AssetPostprocessor
{
public void OnPostprocessModel(GameObject gameObject)
{
if (assetPath.EndsWith("_model.fbx"))
{
string prefabPath = assetPath.Replace("_model.fbx", ".prefab");
Object prefab = EditorUtility.CreateEmptyPrefab(prefabPath);
// prefab.AddGameObject(gameObject); ?
}
}
}
But I don't know how to add the mesh to the prefab. In the editor, I can drag-and-drop it in the project window. How do I do this programmatically?
and how the code is for java? i dont really get it how it works :(
Answer by Ricardo · Dec 05, 2009 at 02:48 PM
This is a snippet from the code I use to create prefabs from a group of selected objects:
[MenuItem ("My Project/Create Simple Prefab")]
static void DoCreateSimplePrefab()
{
Transform[] transforms = Selection.transforms;
foreach (Transform t in transforms) {
Object prefab = EditorUtility.CreateEmptyPrefab("Assets/Temporary/"+t.gameObject.name+".prefab");
EditorUtility.ReplacePrefab(t.gameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
}
You may want to try with EditorUtility.ReplacePrefab, althought caveat emptor: I've only used it at editor time, not in the asset post-processor.
As Soviut says, use PrefabUtility ins$$anonymous$$d of EditorUtility (current version: Unity 5.2)
Answer by AngryAnt · Dec 05, 2009 at 03:32 PM
Use CreateEmptyPrefab to prepare your prefab asset. Then construct your desired GameObject as you would like it to be ingame. Finally use ReplacePrefab to store your GameObject in your prefab and Destroy the original GameObject if you don't want it in the scene.
Thansk. You also answered the next question I was about to ask, which was "I want to put my FBX mesh into another empty GameObject and create a prefab out of that, so that my prefab doesn't have an X rotation of 270."... it's not completely obvious you have to instantiate it in the scene then destroy it. Thanks!
I'm having some issues with destroying the original GameObject because I don't want it in the scene. If you can help, I started a new question here
Answer by Soviut · Nov 11, 2012 at 08:49 PM
You should now be using PrefabUtility, not EditorUtility for all prefab operations. Not only does it have a method for creating empty prefabs but it also has a method to create prefabs from existing GameObjects:
GameObject prefab = PrefabUtility.CreatePrefab("Assets/Whatever.prefab", (GameObject)Selection.object, ReplacePrefabOptions.ReplaceNameBased);
Answer by Robotron18 · Sep 02, 2010 at 03:49 PM
You can use ReplacePrefabOptions = ReplaceNameBased without destroying scene object problem.
Object prefab = EditorUtility.CreateEmptyPrefab("Assets/test1.prefab");
EditorUtility.ReplacePrefab(importedObject, prefab, ReplacePrefabOptions.ReplaceNameBased);
O$$anonymous$$G dude, you just made my day...I have been working on a Editor Extension to let me update Prefabs automatically, and although I had it working the way I (almost) wanted, I was having a problem with the Prefabs getting deleted and re-instantiated again, which would move them all back to the Prefab's default position/rotation...I have been going nuts for the past 3 hours trying to figure this out, and then your post finally made sense to me and I saw a big bright light :)
A BIG THAN$$anonymous$$ YOU lol
Answer by vivek2012.du · Jul 13, 2011 at 10:28 AM
Use Prefab in your Game Here is the tutorial for that . http://lnkd.in/J6ufVr . Here is demo and code for Prefab instantiate so download and try to undestand i hope u will get the solution ..