Search Unity

Question How to use Hierarchy Prefab

Discussion in 'Prefabs' started by JonasSchnettker, Dec 1, 2022.

  1. JonasSchnettker

    JonasSchnettker

    Joined:
    Nov 3, 2022
    Posts:
    4
    Hi all,
    I got a project with some people and I want to make sure they use the same Hierarchy in each scene. I tried this with a Prefab -> Variants.But it seems like bad practice, since drag and drop will trigger a popup window each time.
    Any ideas how to do this?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,956
    • Template Scene: provide a template scene with a structure already set up. Every new scene has to be a copy of that scene.
    • Editor Script: write an editor script that creates a new scene asset and adds and renames GameObjects that create the desired hierarchy. It should probably offer a wizard dialogue (there's an editor wizard window class I believe) to allow creators to specify name of the scene and other details. Every new scene needs to be created through that menu item.
    • Alternative Editor Script: rather than creating a new scene, this works on an empty scene and simply adds the desired hierarchy. HOWEVER care must be taken to avoid duplicating the hierarchy (or possibly deleting parts of the scene) if that script is run again on the same scene as a designer might run it only after hours of working.

    Theoretically, you could also write an editor script that monitors the scene and makes changes to the scene hierarchy if it "isn't correct" but I'd strongly advise against doing so because this can terribly mess up a scene in unexpected ways, or simply waste time because someone might not understand what's happening and keeps fighting with that script, or the script might delete an entire tree of the scene simply because one of the root objects got renamed to a "forbidden" name and so on.

    Personally, I'd go for the Template scene. It's easy to set up and use and everyone understands it. You can add a script that adds "Create Scene from Template" to the Create Asset menu. It may even be possible to replace the existing "Create Scene" menu item but I'm not sure about that, it probably requires reverse engineering that menu to find and delete to proper item or to change the method it runs through reflection.
     
  3. JonasSchnettker

    JonasSchnettker

    Joined:
    Nov 3, 2022
    Posts:
    4
    I also considered Editor Script, but it seems tidious and kind of wrong. Same reasoning as yours.

    Last time I worked with reflection was fun, but also time consuming. That's why I thought, there must be an easier way. I work with unity for years now and totally did not know that Scene Templates exist xD