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
}
}