コガネブログ

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

【Unity】EditorGUIUtility.labelWidth の設定範囲を using で指定できるクラス

ソースコード

using System;
using UnityEditor;

public sealed class LabelWidthScope : IDisposable
{
    private readonly float m_oldLabelWidth;

    public LabelWidthScope( int labelWidth )
    {
        m_oldLabelWidth = EditorGUIUtility.labelWidth;
        EditorGUIUtility.labelWidth = labelWidth;
    }

    public void Dispose()
    {
        EditorGUIUtility.labelWidth = m_oldLabelWidth;
    }
}

使用例

using UnityEditor;

public sealed class Example : EditorWindow
{
    [MenuItem( "Tools/Hoge" )]
    private static void Open()
    {
        GetWindow<Example>();
    }

    private void OnGUI()
    {
        EditorGUILayout.TextField( "Name", "ピカチュウ" );

        using ( new LabelWidthScope( 32 ) )
        {
            EditorGUILayout.TextField( "Name", "カイリュー" );
        }
    }
}

f:id:baba_s:20200825220621p:plain