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

Odin Inspector 系列教程 — Dictionary Drawer Settings Attribute[通俗易懂]DictionaryDrawerSettings自定义字典绘制方式默认以左侧为key,右侧为value的形式展示,如果需要进行序列化,需要继承自SerializedMonoBehaviour[DictionaryDrawerSettings()][ShowInInspector][InfoBox(“为了序列化字典,我们需要做…

大家好,又见面了,我是你们的朋友全栈君。

Dictionary Drawer Settings 自定义字典绘制方式

7643202-a77c6a657ab1853a.png

默认以左侧为key,右侧为value 的形式展示,如果需要进行序列化,需要继承自SerializedMonoBehaviour
    [DictionaryDrawerSettings()]
    [ShowInInspector]
    [InfoBox("为了序列化字典,我们需要做的就是从SerializedMonoBehaviour继承类")]
    public Dictionary<int, Material> IntMaterialLookup = new Dictionary<int, Material>()
{
};
【KeyLabel和ValueLabel】自定义标签
7643202-041b4dbc98b338b8.png

    [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默认以折叠还是展开形式显示
7643202-440505f1033e02be.png

    [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.CollapsedFoldout)]
    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.CollapsedFoldout)]
    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 系列教程

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/162804.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 我刚刚做了一个艰难的决定「建议收藏」

    我刚刚做了一个艰难的决定「建议收藏」腾讯做了个艰难的决定,如果您电脑里安装了360软件,建议您卸载了它在运行QQ~可口可乐做出了一个非常艰难的决定,如果检测到用户胃里有百事可乐,将自动释放敌敌畏和鹤顶红中国移动做出一个非常艰难的决定,如果方圆百米内检测到有联通用户将使这些用户不间断自动拨打110迅雷做出一个非常艰难的决定,如果检测到用户电脑内安装快车,将拒绝下载教育片宝马做出了一个非常艰难的决定,如果检测到用户使用过奔驰宝马刹车将自动失灵蒙牛做了个艰难的决定,如果监测到用户胃里有伊利牛奶,将自动释放三聚氰胺麦当劳做了个艰难的决定,如果监测

    2022年7月25日
    9
  • Winform控件开发(1)——Label(史上最全)

    Winform控件开发(1)——Label(史上最全)作用:一般用于显示文本或者作为”按钮使用”,当作为显示文本使用时,通过设置label的Text属性实现,当作为“按钮使用时”,在lable的单击事件下注册事件即可,下面详细介绍label的属性:1、Name属性,该属性代表label类对象的名称,通过该属性可以获取到该label对象,如下图:该label对象名称为label1,当然也可以更改为其他名称2、AllowDrop属性,该属性的值是…

    2022年7月26日
    33
  • Java 集合详解

    Java 集合详解Java集合详解1.集合了解集合类存放于java.util包中。集合类存放的都是对象的引用,而非对象本身。集合的长度可变。2.集合层次关系观看上图需要注意一下实线边框的是实现类折线边框的是抽象类点线边框的是接口2.1Collection接口Collection接口是集合类的根接口,Java中没有提供这个接口的直接的实现类。但是却让其被继承产…

    2022年5月18日
    33
  • 深入理解 MySQL 事务:隔离级别、ACID 特性及其实现原理

    深入理解 MySQL 事务:隔离级别、ACID 特性及其实现原理什么是事务?事务(`Transaction`),一般是指要做的或所做的事情。在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元。事务通常由高级数据库操作语言或编程语言(如SQL,C++或Java)书写的用户程序的执行所引起,并用形如`begintransaction`和`endtransaction`语句(或函数调用)来界定。事务由事务开始(`begintransaction`)和事务结束(`endtransaction`)之间执行的全部操作组成。

    2022年5月26日
    29
  • select top 的用法

    select top 的用法selecttop1*from员工orderby编号select*from员工orderby编号select*from员工where编号likeYG%orderby编号descselectTOP1*from员工where编号likeYG%orderby编号desc

    2022年7月13日
    16
  • 通过ffi在nodejs中调用动态链接库(.so文件)

    通过ffi在nodejs中调用动态链接库(.so文件)通过 ffi 在 nodejs 中调用 C 接口概述获取 C 接口的指针内容异步调用实际案例通过 ffi 在 nodejs 中调用 C 接口概述注意事项 ffi 只能调用 C 风格的模块 需要将 C 源码 build 成动态链接库以供调用 在 Linux 下将 C 源码 build 成 so 文件 在 windows 下 build 成 dll 文件 获取 C 接口的指针内容异步调用实际案例

    2025年11月7日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号