Sorting Layer Popup (with Add Sorting Layer… option)

Recently, I had to create a Sorting Layer popup for a project I am working on.

I found a way to get the list of sorting layers:

public static string[] GetSortingLayerNames()
{
System.Type internalEditorUtilityType = typeof(InternalEditorUtility);
PropertyInfo sortingLayersProperty =
internalEditorUtilityType.GetProperty(“sortingLayerNames”, BindingFlags.Static | BindingFlags.NonPublic);
return (string[])sortingLayersProperty.GetValue(null, new object[0]);
}

And using the returned list of sorting layer names, create a popup in the editor.
But the editor popup is not showing the special option “Add Sorting Layer…”, like the one in SpriteRenderer:

tempScreen

When you press on “Add Sorting Layer…”, Tags & Layers setting are displayed on the Inspector View, and “Sorting Layers” foldout is open to easily add a new sorting layer to the project.

I have created two methods, similar to the ones used internally by Unity, to create this popup:

  • public static void SortingLayerField(Rect position, GUIContent label, SerializedProperty layerID, GUIStyle style, GUIStyle labelStyle)

This method will draw the popup in the rectangle position.

ex:

SortingLayerFieldLayout(new GUIContent(“Sorting Layer”), serializedObject.FindProperty(“m_sortingLayerID”), EditorStyles.popup, EditorStyles.label);

  • public static void SortingLayerFieldLayout(GUIContent label, SerializedProperty layerID, GUIStyle style, GUIStyle labelStyle)

This is the layout version of the previous method.

Download the scripts here!

 

Advertisements