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 unity
private string[] sortingLayerNames;
//Order
private int sortingOrder;
//Layer
private int sortingLayer;
//Objetos selecionados
private MeshRenderer[] renderer;
//Se todos os objetos selecionado possuem os mesmos valores
private bool sortingLayerEqual;
private bool sortingOrderEqual;
void OnEnable()
{
//Cache de Sorting Layer criadas.
sortingLayerNames = GetSortingLayerNames();
//Recupera objetos selecionados
System.Object[] objects = serializedObject.targetObjects;
//Armazena valores iniciais
MeshRenderer first = objects[0] as MeshRenderer;
sortingOrder = first.sortingOrder;
string layerName = first.sortingLayerName;
sortingLayer = Mathf.Max(System.Array.IndexOf(sortingLayerNames, layerName), 0);
//Cast
renderer = new MeshRenderer[objects.Length];
//Igualdade entre multiobjects
sortingLayerEqual = true;
sortingOrderEqual = true;
for (int i = 0; i < objects.Length; i++)
{
//Cast
renderer[i] = objects[i] as MeshRenderer;
//Verifica se todos os objetos possuem o mesmo valor
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();
//UI
EditorGUI.showMixedValue = !sortingLayerEqual;
sortingLayer = EditorGUILayout.Popup(sortingLayer, sortingLayerNames);
//Aplicar modificacoes e igualar valores
if (EditorGUI.EndChangeCheck())
{
foreach (MeshRenderer r in renderer)
{
r.sortingLayerName = sortingLayerNames[sortingLayer];
EditorUtility.SetDirty(r);
}
sortingLayerEqual = true;
}
/**
* SORTING ORDER
**/
EditorGUI.BeginChangeCheck();
//UI
EditorGUI.showMixedValue = !sortingOrderEqual;
sortingOrder = EditorGUILayout.IntField("Order in Layer", sortingOrder);
//Aplicar modificacoes e igualar valores
if (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 Layer
private string[] sortingLayerNames;
//Order
private int sortingOrder;
//Layer
private int sortingLayer;
//选中的对象
private SkinnedMeshRenderer[] renderer;
//所有选中对象是否具有相同的值
private bool sortingLayerEqual;
private bool sortingOrderEqual;
void OnEnable()
{
//获取 Sorting Layer
sortingLayerNames = 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();
//UI
EditorGUI.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();
//UI
EditorGUI.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