Before updating to Unity 2018.3, we used to have a custom AssetImporter for model types. Doing so, upon importing a new model in the project we could create a prefab for that model. If the prefab already existed, we replaced it.
Now, with the Nested Prefabs workflow, I am unable to use the new API.
Now I am supposed to call SaveAsPrefabAsset instead, as far as I know, in both situations. When I do so, however, I get an ArgumentException saying “Can’t save persistent object as a Prefab asset”.
What should I do to be able to create a prefab from a model by script?
That does sound like a bug, could you file a bug report please.
That said I strongly recommend you explore using Prefab Variants if you want to have Prefabs based on Models.
Using Variants means that changes to your models propagate automatically and you don’t have to manually update your custom Prefabs.
Variants are basically instances of other prefabs saved into their own separate prefabs.
So instantiate the model and save the instance to a new prefab.
var modelRootGO = (GameObject)AssetDatabase.LoadMainAssetAtPath("Assets/MyModel.fbx");
var instanceRoot = PrefabUtility.InstantiatePrefab(modelRootGo);
var variantRoot = PrefabUtility.SaveAsPrefabAsset(instanceRoot, "Assets/MyModel_Variant.prefab");
Now that ReplacePrefab w/ ReplacePrefabOptions.ReplaceNameBased is obsolete what is the alternative?
With ReplacePrefab w/ ReplacePrefabOptions.ReplaceNameBased I create Prefabs with a script (I have 100’s of prefabs with identical & complex hierarchies) and this allowed me to adjust & modify prefabs without breaking all scene instance serialized links. Now that ReplacePrefab w/ ReplacePrefabOptions.ReplaceNameBased is obsolete I am unsure what to do. I just used PrefabUtility.SaveAsPrefabAsset and it broke my scene serialized links (I lost a LOT of work!).
Edit: Looks like this will be fixed. Unity post here.
“We’re working on a replacement for ReplacePrefab. You can keep using the obsolete API in the mean time. The replacement was meant to have been done when we shipped but didn’t make it. It’s a mistake on our part that the old method was marked obsolete without the replacement being ready. Sorry for the inconvenience.”
Hello everyone, I am trying to save a gameobject as a prefab in edit mode, but the prefab is being saved with its MeshFilter but not the actual mesh. Is there a way I can save the prefab and keep the reference to the mesh?