Odin Inspector 系列教程 — Value Dropdown Attribute

Odin Inspector 系列教程 — Value Dropdown AttributeValueDropdownAttribute特性用于任何属性,并使用可配置选项创建下拉列表。使用此选项可为用户提供一组特定的选项供您选择。也就是创建一些特殊的下拉条这个里面的属性就有点多了,达到了16个!!!下面笔者逐个讲解MemberName,也是唯一一个有参构造函数需要的属性,有两种形式的Drop下拉条,一种是直接数值的,另一种是Key-Value形式的…

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

Value Dropdown Attribute特性用于任何属性,并使用可配置选项创建下拉列表。使用此选项可为用户提供一组特定的选项供您选择。
也就是创建一些特殊的下拉条

这个里面的属性就有点多了,达到了16个!!!
下面笔者逐个讲解

MemberName,也是唯一一个有参构造函数需要的属性,有两种形式的Drop下拉条,一种是直接数值的,另一种是Key-Value形式的
7643202-5133392a301e7482.gif

    /*【MemberName】*/
    [PropertySpace(40, 0)]
    [ValueDropdown("TextureSizes")]
    public int SomeSize1;
    private static int[] TextureSizes = new int[] { 32, 64, 128, 256, 512, 1024, 2048, 4096 };

    [ValueDropdown("FriendlyTextureSizes")]
    public int SomeSize2;
    private static IEnumerable FriendlyTextureSizes = new ValueDropdownList<int>()
    {
      { "Small", 256 },
      { "Medium", 512 },
      { "Large", 1024 },
    };
【SortDropdownItems】默认为false 开启后为下拉列表为根据Key升序排序
7643202-0d2e87e0c595e834.gif

    /*【SortDropdownItems】默认为false 开启后为下拉列表为根据Key升序排序*/
    [PropertySpace(40, 0)]
    [ValueDropdown("SortList1")]
    public int SomeSize3;
    private IEnumerable SortList1 = new ValueDropdownList<int>()
    {
      { "Small", 256 },
      { "Medium", 512 },
      { "Large", 1024 },
       { "A", 128 },
    };
    [PropertySpace(0, 40)]
    [ValueDropdown("SortList2", SortDropdownItems = true)]
    public int SomeSize4;
    private List<ValueDropdownItem<int>> SortList2 = new ValueDropdownList<int>()
    {
      { "Small", 256 },
      { "Medium", 512 },
      { "Large", 1024 },
      { "A", 128 },
    };
【DropdownTitle】给下来条提供一个标题
7643202-9a139d5edd540247.gif

    [PropertySpace(0, 40)]
    [ValueDropdown("TextureSizes", DropdownTitle = "下拉条标题")]
    public int SomeSize5;
【DropdownHeight】下拉条高度
7643202-8d497fb864acf9ad.gif

    /*【DropdownHeight】下拉条高度*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TextureSizes", DropdownHeight = 80)]
    public int SomeSize6;
【DropdownWidth】下拉条的宽度
7643202-94faa27ba6d8709c.gif

    /*【DropdownWidth】下拉条的宽度*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TextureSizes", DropdownWidth = 100)]
    public int SomeSize7;
【FlattenTreeView】是否使用平铺的树形视图
7643202-b8448162f2aff711.gif

    /*【FlattenTreeView】是否使用平铺的树形视图*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", FlattenTreeView = true)]//默认为false,如果设置为true则禁用树形结构使用平铺模式
    public int SomeSize8;
【DoubleClickToConfirm】需要双击才能确地选中的内容
7643202-0522a8c8db841a5e.gif

    /*【DoubleClickToConfirm】需要双击才能确地选中的内容*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", DoubleClickToConfirm = true)]//需要双击才能选中
    public int SomeSize9;
【HideChildProperties】是否隐藏此类型所含有的属性信息
7643202-f9351ae2fc862355.png

    /*【HideChildProperties】是否隐藏此类型所含有的属性信息*/
    [ValueDropdown("RangVector3", HideChildProperties = true)]//
    public Vector3 vector3HideChildProperties;
    [PropertySpace(0, 40)]
    [ValueDropdown("RangVector3", HideChildProperties = false)]//
    public Vector3 vector3ShowChildProperties;

    public IEnumerable<Vector3>  RangVector3()
    {
       return Enumerable.Range(0, 10).Select(i => new Vector3(i, i, i));
    }
