UnityのDropDown(ドロップダウン)の要素をスクリプトで変更出来るようになりました
UnityのDropDown(ドロップダウン)の要素をスクリプトで変更出来るようになりました。
いやー、これマジで良かった。生年月日の入力画面を作ってたんですが、さすがに管理画面(て言うのかな?)からだと面倒なんで、コードで変更出来ないかと探してたらありましたよ。
【Unity】【C#】uGUI ドロップダウンの要素をコードで設定と取得、外観のカスタマイズなど - ヽ|∵|ゝ(Fantom) の 開発blog?
こちらの記事を参考にさせていただいたんですが、すぐに出来て良かったです。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using System;
using UnityEngine.SceneManagement;
public class DropDownManager2 : MonoBehaviour {
public Dropdown dropDownYear;
public Dropdown dropDownMonth;
public Dropdown dropDownDay;
int year;
int month;
int day;
// Use this for initialization
void Start () {
if (dropDownYear) {
dropDownYear.ClearOptions(); //現在の要素をクリアする
List<string> list = new List<string>();
for (int i = 1919; i < 2018; i++) {
if (i == 1919) {
list.Add ("年");
} else {
list.Add (i.ToString () + "年");
}
}
dropDownYear.AddOptions(list); //新しく要素のリストを設定する
dropDownYear.value = 0; //デフォルトを設定(0~n-1)
}
if (dropDownMonth) {
dropDownMonth.ClearOptions(); //現在の要素をクリアする
List<string> list = new List<string>();
for (int i = 0; i <= 12; i++) {
if (i == 0) {
list.Add ("月");
} else {
list.Add (i.ToString () + "月");
}
}
dropDownMonth.AddOptions(list); //新しく要素のリストを設定する
dropDownMonth.value = 0; //デフォルトを設定(0~n-1)
}
if (dropDownDay) {
dropDownDay.ClearOptions(); //現在の要素をクリアする
List<string> list = new List<string>();
for (int i = 0; i <= 31; i++) {
if (i == 0) {
list.Add ("日");
} else {
list.Add (i.ToString () + "日");
}
}
dropDownDay.AddOptions(list); //新しく要素のリストを設定する
dropDownDay.value = 0; //デフォルトを設定(0~n-1)
}
}
// Update is called once per frame
void Update () {
}
}
こんな感じに書きますと、、
ば、バッチリ出来とる。。
やっぱ数が多いときはスクリプトから変更するのがベターですよね。
ちゃんと出来て良かったです。