コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】SortingLayerEditorUtility クラスの internal な機能にアクセスできる機能

リポジトリ

使用例

using System;
using UnityEditor;
using UnityEngine;

namespace Kogane.Internal
{
    [CustomEditor( typeof( MeshRenderer ) )]
    internal sealed class MeshRendererInspector : Editor
    {
        private Editor             m_editor;
        private SerializedProperty m_sortingOrderProperty;
        private SerializedProperty m_sortingLayerIDProperty;

        public override void OnInspectorGUI()
        {
            if ( m_editor == null )
            {
                CreateCachedEditor
                (
                    targetObjects: targets,
                    editorType: Type.GetType( "UnityEditor.MeshRendererEditor, UnityEditor" ),
                    previousEditor: ref m_editor
                );
            }

            m_sortingOrderProperty   ??= serializedObject.FindProperty( "m_SortingOrder" );
            m_sortingLayerIDProperty ??= serializedObject.FindProperty( "m_SortingLayerID" );

            m_editor.OnInspectorGUI();
            serializedObject.Update();

            SortingLayerEditorUtilityInternal.RenderSortingLayerFields
            (
                sortingOrder: m_sortingOrderProperty,
                sortingLayer: m_sortingLayerIDProperty
            );

            serializedObject.ApplyModifiedProperties();
        }
    }
}

ScreenShot00129