コガネブログ

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

【Unity】Inspector で編集できる Dictionary を使用できる「Unity-Serialized Dictionary」紹介

はじめに

「Unity-Serialized Dictionary」を Unity プロジェクトに導入することで
Inspector で編集できる Dictionary を使用できるようになります

使用例

f:id:baba_s:20190906114628p:plain

f:id:baba_s:20190906114632p:plain

使い方

using System;
using UnityEngine;

[Serializable]
public sealed class IntStringTable : UDictionary<int, string>
{
}

public sealed class Example : MonoBehaviour
{
    public IntStringTable m_table;
}

Inspector で編集したい Dictionary を UDictionary の継承クラスとして定義します
また、Serializable 属性も適用します

using UnityEditor;

[CustomPropertyDrawer(typeof(IntStringTable))]
public sealed class IntStringTableDrawers : UDictionaryDrawer { }

そして、Unity プロジェクトの「Editor」フォルダに
上記のようなスクリプトを追加することで使用できます

  • UDictionaryDrawer の継承クラスを定義する
  • CustomPropertyDrawer 属性の typeof で自作の Dictionary を指定する