Site Overlay

Sorting Layer Property Drawer

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!

    1. 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);
      }
      }
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

css.php