This script will allow you to use the property drawer [SortingLayer] over a sorting layer id property, like:
[SortingLayer]
public int SortingLayer = 0;
I hope you find this useful for your projects 🙂
EDIT: code updated using the solution given by Sergi
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!
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);
}
}
}