1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UnityにおけるDropdownリストの使い方

Last updated at Posted at 2020-03-09

UnityのUIの使い方の備忘録

UnityにおいてDropdownリストの作成を行い、リストにおいて選ばれているものをログにおいて表示を行います。参照URL

方法について

まず、UIからDropdownを追加します。Dropdownリストの中のオプションについてはInspectorの下の方に存在している。image.png
このオプションに追加及び削除を行うことが出来ます。また、選択されているものにはvalueという値がつけられています。選択されているオプションの値が画像上部のvalueの値が変更されます。

変更された際のコードについて

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class Decision_ELEVATOR : MonoBehaviour {

    [SerializeField] private Dropdown choiceMethod;
    [SerializeField] private Dropdown choiceMovie;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        

    }

    public void OnClick()
    {
        Debug.Log("choiceMethod : " + choiceMethod.value + "choiceMovie : " + choiceMovie.value);

        if (choiceMovie.value == 0)
        {
            Debug.Log("choiceMovie : 0");
        }
        //DropdownのValueが1のとき(青が選択されているとき)
        else if (choiceMovie.value == 1)
        {
            Debug.Log("choiceMovie : 1");
        }
        //DropdownのValueが2のとき(黄が選択されているとき)
        else if (choiceMovie.value == 2)
        {
            Debug.Log("choiceMovie : 2");
        }
        if (choiceMethod.value == 0)
        {
            Debug.Log("choiceMethod : 0");
        }
        //DropdownのValueが1のとき(青が選択されているとき)
        else if (choiceMethod.value == 1)
        {
            Debug.Log("choiceMethod : 1");
        }

    }
}

ボタンの作成を行い、このスクリプトを追加します。その後作成してあるDropdownをInspectorにおいて追加を行います。

これによって、ボタンを押すことでログにどのオプションが選ばれているかということを表示することが出来ます。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?