riversong code showcase
This commit is contained in:
41
Source/Riversong/Game/UI/Framework/UIService.cs
Normal file
41
Source/Riversong/Game/UI/Framework/UIService.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class UIService
|
||||
{
|
||||
public IUIRoot UIRoot { get; set; }
|
||||
|
||||
public UITemplateLibrary TemplateLibrary { get; set; }
|
||||
|
||||
public TextFormatHelper TextFormatHelper { get; set; }
|
||||
|
||||
public async UniTask<UIView> CreateView(Type type, VisualElement rootElement)
|
||||
{
|
||||
var view = (UIView)Activator.CreateInstance(type);
|
||||
await view.InitializeAsync(this, rootElement);
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
public static class UIServiceExtensions
|
||||
{
|
||||
public static async UniTask CreateViews<TModel, TView>(this UIService uiService, List<TModel> models, VisualElement container, VisualTreeAsset template)
|
||||
where TModel : UIModel where TView : UIView<TModel>
|
||||
{
|
||||
container.Clear();
|
||||
|
||||
foreach (var model in models)
|
||||
{
|
||||
var element = template.CloneTree();
|
||||
container.Add(element);
|
||||
|
||||
var view = (TView)await uiService.CreateView(typeof(TView), element);
|
||||
view.SetModel(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user