【AppendNextDrawer】下拉条变成一个小的选择器,代替原有的宽型下拉条
7643202-132b3d1fe697a04a.gif

    /*【AppendNextDrawer】下拉条变成一个小的选择器,代替原有的宽型下拉条*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", AppendNextDrawer = true)]//
    public int SomeSize11;
【DisableGUIInAppendedDrawer】配合AppendNextDrawer使用,显示的数值为灰度状态,达到不可更改数值的目的
7643202-55796b6570049c75.gif

    /*【DisableGUIInAppendedDrawer】配合AppendNextDrawer使用,显示的数值为灰度状态,达到不可更改数值的目的*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", AppendNextDrawer = true, DisableGUIInAppendedDrawer = true)]//
    public int SomeSize12;
ExpandAllMenuItems】下拉条里面的条目是否全部展开
7643202-01569c004ab367f8.gif

    /*【ExpandAllMenuItems】下拉条里面的条目是否全部展开*/
    [ValueDropdown("TreeViewOfInts" , ExpandAllMenuItems = false)]//
    public int SomeSize13;
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", ExpandAllMenuItems =true )]//
    public int SomeSize14;
【IsUniqueList】在添加的列表Item前面添加勾选框,可以一次性勾选多个Item并添加
7643202-c01d00466668a841.gif

    /*【IsUniqueList】在添加的列表Item前面添加勾选框,可以一次性勾选多个Item并添加*/
    [ValueDropdown("GetAllSceneObjects", IsUniqueList = false)]
    public List<GameObject> UniqueGameobjectList0;
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", IsUniqueList = true)]
    public List<GameObject> UniqueGameobjectList1;
【ExcludeExistingValuesInList】添加列中不会显示已经选中的Item
7643202-a494380d06e61060.gif

    /*【ExcludeExistingValuesInList】添加列中不会显示已经选中的Item*/
    [ValueDropdown("GetAllSceneObjects")]
    public List<GameObject> UniqueGameobjectList2;
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", ExcludeExistingValuesInList = true)]
    public List<GameObject> UniqueGameobjectList3;
【DisableListAddButtonBehaviour】禁用下拉列表,以弹窗的形式弹出
7643202-ce51f8559caf0457.gif

    /*【DisableListAddButtonBehaviour】禁用下拉列表,以弹窗的形式弹出*/
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", DisableListAddButtonBehaviour = true, IsUniqueList = true)]
    public List<GameObject> UniqueGameobjectList4;
【DrawDropdownForListElements】已经添加的Item不会再出现Item下拉表
7643202-cc3b46a31df970df.gif

    /*【DrawDropdownForListElements】已经添加的Item不会再出现Item下拉表*/
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", DrawDropdownForListElements = false)]
    public List<GameObject> UniqueGameobjectList5;
``

##### 【NumberOfItemsBeforeEnablingSearch】查过指定数量的Item则出现搜索框。默认是10。

