Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

SaveAsPrefabAsset returns null (even is successful)

Discussion in 'Prefabs' started by TheJimz, Nov 30, 2018.

  1. TheJimz

    TheJimz

    Joined:
    Jun 26, 2015
    Posts:
    48
    Unity 2018.3.0b11

    Having a problem with prefab creation, where SaveAsPrefabAsset() is successful but returns null. GetCorrespondingObjectFromSource() returns null. AssetDatabase.LoadAssetAtPath() returns null.

    However, the prefab is none the less successfully created. The problem is that I can't create the prefab, and then modify it with AddObjectToAsset() to save meshes, etc. to it.

    Posing a difficult problem in our asset pipeline, and seems to me it must be a bug. I'm making the call inside of PostProcessAllAssets(), and it seems the saved prefab is not being 'imported' until after my code exits, but it's too late then to update it properly.
     
    Airmouse likes this.
  2. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    4,115
    If you think it's a bug, could you please submit a bug-report, as described in this document:
    https://unity3d.com/unity/beta/guide-to-beta-testing#tab-3

    After you submitted the report, you receive a confirmation email with a Case number. Please post this bug-report Case number here (in this thread) for Unity staff to pick up.
     
  3. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    443
    This is not a bug. When create prefabs inside import of other assets, the file is correctly saved, but the import is queued.
    This mean we can't return a valid reference to the asset as the asset does not exists in the assetdatabase.

    What is your use case for needing the new prefab directly in the post processor?
     
  4. TheJimz

    TheJimz

    Joined:
    Jun 26, 2015
    Posts:
    48
    We use the post-processing to detect and combine meshes within imported FBX model prefabs. We create a new prefab with the combined geometry. So I would like to create a new prefab, and then add the combined meshes to it. Seems a reasonable thing to do, as well it used to work in 2017 and prior. It needs to be an automatic generation and not a manually triggered one - would you have a suggestion as to how to go about doing this?
     
    Prodigga likes this.
  5. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    777
    We've had similar workflows in our projects in the past. Definitely important!
     
    Airmouse likes this.
  6. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    443
  7. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    777
    Edit:

    Deleted most of my post, I made up some example to demonstrate why this sucks, but the post Bellow mine does a better job!
     
    Last edited: Dec 3, 2018
    Airmouse likes this.
  8. TheJimz

    TheJimz

    Joined:
    Jun 26, 2015
    Posts:
    48
    I realize I can successfully create a prefab, that isn't the issue. I mentioned it's successfully created in my top post.

    How do I add a mesh to the prefab? Previously we attached the mesh to the prefab with AssetDatabase.AddObjectToAsset(), but we can no longer do that inside the import callback as you can no longer get a reference to it.

    So I'm curious what a proposed solution to the above would be. (adding a mesh to a newly created prefab created in the import callback)
     
    Last edited: Dec 3, 2018
    CardboardDude likes this.
  9. CardboardDude

    CardboardDude

    Joined:
    May 12, 2017
    Posts:
    1
    I too have the problem mentioned above by TheJimz. If anyone has a solution to it I would be grateful.
     
  10. TheJimz

    TheJimz

    Joined:
    Jun 26, 2015
    Posts:
    48
    My workaround is to create the prefab and basically exit out, letting it be imported which will trigger the postprocess callback again, recognize it's our recently created prefab and continue the initialization.
     
    CardboardDude likes this.
  11. nemerle

    nemerle

    Joined:
    Feb 20, 2019
    Posts:
    1
    I've encountered this issue as well while writing an importer for a scene format that uses its prefabs heavily:

    There, I build the source scene-graph (DAG) and count the number of times each internal node is referenced. Every node that has a number of references larger then 1 has to be turned into Unity-side prefab.

    The solution proposed by @SteenLund is of limited use in case of creation of a nested hierarchy of prefab assets during post-process/import phase.
     
  12. TonCol

    TonCol

    Joined:
    Jul 30, 2013
    Posts:
    3
    I have encountered this and ended up caching the path of the saved prefab and implementing a post asset import stage and patching up the references to the prefabs then.