Search Unity

Editor script to find all nested prefabs in the scene

Discussion in 'Prefabs' started by Meceka, Oct 6, 2019.

  1. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    After updating to Unity 2018.3 we've realized that we have made mistakes in the hierarchy and many prefabs are accidentally nested in the scene.

    Therefore I would like to write an editor script that would highlight all the nested prefabs in the scene. I plan to loop through all nested prefabs in the scene and have a debug.log with a reference to gameobject.

    How can I detect which scene objects are nested prefabs, from an editor script?

    Thanks.
     
  2. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    First of all, I would really recommend to upgrade from 2018.3 to latest 2018.4. It's the same, just with more fixes added.

    Anyway, your question depends on what you mean exactly. If this is right after upgrading to 2018.3, then there won't be any nested Prefabs (in the terminology we use) because Unity didn't support nested Prefabs prior to 2018.3. However, you could have Prefabs added in the scene as children of GameObjects belonging to a different Prefab. In this case we say that they are added to the parent Prefab as an override in the scene. These will appear with a plus (+) overlay in the scene Hierarchy.

    Are you talking about detecting such added objects (that happen to be Prefab instances), or did you begin to nest Prefabs (have Prefab Assets that contain instances of other Prefabs inside) after upgrading to 2018.3, and that's what you want to detect?
     
  3. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    We actually used upgraded to Unity 2018.4.9 from 2017.4 but I mentioned 2018.3 as it's the version that added Nested prefabs. We always try to use the latest LTS version. Sorry for the confusion. :)

    After upgrading, I thought they were all nested prefabs but I believe you are right that they have a (+) overlay and I think they are not actually nested, but just parented to each other in prefab instances.

    Yes, I was talking about detecting these kind of objects. Because we have a very large scene, it's just too hard to hunt for (+)'s in the hierarchy. It would be great if we can write a script that gives a reference to all such instances in the console.

    The reason we need to fix those (+) objects is, for example sometimes we replace all instances of a prefab in the scene with some other prefab using the script that's shared here. https://forum.unity.com/threads/replace-game-object-with-prefab.24311/

    In such a case, if there are child (+) objects they would be lost permanently.

    And we are not used to the new prefab system yet. These kinds of objects feel to me like they can cause bugs in the map or break prefab connections.

    Thanks for your response.
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    You can iterate over all GameObjects in the scene and check with https://docs.unity3d.com/ScriptReference/PrefabUtility.IsOutermostPrefabInstanceRoot.html
    This will return true also for added Prefab instances (the ones with a plus overlay).

    I'm not sure what you mean. Prefab instances added as children under other Prefab instances is not new in 2018.3; it was always there. They just weren't very visible before. Now they have the plus overlay so it's easier to see that they are added, and not part of the outer Prefab instance.
     
    Meceka likes this.
  5. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    Yes IsOutermostPrefabInstanceRoot was the thing I was looking for, thank you very much :)

    "it was always there. They just weren't very visible before."
    Thats right, but I am so unfamiliar to the new prefab system, I'm just worried that those prefabs in the scene will accidentally nest each other and somehow get applied and create wormholes in the map. :D

    After updating I broke stuff already. For example just yesterday while in prefab mode of some vehicle, I wanted to compare it with another similar vehicle prefab. So I dragged it in the scene as well. Unfortunately, that wasn't the scene, I was in prefab mode. :eek:
    I was thinking that I was already going to delete that dragged prefab after comparing, so I messed it up. But it was accidentally nested. And of course it auto applied that because I was in prefab mode.
    I had to revert that nested prefab with version control.

    So yes, I'm worried I'm gonna break some other stuff. o_O
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Turn off auto save. It slows down every single operation you do by a lot, in anything else than a tiny project. It also leads to problems like the one you have. When it's off, you get a "do you want to save or discard unsaved changes" checkbox when you leave prefab mode, so there's no problem.
     
    Meceka likes this.
  7. Meceka

    Meceka

    Joined:
    Dec 23, 2013
    Posts:
    423
    Yes for me it's more manageable this way. Thanks for the great recommendation. :)