I really think the new nested prefab system will be a huge improvement for most people. However, I generate many prefabs with scripts and the new system is breaking my workflow.
After creating/adjusting my prefabs I use PrefabUtility.CreatePrefab or PrefabUtility.ReplacePrefab w/ ReplacePrefabOptions.ReplaceNameBased.
The PrefabUtility.ReplacePrefab w/ ReplacePrefabOptions.ReplaceNameBased feature works great because it maintains my serialized scene links. The removal of ReplaceNameBased has me extremely worried. I tested the new PrefabUtility.SaveAsPrefabAsset and it breaks all serialized scene links.
With the removal of PrefabUtility.ReplacePrefab w/ ReplacePrefabOptions.ReplaceNameBased how can I generate/adjust prefabs w/ scripts and maintain scene serialized links?
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.
PrefabUtility.SaveAsPrefabAsset[AndConnect] in latest version of 2018.3 automatically support namebased replace and can now safely be used to replace an existing asset. So there is no longer a need for ReplacePrefab.
Another quick question that is related to the prefab generation with scripts:
PrefabUtility.SaveAsPrefabAsset[AndConnect] and other methods all have the InteractionMode parameter. According to its doc, InteractionMode.UserAction “is used to allow a method to show dialog boxes to the user, and allow it to record to the undo history.”.
Reading this implies that passing InteractionMode.UserAction to one of the prefab methods will show a dialog box to the user and records the result to the undo system. However, the PrefabUtility methods only do the latter, according to the source code of the class.
I don’t know what source code you have looked at but they do display dialogs in some cases.
Among others, ApplyObjectOverride and ApplyPropertyOverride may prompt “Mismatching array size” dialog and “Cannot apply reference to object in scene” dialog, ApplyAddedComponent may prompt “Can’t add component” dialog and RevertAddedComponent may prompt “Can’t revert added component” dialog.
I used the resharper to look into the c# source of PrefabUtility.SaveAsPrefabAssetAndConnect. It checks if the argument is of type UserAction and then registers it for an undo and proceeds to save the asset via the _Internal method call which is C++.
I assumed that the dialog is a classic “Do you really want to proceed” dialog box.
My original reply where I mentioned that as an example was wrong. The dialog about saving as a Variant or Original Prefab is handled specifically in dragh-and-drop code. I checked the code and the examples that I listed up there now do display dialogs. So SaveAsPrefabAssetAndConnect may not display a dialog but many of the other APIs do.
PrefabUtility.SaveAsPrefabAssetAndConnect( ) is still breaking my serialized scene links as well as resetting the transform position to (0, 0, 0) for scene objects.
However the old PrefabUtility.ReplacePrefab(…, …, ReplacePrefabOptions.ReplaceNameBased) works properly.
Has anybody gotten PrefabUtility.SaveAsPrefabAssetAndConnect( ) to work properly? Thanks.
Edit: Upgraded to 2019.1.7f1 and PrefabUtility.SaveAsPrefabAssetAndConnect( ) is working without breaking serialized scene links.
If anyone was wondering what “the later version of 2018” was at the time the nice man above referenced it, if you read through all the release notes you’ll find it, Google will not help you, sadly.