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

Value Dropdown Attribute特性用于任何屬性,并使用可配置選項創建下拉列表。使用此選項可為用戶提供一組特定的選項供您選擇。
也就是創建一些特殊的下拉條

這個里面的屬性就有點多了,達到了16個!!!
下面筆者逐個講解

MemberName,也是唯一一個有參構造函數需要的屬性,有兩種形式的Drop下拉條,一種是直接數值的,另一種是Key-Value形式的
    /*【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升序排序
    /*【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】下拉條高度
    /*【DropdownHeight】下拉條高度*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TextureSizes", DropdownHeight = 80)]
    public int SomeSize6;
【DropdownWidth】下拉條的寬度
    /*【DropdownWidth】下拉條的寬度*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TextureSizes", DropdownWidth = 100)]
    public int SomeSize7;
【FlattenTreeView】是否使用平鋪的樹形視圖
    /*【FlattenTreeView】是否使用平鋪的樹形視圖*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", FlattenTreeView = true)]//默認為false,如果設置為true則禁用樹形結構使用平鋪模式
    public int SomeSize8;
【DoubleClickToConfirm】需要雙擊才能確地選中的內容
    /*【DoubleClickToConfirm】需要雙擊才能確地選中的內容*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", DoubleClickToConfirm = true)]//需要雙擊才能選中
    public int SomeSize9;
【HideChildProperties】是否隱藏此類型所含有的屬性信息
    /*【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】下拉條變成一個小的選擇器,代替原有的寬型下拉條
    /*【AppendNextDrawer】下拉條變成一個小的選擇器,代替原有的寬型下拉條*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", AppendNextDrawer = true)]//
    public int SomeSize11;
【DisableGUIInAppendedDrawer】配合AppendNextDrawer使用,顯示的數值為灰度狀態,達到不可更改數值的目的
    /*【DisableGUIInAppendedDrawer】配合AppendNextDrawer使用,顯示的數值為灰度狀態,達到不可更改數值的目的*/
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", AppendNextDrawer = true, DisableGUIInAppendedDrawer = true)]//
    public int SomeSize12;
【ExpandAllMenuItems】下拉條里面的條目是否全部展開
    /*【ExpandAllMenuItems】下拉條里面的條目是否全部展開*/
    [ValueDropdown("TreeViewOfInts" , ExpandAllMenuItems = false)]//
    public int SomeSize13;
    [PropertySpace(0, 40)]
    [ValueDropdown("TreeViewOfInts", ExpandAllMenuItems =true )]//
    public int SomeSize14;
【IsUniqueList】在添加的列表Item前面添加勾選框,可以一次性勾選多個Item并添加
    /*【IsUniqueList】在添加的列表Item前面添加勾選框,可以一次性勾選多個Item并添加*/
    [ValueDropdown("GetAllSceneObjects", IsUniqueList = false)]
    public List<GameObject> UniqueGameobjectList0;
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", IsUniqueList = true)]
    public List<GameObject> UniqueGameobjectList1;
【ExcludeExistingValuesInList】添加列中不會顯示已經選中的Item
    /*【ExcludeExistingValuesInList】添加列中不會顯示已經選中的Item*/
    [ValueDropdown("GetAllSceneObjects")]
    public List<GameObject> UniqueGameobjectList2;
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", ExcludeExistingValuesInList = true)]
    public List<GameObject> UniqueGameobjectList3;
【DisableListAddButtonBehaviour】禁用下拉列表,以彈窗的形式彈出
    /*【DisableListAddButtonBehaviour】禁用下拉列表,以彈窗的形式彈出*/
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", DisableListAddButtonBehaviour = true, IsUniqueList = true)]
    public List<GameObject> UniqueGameobjectList4;
【DrawDropdownForListElements】已經添加的Item不會再出現Item下拉表
    /*【DrawDropdownForListElements】已經添加的Item不會再出現Item下拉表*/
    [PropertySpace(0, 40)]
    [ValueDropdown("GetAllSceneObjects", DrawDropdownForListElements = false)]
    public List<GameObject> UniqueGameobjectList5;
【NumberOfItemsBeforeEnablingSearch】查過指定數量的Item則出現搜索框。默認是10。
    /*【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 系列教程

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容