コガネブログ

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

【Unity】RectTransform の Inspector を拡張する方法

概要

using System;
using UnityEditor;
using UnityEngine;

[CustomEditor( typeof( RectTransform ) )]
public sealed class RectTransformEditor : Editor
{
    private static readonly Type DEFAULT_EDITOR_TYPE = typeof( Editor )
        .Assembly
        .GetType( "UnityEditor.RectTransformEditor" );

    private Editor m_defaultEditor;

    public override void OnInspectorGUI()
    {
        if ( m_defaultEditor == null )
        {
            m_defaultEditor = CreateEditor( target, DEFAULT_EDITOR_TYPE );
        }

        GUILayout.Label( "ピカチュウ" );

        m_defaultEditor.OnInspectorGUI();

        GUILayout.Label( "カイリュー" );
    }
}

f:id:baba_s:20190929104514p:plain

注意

上記の方法で RectTransform の Inspector を拡張すると

ArgumentException: Object at index 0 is null
UnityEditor.SerializedObject..ctor (UnityEngine.Object[] objs, UnityEngine.Object context) (at C:/buildslave/unity/build/Editor/Mono/SerializedObject.bindings.cs:39)
UnityEditor.Editor.GetSerializedObjectInternal () (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:525)
UnityEditor.Editor.get_serializedObject () (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:432)
UnityEditor.RectTransformEditor.OnEnable () (at C:/buildslave/unity/build/Editor/Mono/Inspector/RectTransformEditor.cs:100)

スクリプトのコンパイル完了時や Unity 再生時に
上記のエラーが発生してしまうのでご了承ください

参考サイト様

https://forum.unity.com/threads/recttransform-custom-inspector.517878/