コガネブログ

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

【Unity】Inspector のコンポーネントの表示をデフォルトで折りたたんでおくエディタ拡張の例

ソースコード

using System;
using System.Linq;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

[InitializeOnLoad]
internal static class Example
{
    private static readonly Type PROPERTY_EDITOR_TYPE = typeof( Editor ).Assembly.GetType( "UnityEditor.PropertyEditor" );

    static Example()
    {
        Selection.selectionChanged -= OnSelectionChanged;
        Selection.selectionChanged += OnSelectionChanged;

        void OnSelectionChanged()
        {
            var propertyEditor = Resources
                    .FindObjectsOfTypeAll( PROPERTY_EDITOR_TYPE )
                    .FirstOrDefault() as EditorWindow
                ;

            if ( propertyEditor == null ) return;

            var gameObjects = Selection.gameObjects;

            if ( gameObjects == null || gameObjects.Length <= 0 ) return;

            foreach ( var gameObject in gameObjects )
            {
                foreach ( var component in gameObject.GetComponents<Component>() )
                {
                    InternalEditorUtility.SetIsInspectorExpanded
                    (
                        obj: component,
                        isExpanded: false
                    );
                }
            }

            propertyEditor.Repaint();
        }
    }
}

参考サイト様

https://discussions.unity.com/t/is-it-possible-to-fold-a-component-from-script-inspector-view/566466