Skip to content

Commit 435badc

Browse files
committed
Add ItemTemplate selection directly in the UI Builder for Unity 2023.
1 parent c9b5575 commit 435badc

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/BindableListView.TItem.TCollection.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public virtual void SetBindingContext(IBindingContext context, IObjectProvider o
5454
}
5555

5656
_itemsSourceBindingData ??= BindingItemsSourcePath.ToPropertyBindingData();
57-
_itemTemplate ??= objectProvider.GetCollectionItemTemplate<TItemBindingContext, VisualTreeAsset>();
57+
_itemTemplate ??= GetItemTemplate(objectProvider);
5858

5959
_objectProvider = objectProvider;
6060

@@ -151,5 +151,16 @@ private void OnUnbindItem(VisualElement item, int index)
151151
UnbindItem(item, index, itemBindingContext, _objectProvider);
152152
}
153153
}
154+
155+
private VisualTreeAsset GetItemTemplate(IObjectProvider objectProvider)
156+
{
157+
#if UNITY_2023_2_OR_NEWER
158+
return ItemTemplate
159+
? ItemTemplate
160+
: objectProvider.GetCollectionItemTemplate<TItemBindingContext, VisualTreeAsset>();
161+
#else
162+
return objectProvider.GetCollectionItemTemplate<TItemBindingContext, VisualTreeAsset>();
163+
#endif
164+
}
154165
}
155166
}

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/BindableScrollView.T.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public virtual void SetBindingContext(IBindingContext context, IObjectProvider o
6262
}
6363

6464
_itemsSourceBindingData ??= BindingItemsSourcePath.ToPropertyBindingData();
65-
_itemTemplate ??= objectProvider.GetCollectionItemTemplate<TItemBindingContext, VisualTreeAsset>();
65+
_itemTemplate ??= GetItemTemplate(objectProvider);
6666

6767
_objectProvider = objectProvider;
6868

@@ -224,5 +224,16 @@ private void OnPoolDestroyItem(VisualElement item)
224224
{
225225
item.DisposeBindableElement(_objectProvider);
226226
}
227+
228+
private VisualTreeAsset GetItemTemplate(IObjectProvider objectProvider)
229+
{
230+
#if UNITY_2023_2_OR_NEWER
231+
return ItemTemplate
232+
? ItemTemplate
233+
: objectProvider.GetCollectionItemTemplate<TItemBindingContext, VisualTreeAsset>();
234+
#else
235+
return objectProvider.GetCollectionItemTemplate<TItemBindingContext, VisualTreeAsset>();
236+
#endif
237+
}
227238
}
228239
}

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/Uxmls/BindableListView.TItem.TCollection.Uxml.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@ namespace UnityMvvmToolkit.UITK.BindableUIElements
66
partial class BindableListView<TItemBindingContext, TCollection>
77
{
88
public string BindingItemsSourcePath { get; private set; }
9+
public VisualTreeAsset ItemTemplate { get; private set; }
910

1011
#if UNITY_2023_2_OR_NEWER
1112
[System.Serializable]
1213
public new class UxmlSerializedData : ListView.UxmlSerializedData
1314
{
14-
// ReSharper disable once InconsistentNaming
1515
#pragma warning disable 649
16+
// ReSharper disable once InconsistentNaming
1617
[UnityEngine.SerializeField] private string BindingItemsSourcePath;
18+
// ReSharper disable once InconsistentNaming
19+
[UnityEngine.SerializeField] private VisualTreeAsset ItemTemplate;
1720
#pragma warning restore 649
1821

1922
public override void Deserialize(object visualElement)
2023
{
2124
base.Deserialize(visualElement);
2225

23-
visualElement
24-
.As<BindableListView<TItemBindingContext, TCollection>>()
25-
.BindingItemsSourcePath = BindingItemsSourcePath;
26+
var bindableListView = visualElement.As<BindableListView<TItemBindingContext, TCollection>>();
27+
bindableListView.BindingItemsSourcePath = BindingItemsSourcePath;
28+
bindableListView.ItemTemplate = ItemTemplate;
2629
}
2730
}
2831
#else

src/UnityMvvmToolkit.UnityPackage/Assets/Plugins/UnityMvvmToolkit/Runtime/UITK/BindableUIElements/Uxmls/BindableScrollView.T.Uxml.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@ namespace UnityMvvmToolkit.UITK.BindableUIElements
66
partial class BindableScrollView<TItemBindingContext>
77
{
88
public string BindingItemsSourcePath { get; private set; }
9+
public VisualTreeAsset ItemTemplate { get; private set; }
910

1011
#if UNITY_2023_2_OR_NEWER
1112
[System.Serializable]
1213
public new class UxmlSerializedData : ScrollView.UxmlSerializedData
1314
{
14-
// ReSharper disable once InconsistentNaming
1515
#pragma warning disable 649
16+
// ReSharper disable once InconsistentNaming
1617
[UnityEngine.SerializeField] private string BindingItemsSourcePath;
18+
// ReSharper disable once InconsistentNaming
19+
[UnityEngine.SerializeField] private VisualTreeAsset ItemTemplate;
1720
#pragma warning restore 649
1821

1922
public override void Deserialize(object visualElement)
2023
{
2124
base.Deserialize(visualElement);
2225

23-
visualElement
24-
.As<BindableScrollView<TItemBindingContext>>()
25-
.BindingItemsSourcePath = BindingItemsSourcePath;
26+
var bindableListView = visualElement.As<BindableScrollView<TItemBindingContext>>();
27+
bindableListView.BindingItemsSourcePath = BindingItemsSourcePath;
28+
bindableListView.ItemTemplate = ItemTemplate;
2629
}
2730
}
2831
#else

0 commit comments

Comments
 (0)