Unity 显示MeshRenderer的渲染层级
- 前言
- 源码
- MeshRendererInspector
- SkinnedMeshRendererInspector
- 参考
前言
Mesh Renderer
和Skinned Mesh Renderer
组件默认不显示Order
,找了个工具显示一下。
源码
下面两个代码放入Editor文件夹中
MeshRendererInspector
MeshRendererInspector.cs
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
using UnityEditorInternal;[CustomEditor(typeof(MeshRenderer)), CanEditMultipleObjects]
public class MeshRendererInspector : Editor
{//Armazena Sorting Layer criadas no unityprivate string[] sortingLayerNames;//Orderprivate int sortingOrder;//Layerprivate int sortingLayer;//Objetos selecionadosprivate MeshRenderer[] renderer;//Se todos os objetos selecionado possuem os mesmos valoresprivate bool sortingLayerEqual;private bool sortingOrderEqual;void OnEnable(){//Cache de Sorting Layer criadas.sortingLayerNames = GetSortingLayerNames();//Recupera objetos selecionadosSystem.Object[] objects = serializedObject.targetObjects;//Armazena valores iniciaisMeshRenderer first = objects[0] as MeshRenderer;sortingOrder = first.sortingOrder;string layerName = first.sortingLayerName;sortingLayer = Mathf.Max(System.Array.IndexOf(sortingLayerNames, layerName), 0);//Castrenderer = new MeshRenderer[objects.Length];//Igualdade entre multiobjectssortingLayerEqual = true;sortingOrderEqual = true;for (int i = 0; i < objects.Length; i++){//Castrenderer[i] = objects[i] as MeshRenderer;//Verifica se todos os objetos possuem o mesmo valorif (renderer[i].sortingOrder != sortingOrder) sortingOrderEqual = false;if (renderer[i].sortingLayerName != layerName) sortingLayerEqual = false;}}public override void OnInspectorGUI(){base.OnInspectorGUI();EditorGUILayout.Space();/*** SORTING Layer**/EditorGUI.BeginChangeCheck();//UIEditorGUI.showMixedValue = !sortingLayerEqual;sortingLayer = EditorGUILayout.Popup(sortingLayer, sortingLayerNames);//Aplicar modificacoes e igualar valoresif (EditorGUI.EndChangeCheck()){foreach (MeshRenderer r in renderer){r.sortingLayerName = sortingLayerNames[sortingLayer];EditorUtility.SetDirty(r);}sortingLayerEqual = true;}/*** SORTING ORDER**/EditorGUI.BeginChangeCheck();//UIEditorGUI.showMixedValue = !sortingOrderEqual;sortingOrder = EditorGUILayout.IntField("Order in Layer", sortingOrder);//Aplicar modificacoes e igualar valoresif (EditorGUI.EndChangeCheck()){foreach (MeshRenderer r in renderer){r.sortingOrder = sortingOrder;EditorUtility.SetDirty(r);}sortingOrderEqual = true;}}public string[] GetSortingLayerNames(){Type t = typeof(InternalEditorUtility);PropertyInfo prop = t.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);return (string[])prop.GetValue(null, null);}}
SkinnedMeshRendererInspector
SkinnedMeshRendererInspector.cs
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;
using UnityEditorInternal;[CustomEditor(typeof(SkinnedMeshRenderer)), CanEditMultipleObjects]
public class SkinnedMeshRendererInspector : Editor
{//存储 Sorting Layerprivate string[] sortingLayerNames;//Orderprivate int sortingOrder;//Layerprivate int sortingLayer;//选中的对象private SkinnedMeshRenderer[] renderer;//所有选中对象是否具有相同的值private bool sortingLayerEqual;private bool sortingOrderEqual;void OnEnable(){//获取 Sorting LayersortingLayerNames = GetSortingLayerNames();//获取选中的对象System.Object[] objects = serializedObject.targetObjects;//存储初始值SkinnedMeshRenderer first = objects[0] as SkinnedMeshRenderer;sortingOrder = first.sortingOrder;string layerName = first.sortingLayerName;sortingLayer = Mathf.Max(System.Array.IndexOf(sortingLayerNames, layerName), 0);//转换renderer = new SkinnedMeshRenderer[objects.Length];//是否所有对象具有相同的值sortingLayerEqual = true;sortingOrderEqual = true;for (int i = 0; i < objects.Length; i++){//转换renderer[i] = objects[i] as SkinnedMeshRenderer;//检查所有对象是否具有相同的值if (renderer[i].sortingOrder != sortingOrder) sortingOrderEqual = false;if (renderer[i].sortingLayerName != layerName) sortingLayerEqual = false;}}public override void OnInspectorGUI(){base.OnInspectorGUI();EditorGUILayout.Space();/*** SORTING Layer**/EditorGUI.BeginChangeCheck();//UIEditorGUI.showMixedValue = !sortingLayerEqual;sortingLayer = EditorGUILayout.Popup(sortingLayer, sortingLayerNames);//应用修改和同步值if (EditorGUI.EndChangeCheck()){foreach (SkinnedMeshRenderer r in renderer){r.sortingLayerName = sortingLayerNames[sortingLayer];EditorUtility.SetDirty(r);}sortingLayerEqual = true;}/*** SORTING ORDER**/EditorGUI.BeginChangeCheck();//UIEditorGUI.showMixedValue = !sortingOrderEqual;sortingOrder = EditorGUILayout.IntField("Order in Layer", sortingOrder);//应用修改和同步值if (EditorGUI.EndChangeCheck()){foreach (SkinnedMeshRenderer r in renderer){r.sortingOrder = sortingOrder;EditorUtility.SetDirty(r);}sortingOrderEqual = true;}}public string[] GetSortingLayerNames(){Type t = typeof(InternalEditorUtility);PropertyInfo prop = t.GetProperty("sortingLayerNames", BindingFlags.Static | BindingFlags.NonPublic);return (string[])prop.GetValue(null, null);}
}
参考
Unity 3d : Expose the rendering order of MeshRenderer in the Unity3D