Odin Inspector 系列教程 --- Value Dropdown Attribute

2024-02-09 22:08

本文主要是介绍Odin Inspector 系列教程 --- Value Dropdown Attribute,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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为Transformreturn 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 系列教程

这篇关于Odin Inspector 系列教程 --- Value Dropdown Attribute的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/695395

相关文章

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Java Scanner类解析与实战教程

《JavaScanner类解析与实战教程》JavaScanner类(java.util包)是文本输入解析工具,支持基本类型和字符串读取,基于Readable接口与正则分隔符实现,适用于控制台、文件输... 目录一、核心设计与工作原理1.底层依赖2.解析机制A.核心逻辑基于分隔符(delimiter)和模式匹

spring AMQP代码生成rabbitmq的exchange and queue教程

《springAMQP代码生成rabbitmq的exchangeandqueue教程》使用SpringAMQP代码直接创建RabbitMQexchange和queue,并确保绑定关系自动成立,简... 目录spring AMQP代码生成rabbitmq的exchange and 编程queue执行结果总结s

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

shell脚本批量导出redis key-value方式

《shell脚本批量导出rediskey-value方式》为避免keys全量扫描导致Redis卡顿,可先通过dump.rdb备份文件在本地恢复,再使用scan命令渐进导出key-value,通过CN... 目录1 背景2 详细步骤2.1 本地docker启动Redis2.2 shell批量导出脚本3 附录总

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

2025版mysql8.0.41 winx64 手动安装详细教程

《2025版mysql8.0.41winx64手动安装详细教程》本文指导Windows系统下MySQL安装配置,包含解压、设置环境变量、my.ini配置、初始化密码获取、服务安装与手动启动等步骤,... 目录一、下载安装包二、配置环境变量三、安装配置四、启动 mysql 服务,修改密码一、下载安装包安装地

电脑提示d3dx11_43.dll缺失怎么办? DLL文件丢失的多种修复教程

《电脑提示d3dx11_43.dll缺失怎么办?DLL文件丢失的多种修复教程》在使用电脑玩游戏或运行某些图形处理软件时,有时会遇到系统提示“d3dx11_43.dll缺失”的错误,下面我们就来分享超... 在计算机使用过程中,我们可能会遇到一些错误提示,其中之一就是缺失某个dll文件。其中,d3dx11_4

Linux下在线安装启动VNC教程

《Linux下在线安装启动VNC教程》本文指导在CentOS7上在线安装VNC,包含安装、配置密码、启动/停止、清理重启步骤及注意事项,强调需安装VNC桌面以避免黑屏,并解决端口冲突和目录权限问题... 目录描述安装VNC安装 VNC 桌面可能遇到的问题总结描js述linux中的VNC就类似于Window

Go语言编译环境设置教程

《Go语言编译环境设置教程》Go语言支持高并发(goroutine)、自动垃圾回收,编译为跨平台二进制文件,云原生兼容且社区活跃,开发便捷,内置测试与vet工具辅助检测错误,依赖模块化管理,提升开发效... 目录Go语言优势下载 Go  配置编译环境配置 GOPROXYIDE 设置(VS Code)一些基本