Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Editor script to ensure all prefabs in a folder have a specific layer set?

Discussion in 'Editor Workflows' started by habahut, Mar 3, 2024.

  1. habahut

    habahut

    Joined:
    Feb 23, 2018
    Posts:
    8
    I'm learning about the nav mesh agent system in unity, and see that I can automatically bake all objects with a layer into the navmesh surface. This seems really useful as I don't need to ensure all my terrain objects are children of the navmesh surface or in some particular part of the scene hierarchy.

    I'm wondering if there is a way to automatically ensure all prefabs in a specific folder have this layer applied? I have a lot of terrain prefabs and don't want to have to modify them all manually, it would be great if I could just stick them in a folder and not have to worry about applying the layer to all new ones.

    I did a bit of Googling and found [this thread](https://forum.unity.com/threads/how-do-i-edit-prefabs-from-scripts.685711/#post-5139947) for modifying prefabs from a script, but the part I'm confused about is how would I trigger such a script to run? [this tutorial](https://learn.unity.com/tutorial/editor-scripting#) shows how to add various context menus, is there a way to add a context menu to a folder so I could just right click and say "apply layer" or something? Its not clear to me how to add the input of "right clicked on THIS" (the folder path) so the script knows which folder to act on. It would be even better if the editor script just runs automatically but I'm not sure how to make that happen.