コガネブログ

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

【Unity】【Odin - Inspector and Serializer】クラスや構造体のパラメータを折りたたみ無しで表示する「InlineProperty」属性

目次

「InlineProperty」属性

「InlineProperty」属性を使用すると
クラスや構造体のパラメータを折りたたみ無しで表示できるようになります

f:id:baba_s:20170804111910p:plain

using Sirenix.OdinInspector;
using System;
using UnityEngine;

public class Example : MonoBehaviour
{
    public DefaultVector    v1;
    public InlineVector     v2;

    [InlineProperty] public DefaultVector v3;

    public HorizontaleVector v4;
}

[Serializable]
public class DefaultVector
{
    public int x;
    public int y;
}

[Serializable, InlineProperty]
public class InlineVector
{
    public int x;
    public int y;
}

[Serializable, InlineProperty( LabelWidth = 15 )]
public class HorizontaleVector
{
    [HorizontalGroup] public int x;
    [HorizontalGroup] public int y;
}

使い方

通常の書き方だとパラメータが折りたたまれた状態で表示されます

public DefaultVector v1;

...

[Serializable]
public class DefaultVector
{
    public int x;
    public int y;
}

下記のように InlineProperty 属性をクラスや構造体に適用すると
パラメータが折りたたまれずに表示できます

public InlineVector v2;

...

[Serializable, InlineProperty]
public class InlineVector
{
    public int x;
    public int y;
}

InlineProperty 属性は変数に適用することも可能です

[InlineProperty] public DefaultVector v3;

...

[Serializable]
public class DefaultVector
{
    public int x;
    public int y;
}

InlineProperty 属性の LabelWidth を指定したり
HorizontalGroup 属性などと併用したりすることで
Inspector における視認性を向上することができます

public HorizontaleVector v4;

...

[Serializable, InlineProperty( LabelWidth = 15 )]
public class HorizontaleVector
{
    [HorizontalGroup] public int x;
    [HorizontalGroup] public int y;
}

参考サイト様

Odin Inspector and Serializer | Improve your workflow in Unity