Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Creating prefabs from models by script

Discussion in 'Prefabs' started by Cobo3, Jan 3, 2019.

  1. Cobo3

    Cobo3

    Joined:
    Jun 16, 2013
    Posts:
    60
    Hi,

    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.

    Code (CSharp):
    1. if( !File.Exists( destinationPath ))
    2.     return PrefabUtility.CreatePrefab(destinationPath, modelAsset);
    3. else
    4.     return PrefabUtility.ReplacePrefab(modelAsset, AssetDatabase.LoadAssetAtPath<GameObject>(destinationPath), ReplacePrefabOptions.ReplaceNameBased);
    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?
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    365
    Hi,

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

    Cobo3

    Joined:
    Jun 16, 2013
    Posts:
    60
    If that's indeed a bug, I will report it.

    About prefab variants, it sounds great to make a prefab variant from the model, but how can I do that from code?
     
  4. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    365
    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.

    Code (CSharp):
    1. var modelRootGO = (GameObject)AssetDatabase.LoadMainAssetAtPath("Assets/MyModel.fbx");
    2.  
    3. var instanceRoot = PrefabUtility.InstantiatePrefab(modelRootGo);
    4. var variantRoot = PrefabUtility.SaveAsPrefabAsset(instanceRoot, "Assets/MyModel_Variant.prefab");
     
  5. Cobo3

    Cobo3

    Joined:
    Jun 16, 2013
    Posts:
    60
    Instantiating the model, creating a prefab from the instance and then destroying the instance solved the problem.

    Thank you so much!

    I still sent a bug report though.
     
  6. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    550
    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.
    https://forum.unity.com/threads/alternative-to-replaceprefab.607759/#post-4114699
     
    Last edited: Jan 22, 2019 at 2:40 AM
    MMind and golmae like this.