-
-
Save saada/8b84858a654a36cff4f9f0fb2506bcd7 to your computer and use it in GitHub Desktop.
Expose sorting layer in MeshRenderer inspector, for rendering on top of sprites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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