💧

[Unity] ドロップダウンリストを追加する UIElements編

2022/09/03に公開

作り方

Dropdown GUI SampleDropdown GUI Sample

private System.Collections.Generic.Dictionary<int, string> m_DisplayValue;
private System.Collections.Generic.List<int>               m_PopupValues;

//----

m_PopupValues = new System.Collections.Generic.List<int>
{
    1,
    2,
    3,
};

m_DisplayValue = new System.Collections.Generic.Dictionary<int, string>
{
    { 1, "Hoge 1" },
    { 2, "Hoge 2" },
    { 3, "Hoge 3" },
};

//----

new UnityEditor.UIElements.PopupField<int>(
    label: "Popup",
    choices: m_PopupValues,
    defaultIndex: 0,
    formatSelectedValueCallback: value => m_DisplayValue[value],
    formatListItemCallback: value => m_DisplayValue[value]
);

説明

ドロップダウンリストを、UIElementsで作ることができます。

実際に使用する値と、表示するための文字列とで分けて作ることができます。

階段状に、段々にもできます。

Step GUIStep GUI

private System.Collections.Generic.Dictionary<int, string> m_DisplayValue;
private System.Collections.Generic.List<int>               m_PopupValues;

//----

m_PopupValues = new System.Collections.Generic.List<int>
{
    1,
    2,
    3,

    4,
    5,
    6
};

m_DisplayValue = new System.Collections.Generic.Dictionary<int, string>
{
    { 1, "Hoge 1" },
    { 2, "Hoge 2" },
    { 3, "Hoge 3" },

    { 4, "Piya/Hoge 1" },
    { 5, "Piya/Hoge 2" },
    { 6, "Piya/Hoge 3" },
};

//----

new UnityEditor.UIElements.PopupField<int>(
    label: "Popup",
    choices: m_PopupValues,
    defaultIndex: 0,
    formatSelectedValueCallback: value => m_DisplayValue[value],
    formatListItemCallback: value => m_DisplayValue[value]
);

その他、従来のIMGUIでも描画可能です

GitHubで編集を提案

Discussion