Search Unity

How to Generate Prefabs w/ Scripts w/ The New System?

Discussion in 'Prefabs' started by Zullar, Jan 16, 2019.

  1. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    615
    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?

    Thanks in advance.
     
  2. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,612
    Hi,

    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.
     
  3. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    615
    Thanks. This is great news!
     
  4. TriNityGER

    TriNityGER

    Joined:
    Sep 1, 2017
    Posts:
    29
    I assume PrefabUtility.SaveAsPrefabAssetAndConnect is the replacement for PrefabUtility.ReplacePrefab() with ReplacePrefabOptions.ConnectToPrefab?

    Anyways, whats the status of the replacement for ReplacePrefab in 2019.1?
     
  5. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    443
    Yes.

    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.
     
    Last edited: Apr 25, 2019
    chris_schubert, Zullar and TriNityGER like this.
  6. TriNityGER

    TriNityGER

    Joined:
    Sep 1, 2017
    Posts:
    29
    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.

    Is this actually intended behaviour?
     
  7. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,612
    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.
     
    Last edited: Apr 26, 2019
    TriNityGER likes this.
  8. TriNityGER

    TriNityGER

    Joined:
    Sep 1, 2017
    Posts:
    29
    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.
     
  9. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,612
    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.
     
    TriNityGER likes this.
  10. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    723
    Hi, I used

    Code (CSharp):
    1. PrefabUtility.ReplacePrefab(p.gameObject, PrefabUtility.GetPrefabParent(p.gameObject), ReplacePrefabOptions.ConnectToPrefab);
    Is this correct to replace with:
    Code (CSharp):
    1. var parent = PrefabUtility.GetCorrespondingObjectFromSource(p.gameObject);
    2. var parentPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(parent);
    3. PrefabUtility.SaveAsPrefabAssetAndConnect(p.gameObject, parentPath, InteractionMode.AutomatedAction);
    4.  
    thank you!
     
  11. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    615
    2018.3.0f2

    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.
     
    Last edited: Jun 18, 2019
    SteenLund and Quatum1000 like this.