Version: Unity 6.0 (6000.0)
言語 : 日本語
USS セレクターをバインドするカスタムバインディングの作成
SerializedObject のデータバインディング

ランタイムバインディングによる ListView とリストのバインド

バージョン: 6000+

ListView コントロールは、 リストの作成に最も効率的な方法です 。この例では、ランタイムバインディングを使用して ListView をリストにバインドする方法を示します。ランタイムバインディングは、ランタイム UI とエディター UI の両方をサポートします。この例ではデモとして、エディターウィンドウに ListView を表示します。

例の概要

この例では、ListView とリストを作成します。ListView をリストにバインドするには、ListView のバインディングデータソースにリストを含むプロパティを設定します。

この例で作成する完成したファイルは、この GitHub リポジトリ にあります。

必要な要件

このガイドは、Unity エディター、UI Toolkit、および C# スクリプトに精通している開発者を対象としています。始める前に、以下の点を理解しておいてください。

リストを持つオブジェクトの作成

Item オブジェクトのリストを持つ List オブジェクトの例を作成します。各 Item には、nameenabled プロパティが含まれます。

  1. 任意のテンプレートで Unity プロジェクトを作成します。
  2. Project ウィンドウで、runtime-binding-listview という名前のフォルダーを作成し、すべてのファイルを保存します。
  3. runtime-binding-listview フォルダーに、Scripts という名前のフォルダーを作成し、すべての C# スクリプトを保存します。
  4. Scripts フォルダーに、以下の内容の C# スクリプトを作成し ExampleItemObject.cs と命名します。
   using System;
   using System.Collections.Generic;
   using UnityEngine;
   
   public class ExampleItemObject  
   {
       public List<Item> items = new();
   
       public void Reset()
       {
           items = new List<Item>{
               new() { name = "Use Local Server", enabled = false },
               new() { name = "Show Debug Menu", enabled = false },
               new() { name = "Show FPS Counter", enabled = true },
           };
       }
   
       public struct Item
       {
           public bool enabled;
           public string name;
       }
   }

UI Builder で ListView 項目テンプレートを作成する

UI Builder でリスト項目のテンプレートを作成します。各項目には Toggle と TextField を追加します。これらを Item オブジェクトの enabled および name プロパティにバインドします。

  1. runtime-binding-listview フォルダーに、UXML という名前のフォルダーを作成します。

  2. UXML フォルダーに、ListViewItem.uxml という名前の UXML ファイルを作成します。

  3. ListViewItem.uxml ファイルをダブルクリックして、UI Builder で開きます。

  4. Hierarchy パネルで、VisualElement を追加します。

  5. VisualElement の子として Toggle を追加します。

  6. VisualElement の子として TextField を追加します。

  7. ToggleTextField のラベルテキストを削除します。

  8. VisualElementFlex directionRow に設定します。

  9. ToggleInspector パネルで、以下を行います。

  10. Value を右クリックし、Add binding を選択します。

  11. Add Binding ウィンドウで、Data Source Pathenabled に設定します。

  12. TextFieldInspector パネルで、以下を行います。

    • Value を右クリックし、Add binding を選択します。
    • Add Binding ウィンドウで、Data Source Pathname に設定します。
  13. UXML ファイルを保存します。完成した UXML ファイルは以下のようになります。

   <ui:UXML xmlns:ui="UnityEngine.UIElements">
       <ui:VisualElement name="VisualElement" style="flex-direction: row;">
           <ui:Toggle>
               <Bindings>
                   <ui:DataBinding property="value" data-source-path="enabled"  />
               </Bindings>
           </ui:Toggle>
           <ui:TextField placeholder-text="filler text">
               <Bindings>
                   <ui:DataBinding property="value" data-source-path="name"  />
               </Bindings>
           </ui:TextField>
       </ui:VisualElement>
   </ui:UXML>

UI Builder で ListView UI を作成する

UI Builder で ListView UI を作成し、項目テンプレートを ListViewItem.uxml に設定します。

  1. UXML フォルダーに、UIListView.uxml という名前の UXML ファイルを作成します。

  2. UIListView.uxml ファイルをダブルクリックして、UI Builder で開きます。

  3. Hierarchy パネルで、ListView を追加します。

  4. ListViewInspector パネルで、以下を行います。

  5. Virtualization MethodDynamic Height に設定します。

  6. Reorder ModeAnimated に設定します。

  7. Item TemplateListViewItem.uxml に設定します。

  8. Binding Source Selection ModeAuto Assign に設定します。

  9. Reorderable チェックボックスをオンにします。

  10. Show Add Remove Footer チェックボックスをオンにします。

  11. Header TitleItems に設定します。

  12. Show Foldout Header チェックボックスをオンにします。

  13. UXML ファイルを保存します。完成した UXML ファイルは以下のようになります。

   <ui:UXML xmlns:ui="UnityEngine.UIElements">
       <ui:ListView binding-source-selection-mode="AutoAssign" item-template="ListViewItem.uxml" />
   </ui:UXML>

カスタムエディターウィンドウを作成しバインディングを設定する

ListView を含むカスタムエディターウィンドウを作成し、Listitems プロパティにバインドします。この例では、C# スクリプトでバインディングソースを設定します。次のように、ListView 要素内の UXML ファイルにバインディングソースを手動で設定することもできます。

<ui:ListView>
    <Bindings>
        <ui:DataBinding property="itemsSource" data-source-path="items" />
    </Bindings>
</ui:ListView>
  1. Scripts フォルダーに、Editor という名前のフォルダーを作成します。
  2. Editor フォルダーに、以下の内容の C# スクリプトを作成し ListViewTestWindow.cs と命名します。
   using UnityEngine;
   using UnityEngine.UIElements;
   using UnityEditor;
   using UnityEditor.UIElements;
   using System.Collections.Generic;
   using Unity.Properties;
   
   internal class ListViewTestWindow : EditorWindow
   {
       // This example sets the itemTemplate and bindingSourceSelectionMode in the UXML file.
       // If you want to set them in the C# script, declare an itemLayout field of type VisualTreeAsset
       // and then set the "Item Layout" to "ListViewItem.uxml" in the Inspector window.
       //[SerializeField] private VisualTreeAsset itemLayout;
       [SerializeField] private VisualTreeAsset editorLayout;
   
       ExampleItemObject m_ExampleItemObject;
       
       [MenuItem("Window/ListViewTestWindow")]
       static void Init()
       {
           ListViewTestWindow window = EditorWindow.GetWindow<ListViewTestWindow>();
           window.Show();
       }
   
       void CreateGUI()
       {
           m_ExampleItemObject = new();
   
           editorLayout.CloneTree(rootVisualElement);
           var listView = rootVisualElement.Q<ListView>();
           
           // This example sets the itemTemplate and bindingSourceSelectionMode in the UXML file.
           // You can also set them in the C# script like the following:
           // listView.itemTemplate = itemLayout;
           // listView.bindingSourceSelectionMode = BindingSourceSelectionMode.AutoAssign;
   
           // Set the binding source to the ExampleItemObject instance.
           listView.dataSource = m_ExampleItemObject;
   
           // Set the itemsSource binding to the items property of the List object.
           // You can also set the itemsSource binding manually in the UXML file and comment out this line. 
           // Refer to the next section for how to set binding in UXML.
           listView.SetBinding("itemsSource", new DataBinding() {dataSourcePath = new PropertyPath("items")});
           
           m_ExampleItemObject.Reset();
       }
   }
   

バインディングのテスト

  • メニューから Window > ListViewTestWindow の順に選択します。

エディターウィンドウには、ExampleItemObject.cs で定義された項目にバインドされた ListView が表示されます。Itemenabled または name プロパティの値を ExampleItemObject.cs で更新すると、変更が自動的に ListView に反映されます。

追加リソース

USS セレクターをバインドするカスタムバインディングの作成
SerializedObject のデータバインディング