Search Unity

Modifying Prefabs from Editor Script

Discussion in 'Prefabs' started by LesBloom, Jul 17, 2019.

  1. LesBloom

    LesBloom

    Joined:
    Feb 2, 2017
    Posts:
    167
    Hi all,

    I have read a few threads, but I am still stuck
    https://forum.unity.com/threads/how-do-i-edit-prefabs-from-scripts.685711/
    https://forum.unity.com/threads/bug-problem-with-indirectly-adding-components.544168/
    etc

    The PrefabUtility.SaveAsPrefabAsset approach

    I am getting bugs with this approach. I have a script that scrapes all assets via:

    AssetDatabase.FindAssets
    AssetDatabase.GUIDToAssetPath

    If the asset is a GameObject, then
    GameObject prefabInstance = PrefabUtility.LoadPrefabContents
    <delete some components from prefabInstance>
    PrefabUtility.SaveAsPrefabAsset
    PrefabUtility.UnloadPrefabContents

    If I run this on all prefabs then the Unity Editor ends up getting stuck in a soft lock. Some of the Unity Editor buttons (like File, Edit, Help, close, minimize, etc) work. However, the rest of the Unity Editor is locked up. I can't switch between any tabs. I can't select or inspect any asset or GameObject. My only choice is to close the Editor.

    So, I started reading the documentation more, and came across this tidbit:

    Note: Because this matching is done by name only, if there are multiple GameObjects with the same name in the Prefab's hierarchy, you cannot predict which will be matched. Therefore if you need to ensure your references are preserved when saving over an existing prefab, you must ensure all GameObjects within the Prefab have unique names.

    Also note: You may encounter a similar problem in the case of preserving references to existing Components when you save over an existing Prefab, if a single GameObject within the Prefab has more than one of the same Component type attached. In this case you cannot predict which of them will be matched to the existing references.
    This alone makes me think that I should not be trying to use the PrefabUtility.SaveAsPrefabAsset.


    The PrefabUtility.SavePrefabAsset approach

    Based on the above, I am lead to believe that this is the better approach for me to take. However, I can't figure it out. The documentation page is not clear to me. And everyone in this forum seems to keep saying to use the SaveAS version.

    Any help on this would be greatly appreciated.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    PrefabUtility.SaveAsPrefabAsset is the correct method to use in conjunction with PrefabUtility.LoadPrefabContents. The note you're referring to is irrelevant when you're loading a Prefab content's and saving back into the same Prefab.

    I don't know why the editor is locking up. If you have a huge amount of data in your project, the process might just take a long time. I would suggest trying out your code on just one Prefab and get that working before you start executing it on all Prefabs in the project in one go.

    Does the code work on one Prefab without locking up? Does it give the expected result?

    If it locks up regardless, that might be a bug, and then we'd appreciate a bug report. Here too it would be best if the project you submit demonstrates your code being applied to just one Prefab.
     
  3. LesBloom

    LesBloom

    Joined:
    Feb 2, 2017
    Posts:
    167
    Hi @runevision ,

    Thank you for the clarification about that 'naming note'.

    Regarding the lock up. I did eventually find the prefab. I had to do just what you described; and go 1 by 1, session by session, through a couple hundred before I found the culprit.

    There is no Editor output or error of any kind either. So it took a while.

    That being said, the bug was because I called PrefabUtility.LoadPrefabContents with a .fbx asset.