[CanEditMultipleObjects]
[CustomEditor(typeof(MeshRenderer))]
public class MeshRendererEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        serializedObject.Update();
        
        SerializedProperty sortingLayerID = serializedObject.FindProperty("m_SortingLayerID");
        
        SerializedProperty sortingOrder = serializedObject.FindProperty("m_SortingOrder");
        
        //MeshRenderer renderer = target as MeshRenderer;
        Rect firstHoriz = EditorGUILayout.BeginHorizontal();
        EditorGUI.BeginChangeCheck();
        EditorGUI.BeginProperty(firstHoriz, GUIContent.none, sortingLayerID);
        
        string[] layerNames = GetSortingLayerNames();
        int[] layerID = GetSortingLayerUniqueIDs();
        
        int selected = -1;
        int sID = sortingLayerID.intValue;
        for (int i = 0; i < layerID.Length; i++)
            if (sID == layerID[i])
                selected = i;
        if (selected == -1)
            for (int i = 0; i < layerID.Length; i++)
                if (layerID[i] == 0)
                    selected = i;
        selected = EditorGUILayout.Popup("Sorting Layer", selected, layerNames);
        sortingLayerID.intValue = layerID[selected];
        EditorGUI.EndProperty();
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(sortingOrder, new GUIContent("Order in Layer"));
        EditorGUILayout.EndHorizontal();

        serializedObject.ApplyModifiedProperties();
    }
    public string[] GetSortingLayerNames()
    {
        Type internalEditorUtilityType = typeof(InternalEditorUtility);
        PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
        return (string[])sortingLayersProperty.GetValue(null, new object[0]);
    }
    public int[] GetSortingLayerUniqueIDs()
    {
        Type internalEditorUtilityType = typeof(InternalEditorUtility);
        PropertyInfo sortingLayerUniqueIDsProperty = internalEditorUtilityType.GetProperty("sortingLayerUniqueIDs", BindingFlags.Static | BindingFlags.NonPublic);
        return (int[])sortingLayerUniqueIDsProperty.GetValue(null, new object[0]);
    }
}

注意:

Drawing-Order-Of-meshes-and-sprites

参考:

Drawing-Order-Of-meshes-and-sprites

相关文章:

  • 2021-10-05
  • 2022-12-23
  • 2021-09-05
  • 2022-02-28
  • 2022-12-23
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-03
  • 2021-08-23
  • 2022-12-23
  • 2021-07-05
  • 2022-12-23
  • 2021-06-21
  • 2021-08-01
相关资源
相似解决方案