Odin Inspector 系列教程 --- Dictionary Drawer Settings Attribute

Dictionary Drawer Settings 自定義字典繪制方式

默認以左側為key,右側為value 的形式展示,如果需要進行序列化,需要繼承自SerializedMonoBehaviour
    [DictionaryDrawerSettings()]
    [ShowInInspector]
    [InfoBox("為了序列化字典,我們需要做的就是從SerializedMonoBehaviour繼承類")]
    public Dictionary<int, Material> IntMaterialLookup = new Dictionary<int, Material>()
{
};
【KeyLabel和ValueLabel】自定義標簽
    [ShowInInspector]
    [DictionaryDrawerSettings(KeyLabel = "自定義 Key 標簽名稱", ValueLabel = "自定義 Value 標簽名稱")]
    public Dictionary<SomeEnum, MyCustomType> CustomLabels = new Dictionary<SomeEnum, MyCustomType>()
{
    { SomeEnum.First, new MyCustomType() },
    { SomeEnum.Second, new MyCustomType() },
};
【DictionaryDisplayOptions】控制value默認以折疊還是展開形式顯示
    [InfoBox("默認是<color=green><size=15><b>Value折疊</b></size></color>方式打開,只在第一次生效")]
    [ShowInInspector]
    [DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.CollapsedFoldout)]
    public Dictionary<string, List<int>> StringListDictionary = new Dictionary<string, List<int>>()
{
    { "Numbers", new List<int>(){ 1, 2, 3, 4, } },
    { "Numbers1", new List<int>(){ 1, 2, 3, 4, } },
};

    [InfoBox("默認是<color=green><size=15><b>Value展開</b></size></color>方式打開,只在第一次生效")]
    [ShowInInspector]
    [DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.ExpandedFoldout)]
    public Dictionary<SomeEnum, MyCustomType> EnumObjectLookup = new Dictionary<SomeEnum, MyCustomType>()
{
    { SomeEnum.Third, new MyCustomType() },
    { SomeEnum.Fourth, new MyCustomType() },
};
完整示例代碼
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DictionaryDrawerSettingsExample : MonoBehaviour
{
    [DictionaryDrawerSettings()]
    [ShowInInspector]
    [InfoBox("為了序列化字典,我們需要做的就是從SerializedMonoBehaviour繼承類")]
    public Dictionary<int, Material> IntMaterialLookup = new Dictionary<int, Material>()
{
};

    [ShowInInspector]
    [DictionaryDrawerSettings(KeyLabel = "自定義 Key 標簽名稱", ValueLabel = "自定義 Value 標簽名稱")]
    public Dictionary<SomeEnum, MyCustomType> CustomLabels = new Dictionary<SomeEnum, MyCustomType>()
{
    { SomeEnum.First, new MyCustomType() },
    { SomeEnum.Second, new MyCustomType() },
};

    [DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.Foldout)]
    [ShowInInspector]
    public Dictionary<string, string> StringStringDictionary = new Dictionary<string, string>()
{
    { "One", ExampleHelper.GetString() },
    { "Two", ExampleHelper.GetString() },
};

    [InfoBox("默認是<color=green><size=15><b>Value折疊</b></size></color>方式打開,只在第一次生效")]
    [ShowInInspector]
    [DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.CollapsedFoldout)]
    public Dictionary<string, List<int>> StringListDictionary = new Dictionary<string, List<int>>()
{
    { "Numbers", new List<int>(){ 1, 2, 3, 4, } },
    { "Numbers1", new List<int>(){ 1, 2, 3, 4, } },
};

    [InfoBox("默認是<color=green><size=15><b>Value展開</b></size></color>方式打開,只在第一次生效")]
    [ShowInInspector]
    [DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.ExpandedFoldout)]
    public Dictionary<SomeEnum, MyCustomType> EnumObjectLookup = new Dictionary<SomeEnum, MyCustomType>()
{
    { SomeEnum.Third, new MyCustomType() },
    { SomeEnum.Fourth, new MyCustomType() },
};



    [InlineProperty(LabelWidth = 100)]
    public struct MyCustomType
    {
        public int SomeMember;
        public GameObject SomePrefab;
    }

    public enum SomeEnum
    {
        First, Second, Third, Fourth, AndSoOn
    }
}

更多教程內容詳見:革命性Unity 編輯器擴展工具 --- Odin Inspector 系列教程

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容