Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A unity mesh renderer inspector to view and modify renderer's sorting layer and material's render queue.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(MeshRenderer), true)]
public class MeshRendererInspector : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
OnGUISortingLayer();
}
private void OnGUISortingLayer()
{
GUILayout.Space(6);
var rend = (MeshRenderer)target;
GUILayout.BeginVertical("box");
var layerNames = SortingLayer.layers.Select(x => x.name).ToList();
EditorGUI.BeginChangeCheck();
var selectedIndex = EditorGUILayout.Popup("SortingLayerName", layerNames.IndexOf(rend.sortingLayerName), layerNames.ToArray());
if (EditorGUI.EndChangeCheck())
{
rend.sortingLayerName = SortingLayer.layers[selectedIndex].name;
EditorUtility.SetDirty(rend.gameObject);
}
EditorGUI.BeginChangeCheck();
var sortingOrder = EditorGUILayout.IntField("SortingOrder", rend.sortingOrder);
if (EditorGUI.EndChangeCheck())
{
rend.sortingOrder = sortingOrder;
EditorUtility.SetDirty(rend.gameObject);
}
var mats = rend.sharedMaterials;
foreach (var mat in mats)
DrawMaterialRenderQueue(mat);
GUILayout.EndVertical();
}
private void DrawMaterialRenderQueue(Material mat)
{
GUILayout.BeginHorizontal();
EditorGUILayout.ObjectField(mat, typeof(Material), false);
EditorGUI.BeginChangeCheck();
var val = EditorGUILayout.IntField("RenderQueue", mat.renderQueue);
if (EditorGUI.EndChangeCheck())
{
mat.renderQueue = val;
EditorUtility.SetDirty(mat);
}
GUILayout.EndHorizontal();
}
}
@zilongshanren
Copy link

zilongshanren commented Apr 12, 2021

A improved version:

/// This just exposes the Sorting Layer / Order in MeshRenderer since it's there
/// but not displayed in the inspector. Getting MeshRenderer to render in front
/// of a SpriteRenderer is pretty hard without this.
[CustomEditor(typeof(MeshRenderer), true)]
public class MeshRendererSortingEditor : Editor
{
    Editor defaultEditor;
    void OnEnable()
    {
        defaultEditor = CreateEditor(targets, Type.GetType("UnityEditor.MeshRendererEditor, UnityEditor"));
    }

    public override void OnInspectorGUI()
    {
        defaultEditor.OnInspectorGUI();

        MeshRenderer renderer = target as MeshRenderer;


        var layers = SortingLayer.layers;

        EditorGUILayout.BeginHorizontal();
        EditorGUI.BeginChangeCheck();
        int newId = DrawSortingLayersPopup(renderer.sortingLayerID);
        if(EditorGUI.EndChangeCheck()) {
            renderer.sortingLayerID = newId;
        }
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        EditorGUI.BeginChangeCheck();
        int order = EditorGUILayout.IntField("Sorting Order", renderer.sortingOrder);
        if(EditorGUI.EndChangeCheck()) {
            renderer.sortingOrder = order;
        }
        EditorGUILayout.EndHorizontal();

    }

    int DrawSortingLayersPopup(int layerID) {
        var layers = SortingLayer.layers;
        var names = layers.Select(l => l.name).ToArray();
        if (!SortingLayer.IsValid(layerID))
        {
            layerID = layers[0].id;
        }
        var layerValue = SortingLayer.GetLayerValueFromID(layerID);
        var newLayerValue = EditorGUILayout.Popup("Sorting Layer", layerValue, names);
        return layers[newLayerValue].id;
    }

}

Unless you call the original MeshRenderer's OnInspectorGUI function, otherwise the lightmap settings perperties will be missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment