Unity/TMPro/Dropdown
提供: 初心者エンジニアの簡易メモ
TMP_Dropdownのスクリプトで制御
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class SettingScene : MonoBehaviour
{
TMP_Dropdown areaDropdown;
List<string> areas;
string area;
void Start()
{
AreaDropdownInit();
areaDropdown = this.transform.Find("AreaDropdown").GetComponent<TMP_Dropdown>();
areaDropdown.onValueChanged.AddListener(delegate {
AreaDropdownValueChanged(areaDropdown);
});
}
void AreaDropdownInit()
{
areas = new List<string>();
areas.Add("simple");
areas.Add("sabaku");
areas.Add("sushi");
List<TMP_Dropdown.OptionData> optionMessages = new List<TMP_Dropdown.OptionData>();
TMP_Dropdown areaDropdown = this.transform.Find("AreaDropdown").GetComponent<TMP_Dropdown>();
areaDropdown.ClearOptions();
foreach (string area in areas)
{
TMP_Dropdown.OptionData optionData;
optionData = new TMP_Dropdown.OptionData();
optionData.text = area;
optionMessages.Add(optionData);
}
foreach (TMP_Dropdown.OptionData message in optionMessages)
{
areaDropdown.options.Add(message);
}
areaDropdown.value = areas.IndexOf(area);
this.transform.Find("AreaDropdown/Label").GetComponent<TextMeshProUGUI>().text = areas[areaDropdown.value].ToString();
}
void AreaDropdownValueChanged(TMP_Dropdown change)
{
area = areas[change.value];
this.transform.Find("AreaDropdown/Label").GetComponent<TextMeshProUGUI>().text = areas[areaDropdown.value].ToString();
}
}
Dropdownを開いたときに選択した項目を表示する方法
UIのButtonをDropdownに被せて、ButtonをクリックしたときにDropdownを開く以下処理を走らせる。
選択Indexが表示領域の中央より上なら、スクロールしないように。
UniTaskバージョン
using Cysharp.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class TMPDropdownScrollFix : MonoBehaviour
{
[SerializeField]
public TMP_Dropdown dropdown;
[SerializeField]
private Button button;
void Start()
{
button.onClick.AddListener(() => OpenDropdownAsync().Forget());
}
public async UniTaskVoid OpenDropdownAsync()
{
dropdown.Show();
await ScrollToSelectedAsync();
}
async UniTask ScrollToSelectedAsync()
{
// UI生成待ち(超重要)
await UniTask.NextFrame();
await UniTask.NextFrame();
var scrollRect = dropdown.GetComponentInChildren<ScrollRect>();
if (scrollRect == null) return;
int index = dropdown.value;
int totalCount = dropdown.options.Count;
if (totalCount <= 1) return;
// ===== 表示行数を計算 =====
var viewport = scrollRect.viewport.rect.height;
var content = scrollRect.content;
if (content.childCount == 0) return;
float itemHeight = ((RectTransform)content.GetChild(0)).rect.height;
int visibleCount = Mathf.FloorToInt(viewport / itemHeight);
int centerIndex = visibleCount / 2;
// ===== 中央まではスクロールしない =====
if (index <= centerIndex)
{
scrollRect.verticalNormalizedPosition = 1f;
return;
}
// ===== 中央を超えたらスクロール =====
float scrollIndex = index - centerIndex;
float maxScrollIndex = totalCount - visibleCount;
if (maxScrollIndex <= 0)
{
scrollRect.verticalNormalizedPosition = 1f;
return;
}
float normalizedPos = 1f - (scrollIndex / maxScrollIndex);
scrollRect.verticalNormalizedPosition = Mathf.Clamp01(normalizedPos);
}
}
コルーチンパターン
using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class TMPDropdownScrollFix : MonoBehaviour
{
[SerializeField]
public TMP_Dropdown dropdown;
[SerializeField]
private Button button;
void Start()
{
button.onClick.AddListener(() => OpenDropdown());
}
void OpenDropdown()
{
dropdown.Show();
StartCoroutine(ScrollToSelected());
}
IEnumerator ScrollToSelected()
{
// UI生成待ち(超重要)
yield return null;
yield return null;
var scrollRect = dropdown.GetComponentInChildren<ScrollRect>();
if (scrollRect == null) yield break;
var content = scrollRect.content;
if (content.childCount == 0) yield break;
int index = dropdown.value;
int totalCount = dropdown.options.Count;
if (totalCount <= 1) yield break;
// ===== 表示行数を計算 =====
float viewportHeight = scrollRect.viewport.rect.height;
float itemHeight =
((RectTransform)content.GetChild(0)).rect.height;
int visibleCount = Mathf.FloorToInt(viewportHeight / itemHeight);
int centerIndex = visibleCount / 2;
// ===== 中央まではスクロールしない =====
if (index <= centerIndex)
{
scrollRect.verticalNormalizedPosition = 1f;
yield break;
}
// ===== 中央を超えたらスクロール =====
float maxScrollIndex = totalCount - visibleCount;
if (maxScrollIndex <= 0)
{
scrollRect.verticalNormalizedPosition = 1f;
yield break;
}
float scrollIndex = index - centerIndex;
float normalizedPos = 1f - (scrollIndex / maxScrollIndex);
scrollRect.verticalNormalizedPosition = Mathf.Clamp01(normalizedPos);
}
}
