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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PrintWriter and BufferedWriter区别和使用「建议收藏」

    PrintWriter and BufferedWriter区别和使用「建议收藏」区别:BufferedWriter:将文本写入字符输出流,缓冲各个字符从而提供单个字符,数组和字符串的高效写入。通过write()方法可以将获取到的字符输出,然后通过newLine()进行换行操作。并且BufferedWriter只能对字符流进行操作。PrintWriter:相对于BufferedWriter的好处在于,如果PrintWriter开启了自动刷新,那么当PrintWri…

    2022年6月5日
    36
  • Win10安装Tomcat服务器与配置环境变量[通俗易懂]

    Win10安装Tomcat服务器与配置环境变量[通俗易懂]文章目录下载安装JDK下载Tomcat压缩包解压Tomcat压缩包Tomcat目录结构启动Tomcat环境变量配置Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。本文主要讲述Windows环境Tomcat服务器安装与环境变量配置下载安装JDK要想安装Tomcat服务…

    2022年6月3日
    31
  • 数据库索引的作用和长处缺点

    数据库索引的作用和长处缺点

    2021年11月29日
    49
  • mac配置IDEA热部署[通俗易懂]

    mac配置IDEA热部署[通俗易懂]1、pom文件中添加依赖<!–开启热部署–><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional&gt

    2022年6月8日
    175
  • pycharm报错traceback most_cm3d2报错error

    pycharm报错traceback most_cm3d2报错errorPycharm报错Pythonerror:PermissionError:[Errno13]Permissiondenied:在pycharm中读取csv文件时,出现错误PermissionError:[Errno13]Permissiondenied:。看了大部分博客说是因为文件权限问题,或者文件被手动打开,这两个方法都试了试后,无效。解决问题的方法:配置Python编译器时将ScriptPath的路径写到脚本的具体路径,要包含脚本的文件名。如下图。…

    2022年8月26日
    5
  • Angular面试题_angular面试

    Angular面试题_angular面试一、ng-show/ng-hide与ng-if的区别?第一点区别是,ng-if在后面表达式为true的时候才创建这个dom节点,ng-show是初始时就创建了,用display:block和display:none来控制显示和不显示。第二点区别是,ng-if会(隐式地)产生新作用域,ng-switch、ng-include等会…

    2022年8月31日
    4

发表回复

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

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