Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Alternative to ReplacePrefab

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

  1. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    Now that PrefabUtility.ReplacePrefab w/ ReplacePrefabOptions.ReplaceNameBased is obsolete I am unsure what to do.

    My workflow:
    1: I use a script to generate 100's of prefabs for monsters. I do this because my monsters hierarchies are complex and nearly identical. For example I may have an Orc, and a BigOrc that is 20% larger with slightly different stats.
    2: The prefab generating script makes a new prefab using PrefabUtility.CreatePrefab, and it modifies and brushes up on existing prefabs using PrefabUtility.ReplacePrefab w/ ReplacePrefabOptions.ReplaceNameBased.

    The reason PrefabUtility.ReplacePrefab w/ ReplacePrefabOptions.ReplaceNameBased is so important is it doesn't break any serialized scene links. Unfortunately the new PrefabUtility.SaveAsPrefabAsset breaks all scene.

    Here's an example of prefabs generated with a script. Again I use the script to make slightly modifications (tint one red and make it faster, or make one 30% larger and stronger, etc.)


    I am unsure what to do now that PrefabUtility.ReplacePrefab is obsolete. Thanks for any help.
     
    Last edited: Jan 5, 2019
    golmae likes this.
  2. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    Edit: This will be fixed!

    https://forum.unity.com/threads/how...scripts-w-the-new-system.613747/#post-4113010

    From Unity
    "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."
     
    RandyLarson and golmae like this.
  3. HeroYss

    HeroYss

    Joined:
    Jan 24, 2017
    Posts:
    3
    it helps,thank you
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
  5. GoldenFoxLLC

    GoldenFoxLLC

    Joined:
    Nov 28, 2019
    Posts:
    7
  6. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    @GoldenFoxLLC

    Can you share some more details and maybe a small test project?
     
  7. GoldenFoxLLC

    GoldenFoxLLC

    Joined:
    Nov 28, 2019
    Posts:
    7
    Sure. I made this model importer script that extracts meshes as separate prefab files from a Maya file.

    Code (CSharp):
    1. private void Import(Transform @group, string outputPath, string folder)
    2. {
    3.     foreach (Transform transform in @group)
    4.     {
    5.         var folderPath = Path.Combine(outputPath, folder);
    6.         if (!AssetDatabase.IsValidFolder(folderPath))
    7.         {
    8.             var guid = AssetDatabase.CreateFolder(outputPath, folder);
    9.             folderPath = AssetDatabase.GUIDToAssetPath(guid);
    10.         }
    11.  
    12.         if (transform.childCount > 0)
    13.         {
    14.             Import(transform, folderPath, folder + $"{transform.name.Replace(modelType.ToString(), string.Empty)}");
    15.             continue;
    16.         }
    17.  
    18.         var instance = Instantiate(transform).gameObject;
    19.         var fileName = $"{instance.name.Replace("(Clone)", string.Empty)}.prefab";
    20.         var assetPath = Path.Combine(folderPath, fileName);
    21.        
    22.         PrefabUtility.SaveAsPrefabAssetAndConnect(instance, AssetDatabase.GenerateUniqueAssetPath(assetPath),
    23.             InteractionMode.AutomatedAction);
    24.  
    25.         DestroyImmediate(instance);
    26.     }
    27. }
     
  8. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639
    AssetDatabase.GenerateUniqueAssetPath(assetPath)
    will generate a unique name and make sure to never collide with existing filenames in the folder
     
  9. GoldenFoxLLC

    GoldenFoxLLC

    Joined:
    Nov 28, 2019
    Posts:
    7
    Right, I don't know how I missed that.

    Thanks.