Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Sorting Layer VS Layer Mask Scripting

Discussion in 'Scripting' started by Phocus, Jul 8, 2015.

  1. Phocus

    Phocus

    Joined:
    Jul 6, 2015
    Posts:
    18
    If I define
    LayerMask sortingLayerSelect;

    In the editor I get a drop down list of Layers.

    How can I replicate this for the sorting layers from the sprite rederer in 2d?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the default setting is to have no sorting layers... you will need to create some in order for something to show in the Sorting Layer drop down field of the sprite renderer inspector.
     
  3. Phocus

    Phocus

    Joined:
    Jul 6, 2015
    Posts:
    18
    They show Up in the sortinglayer defatult dropdown. I want to add it to my script so you can select the sorting layer of my script without having to type it in.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    You're going to have to create a custom editor for this.

    I'm assuming this field is just an int.

    So create an attribute that inherits from PropertyAttribute in your regular scripts folder.

    Code (csharp):
    1.  
    2. public class SortingLayerAttribute : PropertyAttribute
    3. {
    4.  
    5. }
    6.  
    Then create a PropertyDrawer in your Editor scripts folder for that attribute:

    Code (csharp):
    1.  
    2. [CustomPropertyDrawer(typeof(SortingLayerAttribute))]
    3. public class SortyingLayerPropertyDrawer : PropertyDrawer
    4. {
    5.     override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    6.     {
    7.         //get the available layers, draw a popup with EditorGUI.Popup, update property with the info
    8.     }
    9. }
    10.  
    Now where you have a int field that is supposed to be a sorting layer, tag it with that attribute:

    Code (csharp):
    1.  
    2. [SortingLayer()]
    3. public int SortingLayer;
    4.  

    Here's the documentation for PropertyDrawers:
    http://docs.unity3d.com/ScriptReference/PropertyDrawer.html

    And the documentation for EditorGUI.Popup:
    http://docs.unity3d.com/ScriptReference/EditorGUI.Popup.html
     
  5. Phocus

    Phocus

    Joined:
    Jul 6, 2015
    Posts:
    18
    Thank you! I am going to give it a shot.
     
  6. ShuuGames

    ShuuGames

    Joined:
    Jun 25, 2013
    Posts:
    188
    It's an old thread, but for those who find it, here is a working implementation for what was suggested above. For Unity 5.3+

    Use like this:
    Code (csharp):
    1. [IsSortingLayer] public string sortingLayerName;
    IsSortingLayerAttribute.cs
    Code (csharp):
    1. using UnityEngine;
    2. public class IsSortingLayerAttribute : PropertyAttribute {}
    Editor/IsSortyingLayerPropertyDrawer.cs
    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomPropertyDrawer(typeof(IsSortingLayerAttribute))]
    5. public class IsSortyingLayerPropertyDrawer : PropertyDrawer
    6. {
    7.   public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    8.   {
    9.     int selected = -1;
    10.  
    11.     var names = new string[SortingLayer.layers.Length];
    12.     for(int i=0; i<SortingLayer.layers.Length; i++)
    13.     {
    14.       names[i] = SortingLayer.layers[i].name;
    15.       if(property.stringValue == names[i])
    16.         selected = i;
    17.     }
    18.  
    19.     EditorGUI.BeginProperty(position, label, property);
    20.     position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
    21.     selected = EditorGUI.Popup(position, selected, names);
    22.     EditorGUI.EndProperty();
    23.  
    24.     if(selected >= 0)
    25.       property.stringValue = names[selected];
    26.   }
    27. }
     
    Last edited: Jan 6, 2016