Skip to content

Instantly share code, notes, and snippets.

@saada
Forked from sinbad/MeshRendererSortingEditor.cs
Last active April 11, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saada/8b84858a654a36cff4f9f0fb2506bcd7 to your computer and use it in GitHub Desktop.
Save saada/8b84858a654a36cff4f9f0fb2506bcd7 to your computer and use it in GitHub Desktop.
Expose sorting layer in MeshRenderer inspector, for rendering on top of sprites
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine.Rendering;
using System;
using System.Collections;
using System.Reflection;
[CustomEditor(typeof(MeshRenderer))]
public class MeshRenderOverrideEditor : Editor
{
MeshRenderer renderer
{
get
{
return target as MeshRenderer;
}
}
public override void OnInspectorGUI()
{
EditorGUILayout.HelpBox("Custom Inspector", MessageType.Info);
EditorGUI.BeginChangeCheck();
var so = new SerializedObject(renderer);
EditorGUILayout.PropertyField(so.FindProperty("m_CastShadows"));
EditorGUILayout.PropertyField(so.FindProperty("m_ReceiveShadows"));
EditorGUILayout.PropertyField(so.FindProperty("m_MotionVectors"));
EditorGUILayout.PropertyField(so.FindProperty("m_Materials"), true);
var options = GetSortingLayerNames();
var picks = new int[options.Length];
var name = renderer.sortingLayerName;
var choice = -1;
for (int i = 0; i < options.Length; i++)
{
picks[i] = i;
if (name == options[i]) choice = i;
}
choice = EditorGUILayout.IntPopup("Sorting Layer", choice, options, picks);
renderer.sortingLayerName = options[choice];
renderer.sortingOrder = EditorGUILayout.IntField("Sorting Order", renderer.sortingOrder);
renderer.lightProbeUsage = (LightProbeUsage)EditorGUILayout.EnumPopup("Ligth Probes", renderer.lightProbeUsage);
renderer.reflectionProbeUsage = (ReflectionProbeUsage)EditorGUILayout.EnumPopup("Reflection Probes", renderer.reflectionProbeUsage);
renderer.probeAnchor = EditorGUILayout.ObjectField("Anchor Override", renderer.probeAnchor, typeof(Transform), true) as Transform;
if (EditorGUI.EndChangeCheck())
SceneView.RepaintAll();
}
public string[] GetSortingLayerNames()
{
Type internalEditorUtilityType = typeof(InternalEditorUtility);
PropertyInfo sortingLayersProperty = internalEditorUtilityType.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);
return (string[])sortingLayersProperty.GetValue(null, new object[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment