Unity/TMPro/Dropdownのソースを表示
←
Unity/TMPro/Dropdown
ナビゲーションに移動
検索に移動
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、次のグループに属する利用者のみが実行できます:
登録利用者
。
このページのソースの閲覧やコピーができます。
==TMP_Dropdownのスクリプトで制御== <pre> 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(); } } </pre> ==Dropdownを開いたときに選択した項目を表示する方法== UIのButtonをDropdownに被せて、ButtonをクリックしたときにDropdownを開く以下処理を走らせる。 選択Indexが表示領域の中央より上なら、スクロールしないように。 ===UniTaskバージョン=== <pre> 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 DropdownScrollUtil.AdjustScroll(dropdown.gameObject); } } </pre> DropdownScrollUtil.cs <pre> using UnityEngine; using UnityEngine.UI; using Cysharp.Threading.Tasks; using TMPro; // Dropdownのスクロール位置調整 // 選択Indexが表示領域の中央より上なら、スクロールしないように // 使い方:await DropdownScrollUtil.AdjustScroll(dropdown.gameObject); public static class DropdownScrollUtil { public static async UniTask AdjustScroll(GameObject dropdownObj) { var dropdown = dropdownObj.GetComponent<TMP_Dropdown>(); if (dropdown == null) return; // UI生成待ち(Dropdown開いた直後は要る) 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); } } </pre> ===コルーチンパターン=== <pre> 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(DropdownScrollUtil.AdjustScroll(dropdown.gameObject)); } } </pre> DropdownScrollUtil.cs <pre> using System.Collections; using UnityEngine; using UnityEngine.UI; using TMPro; // 使い方:StartCoroutine(DropdownScrollUtil.AdjustScroll(dropdown.gameObject)); public static class DropdownScrollUtil { public static IEnumerator AdjustScroll(GameObject dropdownObj) { var dropdown = dropdownObj.GetComponent<TMP_Dropdown>(); if (dropdown == null) yield break; // UI生成待ち yield return null; yield return null; var scrollRect = dropdown.GetComponentInChildren<ScrollRect>(); if (scrollRect == null) yield break; int index = dropdown.value; int totalCount = dropdown.options.Count; if (totalCount <= 1) yield break; var viewport = scrollRect.viewport.rect.height; var content = scrollRect.content; if (content.childCount == 0) yield break; float itemHeight = ((RectTransform)content.GetChild(0)).rect.height; int visibleCount = Mathf.FloorToInt(viewport / itemHeight); int centerIndex = visibleCount / 2; // 中央まではスクロールしない if (index <= centerIndex) { scrollRect.verticalNormalizedPosition = 1f; yield break; } float scrollIndex = index - centerIndex; float maxScrollIndex = totalCount - visibleCount; if (maxScrollIndex <= 0) { scrollRect.verticalNormalizedPosition = 1f; yield break; } float normalizedPos = 1f - (scrollIndex / maxScrollIndex); scrollRect.verticalNormalizedPosition = Mathf.Clamp01(normalizedPos); } } </pre>
Unity/TMPro/Dropdown
に戻る。
ナビゲーション メニュー
個人用ツール
ログイン
名前空間
ページ
議論
日本語
表示
閲覧
ソースを閲覧
履歴表示
その他
検索
案内
プログラムメモ
php
flutter
java
android
kotlin
ios
unity
unrealengine
javascript
mysql
sqlite
postgresql
oracle
mroonga
mongodb
flash
electron
cocos2dx
titanium
cpp
ruby
perl
python
accessメモ
rss
html
monaca
cordova
golang
blender
セキュリティ
テストツール
サーバメモ
linux
dotnet
apacheメモ
htaccessメモ
subversion
git
仮想サーバ
ansible
sendgrid
xampp
cacti
mecab
faces
flashpolicyd
fcs
jenkins
運用
デザインメモ
css
ユーザビリティ
ux
サービスメモ
twitter
facebook
instagram
mixi
セカンドライフ
通信ログ横取り
google
ustream
aws
gcp
plesk
azure
vps
AI
その他サービス
便利系メモ
SEO
モバイル
抽象変数名
DDD
クライアント
firefox
chrome
pgp
windows
mac
jmetar
Thunderbird
excel
libreoffice
vpnclient
doxygen
VisualStudioCode
fastlane
metaquest
cmsメモ
mediawiki
pukiwiki
wordpress
その他
資格
IT用語
pvを稼ぐ方法
将棋プログラム
その他
ログイン
ページ内
メインページ
最近の更新
人気のページ
問い合わせ
ツール
リンク元
関連ページの更新状況
ページ情報