Skip to content

Commit 0585f78

Browse files
Merge pull request #23 from CoderGamester/develop
Release 0.13.1
2 parents 6a8628d + dcc4c1d commit 0585f78

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this package will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html)
66

7+
## [0.13.1] - 2025-09-28
8+
9+
**New**:
10+
- Added *UiToolkitPresenter<Data>* script to allow UI Toolkit based UIs to work similar to *UiPresenter<Data>*
11+
12+
**Changed**:
13+
- Refactored *UiToolkitPresenter* to also pass the root visual element to it's implemented class and properly assign the element OnValidate
14+
715
## [0.13.0] - 2025-09-25
816

917
**New**:

Runtime/UiService.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,26 @@ public async UniTask<UiPresenter> OpenUiAsync(Type type)
252252
public async UniTask<UiPresenter> OpenUiAsync<TData>(Type type, TData initialData) where TData : struct
253253
{
254254
var ui = await GetOrLoadUiAsync(type);
255-
var uiPresenter = ui as UiPresenter<TData>;
256-
257-
if (uiPresenter == null)
255+
256+
if (ui is UiToolkitPresenter<TData>)
257+
{
258+
var uiPresenter = ui as UiToolkitPresenter<TData>;
259+
260+
uiPresenter.InternalSetData(initialData);
261+
}
262+
else if (ui is UiPresenter<TData>)
258263
{
259-
Debug.LogError($"The UiPresenter {type} is not a UiPresenter<TData> type you. " +
264+
var uiPresenter = ui as UiPresenter<TData>;
265+
266+
uiPresenter.InternalSetData(initialData);
267+
}
268+
else
269+
{
270+
Debug.LogError($"The UiPresenter {type} is not a {nameof(UiPresenter<TData>)} nor {nameof(UiToolkitPresenter<TData>)} type. " +
260271
$"Implement it to allow it to open with initial defined data");
261272
return ui;
262273
}
263-
264-
uiPresenter.InternalSetData(initialData);
274+
265275
OpenUi(type);
266276

267277
return ui;

Runtime/UiToolkitPresenter.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
namespace GameLovers.UiService
55
{
6-
/// <summary>
7-
/// Interface for UI Toolkit-based presenters.
8-
/// </summary>
9-
public interface IUiPresenter
6+
/// /// <summary>
7+
/// Tags the <see cref="UiToolkitPresenter"/> as a <see cref="UiToolkitPresenterData{T}"/> to allow defining a
8+
/// specific state when opening the UI via the <see cref="UiService"/>
9+
public interface IUiToolkitPresenterData : IUiPresenterData
1010
{
1111
}
1212

@@ -15,21 +15,50 @@ public interface IUiPresenter
1515
/// and provides derived presenters access to it.
1616
/// </summary>
1717
[RequireComponent(typeof(UIDocument))]
18-
public abstract class UiToolkitPresenter : UiPresenter, IUiPresenter
18+
public abstract class UiToolkitPresenter : UiPresenter
1919
{
2020
[SerializeField] private UIDocument _document;
2121

2222
/// <summary>
2323
/// Provides access to the attached <see cref="UIDocument"/> for derived presenters.
2424
/// </summary>
2525
protected UIDocument Document => _document;
26+
27+
/// <summary>
28+
/// The root element of the <see cref="UIDocument"/> defining this presenter
29+
/// </summary>
30+
protected VisualElement Root => _document.rootVisualElement;
2631

2732
/// <summary>
2833
/// Assigns the serialized <see cref="UIDocument"/> reference from the component on this GameObject.
2934
/// </summary>
3035
protected virtual void OnValidate()
3136
{
32-
_document = GetComponent<UIDocument>();
37+
_document = _document == null ? GetComponent<UIDocument>() : _document;
38+
}
39+
}
40+
41+
/// <inheritdoc cref="UiToolkitPresenter"/>
42+
/// <remarks>
43+
/// Extends the <see cref="UiToolkitPresenter"/> behaviour to hold data of type <typeparamref name="T"/>
44+
/// </remarks>
45+
public abstract class UiToolkitPresenter<T> : UiToolkitPresenter, IUiToolkitPresenterData where T : struct
46+
{
47+
/// <summary>
48+
/// The Ui data defined when opened via the <see cref="UiService"/>
49+
/// </summary>
50+
public T Data { get; protected set; }
51+
52+
/// <summary>
53+
/// Allows the ui presenter implementation to have extra behaviour when the data defined for the presenter is set
54+
/// </summary>
55+
protected virtual void OnSetData() {}
56+
57+
internal void InternalSetData(T data)
58+
{
59+
Data = data;
60+
61+
OnSetData();
3362
}
3463
}
3564
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "com.gamelovers.uiservice",
33
"displayName": "UiService",
44
"author": "Miguel Tomas",
5-
"version": "0.13.0",
5+
"version": "0.13.1",
66
"unity": "6000.0",
77
"license": "MIT",
88
"description": "This package provides a service to help manage an Unity's, game UI.\nIt allows to open, close, load, unload and request any Ui Configured in the game.\nThe package provides a Ui Set that allows to group a set of Ui Presenters to help load, open and close multiple Uis at the same time.\n\nTo help configure the game's UI you need to create a UiConfigs Scriptable object by:\n- Right Click on the Project View > Create > ScriptableObjects > Configs > UiConfigs",

0 commit comments

Comments
 (0)