x


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?

more ▼

asked Dec 04 '09 at 06:19 AM

Lance Sun gravatar image

Lance Sun
1k 12 16 41

(comments are locked)
10|3000 characters needed characters left

5 answers: sort oldest

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.

more ▼

answered Dec 05 '09 at 02:48 PM

Ricardo gravatar image

Ricardo
5.2k 20 32 96

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Dec 05 '09 at 03:32 PM

AngryAnt gravatar image

AngryAnt ♦♦
4k 14 19 52

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!

Dec 06 '09 at 01:31 AM Lance Sun

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

Dec 06 '09 at 03:07 AM Lance Sun
(comments are locked)
10|3000 characters needed characters left

You can use ReplacePrefabOptions = ReplaceNameBased without destroying scene object problem.

    Object prefab = EditorUtility.CreateEmptyPrefab("Assets/test1.prefab");
    EditorUtility.ReplacePrefab(importedObject, prefab, ReplacePrefabOptions.ReplaceNameBased);
more ▼

answered Sep 02 '10 at 03:49 PM

Robotron18 gravatar image

Robotron18
113 3 5 12

OMG 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 THANK YOU lol

Aug 06 '12 at 06:11 AM ronronmx
(comments are locked)
10|3000 characters needed characters left

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 ..

more ▼

answered Jul 13 '11 at 10:28 AM

vivek2012 gravatar image

vivek2012
-39

(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered Nov 11 '12 at 08:49 PM

Soviut gravatar image

Soviut
91 3 6 13

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5054
x1250
x969
x347

asked: Dec 04 '09 at 06:19 AM

Seen: 11103 times

Last Updated: Nov 11 '12 at 08:49 PM