Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Is it possible to have unique layers in every scene?

Discussion in 'Editor & General Support' started by WilliamTurns, Oct 8, 2023.

  1. WilliamTurns

    WilliamTurns

    Joined:
    Jun 18, 2022
    Posts:
    5
    The Layers and Sorting Layers prevail throughout the project and I have a lot of folders and scenes in a massive project that I use for prototyping stuff. Is there a way to restrict the Layers and Sorting to particular scenes? Like an asset or setting that I don't know of.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,273
    Ugh, yeah, I feel your pain. AFAIK the answer is "no."

    It's even worse because Sorting Layers, IIRC, won't port between projects, at least they didn't last time I checked. IOW, if you have Sorting Layer
    foo
    in project A and then make a Sorting Layer
    foo
    in project B, then make scenes / prefabs using those sorting layers, they are NOT the same layer as Unity refers to them by some arbitrary integer hash.

    I tend to control it by just making code explicitly drive things like Layer / SortingLayer... that code will generally port over if you move your prototype game "embryo" from one project to another.

    My largest prototype Unity project (I have actually many of these for various purposes) is completely out of control with over 500 scenes.

    Screen Shot 2023-10-08 at 11.05.50 AM.png

    BUT!! I could stop at any point if I wanted to. :) Er, no I can't.
     
  3. WilliamTurns

    WilliamTurns

    Joined:
    Jun 18, 2022
    Posts:
    5
    Would've been quite nice if they did make the option to have scene specific sorting layers/layers. I've had the same issue while importing project assets. Perhaps it's to preserve the integrity of bigger projects, but it's a bane for prototyping.

    500 scenes, eh? There are entire Netflix series with fewer than that... Competing with One Piece, are we?
     
    Kurt-Dekker likes this.
  4. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    87
    The layers are saved as integers from 0 to 31 so in theory you could have a script that just changes the names of the layers when opening a scene. The names are saved in
    ProjectSettings/TagManager.asset
    and there might be some editor api that can change them. Then you must have a script in each scene with the list of names specific to that scene that then applies it (also in edit mode). This obviously will break if you load scenes additively that have different meanings for the layers. If you set layers in prefabs you also could not share them between scenes with different layer definitions if the definitions were not compatible. This is probably why Unity has not bothered to add this feature
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,273
    Yes, this is true for Layers. AFAIK you don't even technically NEED to define a layer to use it in code.

    BUT... OP also called out Sorting Layers, and those are the tricky ones. Here's why:

    The human-readable name is NOT used to identify the Sorting Layer.

    Instead a capriciously-assigned value is generated, different (apparently) between every project.

    Screen Shot 2023-10-09 at 9.34.16 AM.png

    Unfortunately this seemingly-random value is what gets serialized to your scenes / prefabs, which is a data design failure for (AFAICT) no actual benefit to anything:

    Screen Shot 2023-10-09 at 9.42.28 AM.png

    The m_SortingLayer: appears to be an index into the list, but it is apparently ignored on the tests I did.

    If someone knows how to safely pass prefabs from one project to another while preserving the SortingLayer regardless of ID, I'd love to hear it. We just manually go through check them all anytime we import a "foreign" prefab.

    You bring up an excellent point about Sorting Layers being hand-editable via the
    TagManager.asset
    text file, and that could certainly be a way to ensure consistency across multiple projects.

    But you would have to have the foresight to pre-program all your desired SortingLayers in and copy them from project to project, all before you made ANY asset using them. That is FAR outside the bounds of what I would consider normal reasonable use of the feature.