Dropdown은 UI의 기본기로써 여러 목록의 선택지를 쉽게 골라주게 하기위해 Unity에서 기본으로 제공하고 있다.

아래 소스코드는 Dropdown의 목록 생성 및 선택된 value에 대한 출력을 다룬다.


[참고] Dropdown 리스트 중 하나를 선택하면 dropdown.value의 값이 선택지에 대한 배열의 값으로 생성이 된다.

가령 2번째 리스트를 선택한 경우 dropdown.value = 1 이 된다. 이를 이용하여 선택지를 알아내고 값을 응용하여 원하는 셀렉기능을 구현하면 된다.

/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <ggkids9211@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Hyunjun Kim.
 * ----------------------------------------------------------------------------
 */

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

public class DropdownExample : MonoBehaviour{
[Header("Dropdown")]
public Dropdown dropdown;

	void Start () {
		SetDropdownOptionsExample();
	}

	void Update () {

	}

private void SetDropdownOptionsExample() // Dropdown 목록 생성
    {
        dropdown.options.Clear();
        for(int i = 1; i < 11; i++) //1부터 10까지
        {
            Dropdown.OptionData option = new Dropdown.OptionData();
            option.text = i.ToString() +"갯수";
            dropdown.options.Add(option);
        }
    }

public void SelectButton() // SelectButton을 누름으로써 값 테스트.
	{
		Debug.Log("Dropdown Value: "+ dropdown.value + ", List Selected: " + (dropdown.value + 1));
	}
}




+ Recent posts