Sorting Layer Property Drawer

This script will allow you to use the property drawer [SortingLayer] over a sorting layer id property, like:

[SortingLayer]
public int SortingLayer = 0;

screenSortingLayerPropertyView

I hope you find this useful for your projects 🙂

EDIT: code updated using the solution given by Sergi

Download SortingLayerPropertyDrawer.zip

2 thoughts on “Sorting Layer Property Drawer

  1. You can use reflection too 🙂

    using System.Reflection;

    public static void SortingLayerField(GUIContent label, SerializedProperty layerID, GUIStyle style, GUIStyle labelStyle)
    {
    MethodInfo methodInfo = typeof(EditorGUILayout).GetMethod(“SortingLayerField”, BindingFlags.Static | BindingFlags.NonPublic, null, new [] { typeof(GUIContent),typeof(SerializedProperty),typeof(GUIStyle),typeof(GUIStyle) },null);

    if(methodInfo != null)
    {
    object[] parameters = new object[] { label, layerID, style, labelStyle };
    methodInfo.Invoke(null,parameters);
    }
    }

    OnGUI call:

    SortingLayerField(new GUIContent(“Sorting Layer”), m_SortingLayerID, EditorStyles.popup, EditorStyles.label);

    Where m_SortingLayerID is an integer property.

    Cheers!

    Liked by 1 person

    • Thanks for your commentary. It’s a lot better than my solution.
      Now it fits in a simple code box 😉

      [CustomPropertyDrawer(typeof(SortingLayerAttribute))]
      public class SortingLayerPropertyDrawer : PropertyDrawer
      {
      public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
      {
      if (property.propertyType != SerializedPropertyType.Integer)
      {
      Debug.LogError("SortedLayer property should be an integer ( the layer id )");
      }
      else
      {
      SortingLayerField(new GUIContent("Sorting Layer"), property, EditorStyles.popup, EditorStyles.label);
      }
      }

      public static void SortingLayerField(GUIContent label, SerializedProperty layerID, GUIStyle style, GUIStyle labelStyle)
      {
      MethodInfo methodInfo = typeof(EditorGUILayout).GetMethod("SortingLayerField", BindingFlags.Static | BindingFlags.NonPublic, null, new[] { typeof(GUIContent), typeof(SerializedProperty), typeof(GUIStyle), typeof(GUIStyle) }, null);

      if (methodInfo != null)
      {
      object[] parameters = new object[] { label, layerID, style, labelStyle };
      methodInfo.Invoke(null, parameters);
      }
      }
      }

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s