Odin Inspector 系列教程 — Dictionary Drawer Settings Attribute[通俗易懂]

Odin Inspector 系列教程 — Dictionary Drawer Settings Attribute[通俗易懂]Dictionary Drawer Settings 自定义字典绘制方式 默认以左侧为 key 右侧为 value 的形式展示 如果需要进行序列化 需要继承自 SerializedMo DictionaryDr ShowInInspec InfoBox 为了序列化字典

Dictionary Drawer Settings 自定义字典绘制方式
默认以左侧为key,右侧为value 的形式展示,如果需要进行序列化,需要继承自SerializedMonoBehaviour
    [DictionaryDrawerSettings()]
[ShowInInspector]
[InfoBox("为了序列化字典,我们需要做的就是从SerializedMonoBehaviour继承类")]
public Dictionary IntMaterialLookup = new Dictionary()
{
};
【KeyLabel和ValueLabel】自定义标签
    [ShowInInspector]
[DictionaryDrawerSettings(KeyLabel = "自定义 Key 标签名称", ValueLabel = "自定义 Value 标签名称")]
public Dictionary CustomLabels = new Dictionary()
{
{ SomeEnum.First, new MyCustomType() },
{ SomeEnum.Second, new MyCustomType() },
};
【DictionaryDisplayOptions】控制value默认以折叠还是展开形式显示
    [InfoBox("默认是Value折叠方式打开,只在第一次生效")]
[ShowInInspector]
[DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.CollapsedFoldout)]
public Dictionary> StringListDictionary = new Dictionary>()
{
{ "Numbers", new List(){ 1, 2, 3, 4, } },
{ "Numbers1", new List(){ 1, 2, 3, 4, } },
};

[InfoBox("默认是Value展开方式打开,只在第一次生效")]
[ShowInInspector]
[DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.CollapsedFoldout)]
public Dictionary EnumObjectLookup = new Dictionary()
{
{ 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 IntMaterialLookup = new Dictionary()
{
};

[ShowInInspector]
[DictionaryDrawerSettings(KeyLabel = "自定义 Key 标签名称", ValueLabel = "自定义 Value 标签名称")]
public Dictionary CustomLabels = new Dictionary()
{
{ SomeEnum.First, new MyCustomType() },
{ SomeEnum.Second, new MyCustomType() },
};

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

[InfoBox("默认是Value折叠方式打开,只在第一次生效")]
[ShowInInspector]
[DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.CollapsedFoldout)]
public Dictionary> StringListDictionary = new Dictionary>()
{
{ "Numbers", new List(){ 1, 2, 3, 4, } },
{ "Numbers1", new List(){ 1, 2, 3, 4, } },
};

[InfoBox("默认是Value展开方式打开,只在第一次生效")]
[ShowInInspector]
[DictionaryDrawerSettings(DisplayMode = DictionaryDisplayOptions.CollapsedFoldout)]
public Dictionary EnumObjectLookup = new Dictionary()
{
{ 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
}
}

----

编程小号
上一篇 2025-03-15 18:46
下一篇 2025-02-17 13:46

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/139253.html