Quantcast
Channel: Latest Questions by Lance Sun
Viewing all articles
Browse latest Browse all 14

Creating a complex prefab during asset postprocessing

$
0
0

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?


Viewing all articles
Browse latest Browse all 14

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>