スポンサーリンク

【Unity】クラスや構造体をInspectorに表示する方法

Unity

通常ではクラスや構造体は表示されない

using UnityEngine;

//クラスと構造体のInspector表示テストクラス
public class ClassStructDispTest : MonoBehaviour
{
    //クラス表示テストクラス
    class ClassParameter
    {
        [SerializeField] private int x, y, z;

        public int scale;
    }

    //構造体表示テストクラス
    struct StructParameter
    {
        [SerializeField] private int x, y, z;

        public int scale;
    }

    [SerializeField]ClassParameter classParameter;

    [SerializeField]StructParameter structParameter;
}

このコードでは、Inspectorにクラスや構造体は表示されません。

宣言時に[System.Serializable]を付ける

クラスと構造体をInspectorに表示したい時は、宣言時に[System.Serializable]を付けることで、表示されます。

using UnityEngine;

//クラスと構造体のInspector表示テストクラス
public class ClassStructDispTest : MonoBehaviour
{
    //クラス表示テストクラス
    [System.Serializable]
    class ClassParameter
    {
        [SerializeField] private int x, y, z;

        public int scale;
    }

    //構造体表示テストクラス
    [System.Serializable]
    struct StructParameter
    {
        [SerializeField] private int x, y, z;

        public int scale;
    }

    [SerializeField]ClassParameter classParameter;

    [SerializeField]StructParameter structParameter;
}

ちゃんと、クラスと構造体の中身がInspectorに表示されています!

お疲れさまでした!

タイトルとURLをコピーしました