Automatically create prefabs from models on import

2017-10-30T12:10:00+00:00 Oct 30, 2017|Nested Prefabs, Tips and Guides|

Everyone needs to use models in Unity. Though, in many cases, using a model directly in the scene is not practical. If you want to make a modification to the model then you won’t be able to save that modification and you will have to manually copy that modification to all other models in the scene. Therefore, often it is common practice to create a prefab of the model and then use that prefab instead. Modifications to a prefab can be applied and all instances of the prefab will be updated automatically.

Manually creating prefabs for every single model that you import into your project is very time consuming, and this workflow can be easily automated with an asset postprocessor script in Unity.

To start we have to create a new class in Unity that derives from the AssetPostprocessor and add an OnPostprocessAllAssets method. This method will be called for every asset that is imported, moved, or deleted in Unity.

Since this method is called for every single asset we have to filter out all model assets. In order to do this, we can loop through all imported assets and compare the file extension in the path. If a path ends with the “.fbx” file extension then we know it is a model and we can load the asset as a GameObject to process it. We are only using “.FBX” files in this example, however, any other file extension can be added as well.

To create a prefab from this model we have to create a prefab asset by either using PrefabUtility.CreatePrefab, or PrefabUtility.ReplacePrefab if the destination prefab already exists.

The final post-processor script looks like this and will create a prefab in the “Prefabs” folder for every FBX model that is imported into your Unity project.

The one big downside of creating new prefabs from models after importing (either manually or with the script above) is that the entire prefab is replaced. So, all modifications made to the prefab after it was created are lost when reimporting the FBX.

In order to work around this and make sure that all our modifications are kept, we will be using the Nested Prefabs plugin. It supports nesting and reloading of models and uses a merging algorithm instead of completely replacing the prefab. This method ensures that any modifications to the model prefab are kept when recreating the prefab from the model.

Because Nested Prefabs reloads the models automatically we have to modify the asset post-processor slightly, so the prefab is only created once as a prefab with a nested model.

The CreatePrefabFromModel method requires the following changes:

  1. If the destination prefab asset already exists then we don’t have to create or replace the prefab asset. This is done automatically by the plugin so we can immediately return.
  2. The model has to be nested inside the prefab, so we create a new empty GameObject to use as the root of the prefab. The model will be a direct child of the GameObject.
  3. It is not possible to modify the hierarchy of the model asset directly. So we have to instantiate the model asset in order to edit it and save it as a new prefab.

Now the post-processor will only create a prefab asset once, with the model as a nested prefab model inside it. The Nested Prefabs plugin will automatically reload the nested model with any changes made to the original model asset.