x


Creating a complex prefab during asset postprocessing

In OnPostprocessModel, I want to prepare a GameObject that is parented to another GameObject, and then create a prefab out of that. This works fine, but it leaves an instance in the scene. When I try to destroy the object after creating the prefab, the editor crashes. Here's the code:

public class MyAssetPostprocessor : AssetPostprocessor
{
    public void OnPostprocessModel(GameObject gameObject)
    {
        // create an empty gameobject and add the mesh as a child
        GameObject prefabRoot = new GameObject("foo");
        gameObject.transform.parent = prefabRoot.transform;

        // create the prefab, and connect the dummy prefab root to it
        Object prefab = EditorUtility.CreateEmptyPrefab("Assets/test.prefab");
        EditorUtility.ReplacePrefab(prefabRoot, prefab, ReplacePrefabOptions.ConnectToPrefab);

        // cleanup the gameobject instance in the scene
        Object.DestroyImmediate(prefabRoot);
    }
}

I'm not convinced I'm heading down the right path. What's the correct way to create complex prefabs during asset postprocessing, involving the asset which is being imported?

more ▼

asked Dec 06 '09 at 02:53 AM

Lance Sun gravatar image

Lance Sun
1k 12 16 41

I get a little more mileage (e.g. editor doesn't crash) if I detach the game object being imported from the prefab root before I destroy the prefab root.

gameObject.transform.parent = null;

But a new object still appears in the scene as a disconnected prefab instance.

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

1 answer: sort voted first

The GameObject being passed to OnPostprocessModel is not an in-scene GameObject, but the one going into your assets. Trouble starts when you attach this GameObject to an in-scene GameObject.

What you should do is:

  • Instantiate a new scene GO based on your Assets GO:
    • GameObject newGO = Instantiate (gameObject, Vector3.zero, Quaternion.Identity);
  • Modify this newly created GO, parent etc.
  • Do your prefab dance.
  • Destroy the new GO - not the original Assets one.
more ▼

answered Dec 07 '09 at 11:12 AM

AngryAnt gravatar image

AngryAnt ♦♦
4k 14 19 52

Ah, I see. What I'm seeing now is that if I destroy the prefab root GO, the whole prefab still remains up in the scene as a disconnected prefab. If I destroy the instantiated scene GO (but not prefab root) then only the prefab root remains in the scene, as a disconnected prefab.... I'll keep futzing with it.

Dec 07 '09 at 11:31 AM Lance Sun

K, it works now. I was using ReplacePrefabOptions.ConnectToPrefab instead of ReplacePrefabOptions.Default.

Dec 07 '09 at 11:37 AM Lance Sun
(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:

x5268
x1284
x995
x351

asked: Dec 06 '09 at 02:53 AM

Seen: 2732 times

Last Updated: Dec 06 '09 at 02:53 AM