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

should you exclusively use sorting layers instead of order in layer?

Discussion in '2D' started by Mike01923, Nov 5, 2019.

  1. Mike01923

    Mike01923

    Joined:
    Jun 19, 2015
    Posts:
    199
    I find myself being confused about working with sorting orders in the GUI since they are just magic numbers. For example, if I have a player sprite on the "Player" sort layer, and their weapon muzzle flash on the "Player" sort layer with 1 sort order, and the death explosion on the "Player" sort layer with sort order of 2, a month or two down the road, when I see the death explosion is at 2, I have no idea what is at 1 and no quick way to find out.

    Given my example, is it just better to have sort layers for everything and ignore sort order or am I thinking about it wrong?
     
  2. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    370
    First of all there a whole pipeline of sorting, starting from layer, order and Y value, maybe even more, I'm not sure

    "Sorting Layer"s represents planes. Like BG1, BG2, Character, FX, FG, UI. While "Order" represents priority within this plane. Y axis gives depth of view, to show objects "closer" to camera.

    Some old game consoles have 4 planes: plane 1, plane 2, Sprite, Window. Each can display object in low and high priority. So usually it was in order: plane 1 low -> plane 2 low -> sprite low -> plane 1 high -> plane 2 high -> sprite high -> ui.

    I personally use 6 planes and ordering of 0 or 1 to represent priority within plane and leave Y to decide what's on top
     
  3. Mike01923

    Mike01923

    Joined:
    Jun 19, 2015
    Posts:
    199
    Yeah, I understand unity's pipeline of sorting, but from an organizational point of view, sorting order can get confusing keeping track of what is at 0, what is at 1, and so on. It's like a rule in your head that can't be understood from your future self or others.
     
  4. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    370
    Short answer to your given example: you should have Player on "Player" layer, muzzle flash and explosion on "FX" layer.

    Rule of thumb: use up to ten layers and as less ordering as possible, 0 is the best.

    If ordering unavoidable - use component called HightPrioritySprite and LowPrioritySprite to set ordering priority programmatically on void Awake() to avoid magic number and confusions.

    Or store those sprites in separate folders of project "Hight Priority Sprites", "Low Priority Sprites", "Not so prioritized sprites", so you will not forged what they for.
     
    Mike01923 likes this.
  5. Mike01923

    Mike01923

    Joined:
    Jun 19, 2015
    Posts:
    199
    Thanks, those seem like good guidelines. One other thing I'm wondering about for FX, is do people generally have a separate FX layer for all separately sorted game objects or do they just group all FX into one layer?

    For example, if I have a bomb game object that is on a higher layer than the enemy spaceship game object, should I also have a FX layer right above the enemy spaceship and another FX layer right above the bomb object? That would ensure explosions and such are consistently sorted based on the enemy game objects view priority I have going on. That might break your 10 sorting layer rule though. What do you think?
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Order in Layer = Game Objects

    Sorting Layer = UI


    I believe you can add LayerMask to either on top of that as well.
     
  7. vhman

    vhman

    Joined:
    Aug 13, 2018
    Posts:
    370
    FX are shared and repetitive resource. So I have ObjectPool of all FX objects: explosion, blood, muzzle flash. All FX game objects are child of FXPoolManager at the scene root. Every FX objects sprites has FX sorting layer. So as result, all my FX-s sorted on Y value, but always on top of character.

    I have grenades, grenades are interactable with character, you can stand behind grenade or grenade can land behind you, so I put grenades on Character sorting layer. Explosions are not interactable, so they on FX sorting layer (always on top of character).