DropdownField は、ユーザーがリストから値を選択するためのものです。コントロールをクリックすると複数の選択肢を提示するドロワーが開き、ユーザーは そのうち 1 つを選択できます。常にすべての選択肢を表示したい場合は、RadioButtonGroupを を使用します。
You can create a DropdownField with UI Builder, UXML, or C#.
UI Builder
To add options for a DropdownField in UI Builder, in the Inspector panel of the DropdownField, enter the options separated by commas in Choices.
デフォルト値を設定するには、DropdownField の Inspector で、Index にオプションの番号を0
から順に入力します。
C#
The following C# example create a DropdownField with three options. The DropdownField selects the first option by default:
var dropdown = new DropdownField("Options", new List<string> { "Option 1", "Option 2", "Option 3" }, 0);
//Add another option
dropdown.choices.Add("Option 4");
// To return int value instead of string
dropdown.index = 1; // Option 2
dropdown.value = "Option3";
Assert.IsTrue(myDropdown.index == 2);
// Register to the value changed callback
dropdown.RegisterValueChangedCallback(evt => Debug.Log(evt.newValue));
You can override the default Panel Settings fields in a USS file to change the styles of a DropdownField:
The following UXML example creates a DropdownField:
[!code-xml[(External/Resources/editor_resources/Assets/Editor Default Resources/UIPackageResources/Snippets/UXML/DropdownFieldSnippet.uxml)]
The following C# example illustrates some of the customizable functionalities of the DropdownField:
[!code-cs[(Modules/UIElementsSamplesEditor/Snippets/DropdownFieldSnippet.cs#sample)]
To try this example live in Unity, go to Window > UI Toolkit > Samples.
C#クラス: DropdownField
名前空間: UnityEngine.UIElements
基本クラス: PopupField_1
この要素は基本クラスから以下の属性を継承します。
名前 | 型 | 説明 |
---|---|---|
binding-path |
string |
バインドされるべきターゲットプロパティのパス。 |
choices |
string |
ポップアップメニューに表示する選択肢のリストです。 |
focusable |
boolean |
要素がフォーカス可能である場合は true。 |
index |
int |
ポップアップメニューで、現在選択されているインデックスです。 |
label |
string |
フィールドの横に表示されるラベルを表す文字列。 |
tabindex |
int |
フォーカスリング内でフォーカス可能なものをソートするために使用される整数。0 以上であることが必要です。 |
この要素は、VisualElement
から以下の属性も継承します。
名前 | 型 | 説明 |
---|---|---|
content-container |
string |
子要素はそれに追加されますが、通常は要素自体と同じです。 |
name |
string |
この VisualElement の名前。 このプロパティを使用して、特定の要素を対象とする USS セレクターを記述します。要素には一意の名前を付けるのが標準的です。 |
picking-mode |
UIElements.PickingMode |
mouseEvents または IPanel.Pick クエリの間にこの要素を選択 (ピック) できるかどうかを決定します。 |
style |
string |
この要素のスタイルオブジェクトへの参照。 C# でこのオブジェクトに書き込まれた USS ファイルまたはインラインスタイルから計算されたデータが含まれます。 |
toolbar |
string |
ユーザーが要素をマウスオーバーした後、わずかな時間、情報ボックス内に表示するテキスト。これはエディター UI でのみサポートされます。 |
usage-hints |
UIElements.UsageHints |
VisualElement の高レベルの使用パターンを指定するヒント値の組み合わせ。このプロパティは、VisualElement がまだ Panel の一部でない場合にのみ設定できます。Panel の一部になると、このプロパティは事実上読み取り専用となり、変更しようとすると例外がスローされます。適切な UsageHints を指定することで、予想される使用パターンに基づいて、特定の操作をどのように処理するか、または高速化するかについて、システムがより適切な判断を下すようになります。これらのヒントは動作や視覚的な結果には影響しませんが、パネルとその中の要素の全体的なパフォーマンスにのみ影響することに注意してください。常に適切なUsageHints を指定することを考慮することをお勧めしますが、特定の条件下 (例えば、ターゲットプラットフォームのハードウェア制限など) では、いくつかの UsageHints が内部的に無視される可能性があることに留意してください。 |
view-data-key |
string |
ビューデータの永続性 (ツリーの展開状態、スクロール位置、ズームレベルなど) に使用されます。 これは、ビューデータストアからビューデータを保存/ロードするために使用されるキーです。このキーを設定しないと、この VisualElement の永続性が無効になります。 |
以下の表は、すべての C# パブリックプロパティ名と、それに関連する USS セレクターのリストです。
C# プロパティ | USS セレクター | 説明 |
---|---|---|
ussClassName |
.unity-popup-field |
この型の要素の USS クラス名。 |
labelUssClassName |
.unity-popup-field__label |
この型の要素のラベルの USS クラス名。 |
inputUssClassName |
.unity-popup-field__input |
この型の要素の入力要素の USS クラス名。 |
ussClassName |
.unity-base-popup-field |
この型の要素の USS クラス名。 |
textUssClassName |
.unity-base-popup-field__text |
この型の要素のテキスト要素の USS クラス名。 |
arrowUssClassName |
.unity-base-popup-field__arrow |
この型の要素の矢印インジケーターの USS クラス名。 |
labelUssClassName |
.unity-base-popup-field__label |
この型の要素のラベルの USS クラス名。 |
inputUssClassName |
.unity-base-popup-field__input |
この型の要素の入力要素の USS クラス名。 |
ussClassName |
.unity-base-field |
この型の要素の USS クラス名。 |
labelUssClassName |
.unity-base-field__label |
この型の要素のラベルの USS クラス名。 |
inputUssClassName |
.unity-base-field__input |
この型の要素の入力要素の USS クラス名。 |
noLabelVariantUssClassName |
.unity-base-field--no-label |
ラベルがない場合の、この型の要素の USS クラス名。 |
labelDraggerVariantUssClassName |
.unity-base-field__label--with-dragger |
この型の要素にドラッガーがアタッチされている場合のラベルの USS クラス名。 |
mixedValueLabelUssClassName |
.unity-base-field__label--mixed-value |
混合値を示す要素の USS クラス名 |
alignedFieldUssClassName |
.unity-base-field__aligned |
インスペクター要素で整列される要素の USS クラス名 |
disabledUssClassName |
.unity-disabled |
ローカルで無効にされた要素の USS クラス名。 |
また、Inspector や UI Toolkit Debugger の Matching Selectors セクション を使用して、どの USS セレクターが VisualElement
のコンポーネントに影響するかを、階層のすべてのレベルで確認することもできます。