![](https://upload-images.jianshu.io/upload_images/7643202-87b2e38d89c171cc.gif?imageMogr2/auto-orient/strip)

```cs
    /*【NumberOfItemsBeforeEnablingSearch】查过指定数量的Item则出现搜索框。默认是10。*/
    [ValueDropdown("GetAllSceneObjects", NumberOfItemsBeforeEnablingSearch =200)]
    public List<GameObject> UniqueGameobjectList6;
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", NumberOfItemsBeforeEnablingSearch = 20)]
    public List<GameObject> UniqueGameobjectList7;

示例完整代码(含有一些其他辅助性功能代码)

using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;


public class ValueDropdownAttributeExample : MonoBehaviour
{

    /*【MemberName】*/
    [PropertySpace(40, 0)]
    [ValueDropdown("TextureSizes")]
    public int SomeSize1;
    private static int[] TextureSizes = new int[] { 32, 64, 128, 256, 512, 1024, 2048, 4096 };

    [ValueDropdown("FriendlyTextureSizes")]
    public int SomeSize2;
    private static IEnumerable FriendlyTextureSizes = new ValueDropdownList<int>()
    {
      { "Small", 256 },
      { "Medium", 512 },
      { "Large", 1024 },
    };

    /*【SortDropdownItems】默认为false 开启后为下拉列表为根据Key升序排序*/
    [PropertySpace(40, 0)]
    [ValueDropdown("SortList1")]
    public int SomeSize3;
    private IEnumerable SortList1 = new ValueDropdownList<int>()
    {
      { "Small", 256 },
      { "Medium", 512 },
      { "Large", 1024 },
       { "A", 128 },
    };
    [PropertySpace(0, 40)]
    [ValueDropdown("SortList2", SortDropdownItems = true)]
    public int SomeSize4;
    private List<ValueDropdownItem<int>> SortList2 = new ValueDropdownList<int>()
    {
      { "Small", 256 },
      { "Medium", 512 },
      { "Large", 1024 },
      { "A", 128 },
    };

    /*【DropdownTitle】给下来条提供一个标题*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TextureSizes", DropdownTitle = "下拉条标题")]
    public int SomeSize5;

    /*【DropdownHeight】下拉条高度*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TextureSizes", DropdownHeight = 80)]
    public int SomeSize6;

    /*【DropdownWidth】下拉条的宽度*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TextureSizes", DropdownWidth = 100)]
    public int SomeSize7;

    /*【FlattenTreeView】是否使用平铺的树形视图*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", FlattenTreeView = true)]//默认为false,如果设置为true则禁用树形结构使用平铺模式
    public int SomeSize8;

    /*【DoubleClickToConfirm】需要双击才能确地选中的内容*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", DoubleClickToConfirm = true)]//需要双击才能选中
    public int SomeSize9;

    /*【HideChildProperties】是否隐藏此类型所含有的属性信息*/
    [ValueDropdown("RangVector3", HideChildProperties = true)]//
    public Vector3 vector3HideChildProperties;
    [PropertySpace(0, 40)]
    [ValueDropdown("RangVector3", HideChildProperties = false)]//
    public Vector3 vector3ShowChildProperties;

    public IEnumerable<Vector3>  RangVector3()
    {
       return Enumerable.Range(0, 10).Select(i => new Vector3(i, i, i));
    }


    /*【AppendNextDrawer】下拉条变成一个小的选择器,代替原有的宽型下拉条*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", AppendNextDrawer = true)]//
    public int SomeSize11;

    /*【DisableGUIInAppendedDrawer】配合AppendNextDrawer使用,显示的数值为灰度状态,达到不可更改数值的目的*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", AppendNextDrawer = true, DisableGUIInAppendedDrawer = true)]//
    public int SomeSize12;

    /*【ExpandAllMenuItems】下拉条里面的条目是否全部展开*/
    [ValueDropdown("TreeViewOfInts" , ExpandAllMenuItems = false)]//
    public int SomeSize13;
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", ExpandAllMenuItems =true )]//
    public int SomeSize14;

    /*【IsUniqueList】在添加的列表Item前面添加勾选框,可以一次性勾选多个Item并添加*/
    [ValueDropdown("GetAllSceneObjects", IsUniqueList = false)]
    public List<GameObject> UniqueGameobjectList0;
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", IsUniqueList = true)]
    public List<GameObject> UniqueGameobjectList1;

    /*【ExcludeExistingValuesInList】添加列中不会显示已经选中的Item*/
    [ValueDropdown("GetAllSceneObjects")]
    public List<GameObject> UniqueGameobjectList2;
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", ExcludeExistingValuesInList = true)]
    public List<GameObject> UniqueGameobjectList3;

    /*【DisableListAddButtonBehaviour】禁用下拉列表,以弹窗的形式弹出*/
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", DisableListAddButtonBehaviour = true, IsUniqueList = true)]
    public List<GameObject> UniqueGameobjectList4;

    /*【DrawDropdownForListElements】已经添加的Item不会再出现Item下拉表*/
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", DrawDropdownForListElements = false)]
    public List<GameObject> UniqueGameobjectList5;

    /*【NumberOfItemsBeforeEnablingSearch】查过指定数量的Item则出现搜索框。默认是10。*/
    [ValueDropdown("GetAllSceneObjects", NumberOfItemsBeforeEnablingSearch =200)]
    public List<GameObject> UniqueGameobjectList6;
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", NumberOfItemsBeforeEnablingSearch = 20)]
    public List<GameObject> UniqueGameobjectList7;


    [ValueDropdown("GetListOfMonoBehaviours", AppendNextDrawer = true, HideChildProperties = false)]
    public MonoBehaviour SomeMonoBehaviour;
    private IEnumerable<MonoBehaviour> GetListOfMonoBehaviours()
    {
        return GameObject.FindObjectsOfType<MonoBehaviour>();
    }

    [ValueDropdown("KeyCodes")]
    public KeyCode FilteredEnum;
    private static IEnumerable<KeyCode> KeyCodes = Enumerable.Range((int)KeyCode.Alpha0, 10).Cast<KeyCode>();


    [ValueDropdown("TreeViewOfInts", ExpandAllMenuItems = true)]
    public List<int> IntTreeview = new List<int>() { 1, 2, 7 };
    /// <summary>
    /// 以“/”符号作为类别分隔符
    /// </summary>
    private IEnumerable TreeViewOfInts = new ValueDropdownList<int>()
{
    { "Node 1/Node 1.1", 1 },
    { "Node 1/Node 1.2", 2 },
    { "Node 2/Node 2.1", 3 },
    { "Node 3/Node 3.1", 4 },
    { "Node 3/Node 3.2", 5 },
    { "Node 1/Node 3.1/Node 3.1.1", 6 },
    { "Node 1/Node 3.1/Node 3.1.2", 7 },
    { "Node 1", -1 },
    { "Node 2", -2 },
    { "Node 3", -3 },
    { "Node 4", -4 },
};

    /// <summary>
    /// IsUniqueList为true 每个Item上面有一个勾选框
    /// </summary>
    [ValueDropdown("GetAllSceneObjects", IsUniqueList = true, HideChildProperties = false)]
    public List<GameObject> UniqueGameobjectList;
    private static IEnumerable GetAllSceneObjects()
    {
        Func<Transform, string> getPath = null;
        getPath = x => (x ? getPath(x.parent) + "/" + x.gameObject.name : "");//三元运算符 其中X为Transform
        return GameObject.FindObjectsOfType<GameObject>().Select(x => new ValueDropdownItem(getPath(x.transform), x));
    }

    /// <summary>
    /// ExcludeExistingValuesInList 为 ture则选中的item不在出现在等待选择的列下拉表中
    /// DrawDropdownForListElements 为 true  每个item都有一个下拉列表
    /// </summary>
    [ValueDropdown("GetAllSceneObjects", IsUniqueList = false, DropdownTitle = "Select Scene Object", DrawDropdownForListElements = false, ExcludeExistingValuesInList = true)]
    public List<GameObject> UniqueGameobjectListMode2;


    private static IEnumerable GetAllScriptableObjects()
    {
        return UnityEditor.AssetDatabase.FindAssets("t:ScriptableObject")
            .Select(x => UnityEditor.AssetDatabase.GUIDToAssetPath(x))
            .Select(x => new ValueDropdownItem(x, UnityEditor.AssetDatabase.LoadAssetAtPath<ScriptableObject>(x)));
    }

    private static IEnumerable GetAllSirenixAssets()
    {
        var root = "Assets/Plugins/Sirenix/";

        return UnityEditor.AssetDatabase.GetAllAssetPaths()
            .Where(x => x.StartsWith(root))
            .Select(x => x.Substring(root.Length))
            .Select(x => new ValueDropdownItem(x, UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(root + x)));
    }
}

更多教程内容详见:革命性Unity 编辑器扩展工具 — Odin Inspector 系列教程

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

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

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


相关推荐

  • Pytest(6)重复运行用例pytest-repeat「建议收藏」

    Pytest(6)重复运行用例pytest-repeat「建议收藏」前言平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,

    2022年7月31日
    3
  • kali的Ettercap(0.8.3.1)GUI界面中文化[通俗易懂]

    kali的Ettercap(0.8.3.1)GUI界面中文化[通俗易懂]前言:最近在学kali,学到了dns劫持,上的课老师也是讲了一部分功能,我在网上想找一下别的功能,却发现这个Ettercap居然是20年新版,并没有什么详细信息,我就心血来潮写这篇笔记,让自己以后在复习的时候可以更快的回顾,发出来也让大家一起进步。 启动方法 kali的终端中输入ettercap-G 启动界面 GUI界面 中间人攻击选项 其他选项 Targets Currenttargets Selecttargets

    2022年6月28日
    36
  • Idea导入jar包的两种方法

    Idea导入jar包的两种方法今天用IDEA,需要导入一个Jar包,因为以前都是用eclipse的,所以对这个idea还不怎么上手,连打个Jar包都是谷歌了一下。但是发现网上谷歌到的做法一般都是去File–&gt;ProjectStructure中去设置,有没有如同eclipse一样简便的右键添加方法呢。然后自己摸索了一下,找到了。以阿里大于的依赖包为例子,因为正好是我遇到要导入的Jar包。先说下第一种方法吧。也就是Fi…

    2022年6月22日
    54
  • C#中FileStream与StreamReader/StreamWriter区别

    C#中FileStream与StreamReader/StreamWriter区别FileStreamFileStream类不是静态类,表示在磁盘或网络路径上指向文件的流。这个类提供了在文件中读写字节的方法,但经常使用StreamReader或StreamWriter执行这些功能。这是因为FileStream类操作的是字节和字节数组,而Stream类操作的是字符数据。使用时需要创建对象,FileStream类既可以对文本文件进行读也可以对多媒体文件进行写,以字节数组的形式进…

    2022年7月21日
    16
  • Java并发编程:线程池的使用

    Java并发编程:线程池的使用

    2022年2月21日
    36
  • ubuntu20.04主题美化_ubuntu优化工具

    ubuntu20.04主题美化_ubuntu优化工具用了这么长时间ubuntu了,也该让自己的老婆漂亮点了。对吧。 于是搜之,找到资料记录一下: 资料链接:http://www.ubuntuhome.com/ubuntu-10-04-install-themes.html 主要步骤:添加ppa源,然后下载好看的主题,具体见资料链接。 记录一下添加源:首先打开终端并依次输入:sudoadd-apt-r…

    2022年9月23日
    0

发表回复

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

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