コガネブログ

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

【Unity】【Odin - Inspector and Serializer】GUI を拡張する「OnInspectorGUI」属性

目次

「OnInspectorGUI」属性

「OnInspectorGUI」属性を使用すると
GUI を拡張することができます

f:id:baba_s:20170721180616p:plain

using Sirenix.OdinInspector;
using UnityEngine;

public class Example : MonoBehaviour
{
    [OnInspectorGUI( "DrawLabel1" )]
    public int i1;
    
    [OnInspectorGUI( "DrawLabel2", false )]
    public int i2;

    private void DrawLabel1()
    {
        GUILayout.Label( "ヒトカゲ" );
    }

    private void DrawLabel2()
    {
        GUILayout.Label( "ゼニガメ" );
    }

    [OnInspectorGUI]
    private void DrawButton()
    {
        GUILayout.Button( "フシギダネ" );
    }
}

使い方

下記のように記述するだけで使用できます
引数には GUI を拡張する関数の名前を文字列で指定します

[OnInspectorGUI( "DrawLabel1" )]
public int i1;

変数やプロパティの上側で GUI を拡張したい場合は
第 2 引数に false を指定します

[OnInspectorGUI( "DrawLabel2", false )]
public int i2;

関数に対して OnInspectorGUI 属性を適用することも可能です

[OnInspectorGUI]
private void DrawButton()
{
    GUILayout.Button( "フシギダネ" );
}

参考サイト様

Odin Inspector and Serializer | Improve your workflow in Unity