facebook twitter hatena line email

「Unity/UIDropdown」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==Dropdown追加方法== #UI/Dropdownを追加 #Dropdownを選択肢Inspectorのoptionsから選択項目を編集する ==Dropdown選択値取得サンプル== <pre> us...」)
 
(Dropdown選択値取得サンプル)
行34: 行34:
 
===選択項目の高さが狭い場合===
 
===選択項目の高さが狭い場合===
 
templates/viewportのscaleのyを1から3とかにする
 
templates/viewportのscaleのyを1から3とかにする
 +
 +
==Dropdownの選択項目追加==
 +
<pre>
 +
using UnityEngine;
 +
using UnityEngine.UI;
 +
using System.Collections.Generic;
 +
public class Example : MonoBehaviour
 +
{
 +
    //Use these for adding options to the Dropdown List
 +
    Dropdown.OptionData m_NewData, m_NewData2;
 +
    //The list of messages for the Dropdown
 +
    List<Dropdown.OptionData> m_Messages = new List<Dropdown.OptionData>();
 +
    //This is the Dropdown
 +
    Dropdown m_Dropdown;
 +
    string m_MyString;
 +
    int m_Index;
 +
    void Start()
 +
    {
 +
        //Fetch the Dropdown GameObject the script is attached to
 +
        m_Dropdown = GetComponent<Dropdown>();
 +
        //Clear the old options of the Dropdown menu
 +
        m_Dropdown.ClearOptions();
 +
 +
        //Create a new option for the Dropdown menu which reads "Option 1" and add to messages List
 +
        m_NewData = new Dropdown.OptionData();
 +
        m_NewData.text = "Option 1";
 +
        m_Messages.Add(m_NewData);
 +
 +
        //Create a new option for the Dropdown menu which reads "Option 2" and add to messages List
 +
        m_NewData2 = new Dropdown.OptionData();
 +
        m_NewData2.text = "Option 2";
 +
        m_Messages.Add(m_NewData2);
 +
 +
        //Take each entry in the message List
 +
        foreach (Dropdown.OptionData message in m_Messages)
 +
        {
 +
            //Add each entry to the Dropdown
 +
            m_Dropdown.options.Add(message);
 +
            //Make the index equal to the total number of entries
 +
            m_Index = m_Messages.Count - 1;
 +
        }
 +
    }
 +
}
 +
</pre>
 +
参考:https://docs.unity3d.com/ja/current/ScriptReference/UI.Dropdown-options.html

2018年9月10日 (月) 10:47時点における版

Dropdown追加方法

  1. UI/Dropdownを追加
  2. Dropdownを選択肢Inspectorのoptionsから選択項目を編集する

Dropdown選択値取得サンプル

using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
    Dropdown m_Dropdown;
    public Text m_Text;
    void Start()
    {
        //Fetch the Dropdown GameObject
        m_Dropdown = GetComponent<Dropdown>();
        //Add listener for when the value of the Dropdown changes, to take action
        m_Dropdown.onValueChanged.AddListener(delegate {
                DropdownValueChanged(m_Dropdown);
            });
        //Initialise the Text to say the first value of the Dropdown
        m_Text.text = "First Value : " + m_Dropdown.value;
    }
    //Ouput the new value of the Dropdown into Text
    void DropdownValueChanged(Dropdown change)
    {
        m_Text.text =  "New Value : " + change.value;
    }
}

参考:https://docs.unity3d.com/ja/current/ScriptReference/UI.Dropdown-onValueChanged.html

選択項目の高さが狭い場合

templates/viewportのscaleのyを1から3とかにする

Dropdownの選択項目追加

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class Example : MonoBehaviour
{
    //Use these for adding options to the Dropdown List
    Dropdown.OptionData m_NewData, m_NewData2;
    //The list of messages for the Dropdown
    List<Dropdown.OptionData> m_Messages = new List<Dropdown.OptionData>();
    //This is the Dropdown
    Dropdown m_Dropdown;
    string m_MyString;
    int m_Index;
    void Start()
    {
        //Fetch the Dropdown GameObject the script is attached to
        m_Dropdown = GetComponent<Dropdown>();
        //Clear the old options of the Dropdown menu
        m_Dropdown.ClearOptions();

        //Create a new option for the Dropdown menu which reads "Option 1" and add to messages List
        m_NewData = new Dropdown.OptionData();
        m_NewData.text = "Option 1";
        m_Messages.Add(m_NewData);

        //Create a new option for the Dropdown menu which reads "Option 2" and add to messages List
        m_NewData2 = new Dropdown.OptionData();
        m_NewData2.text = "Option 2";
        m_Messages.Add(m_NewData2);

        //Take each entry in the message List
        foreach (Dropdown.OptionData message in m_Messages)
        {
            //Add each entry to the Dropdown
            m_Dropdown.options.Add(message);
            //Make the index equal to the total number of entries
            m_Index = m_Messages.Count - 1;
        }
    }
}

参考:https://docs.unity3d.com/ja/current/ScriptReference/UI.Dropdown-options.html