BITKit/Packages/Runtime/UX/Debuger/UXDebuger.cs

60 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BITKit;
using UnityEngine.UIElements;
using Cysharp.Threading.Tasks;
using System.Linq;
namespace BITKit.UX
{
public sealed class UXDebuger : UXElement<ListView>, IDebuger
{
List<string> texts = new();
Dictionary<string, string> textDict = new();
public override void OnStart()
{
base.OnStart();
visualElement.itemsSource = texts;
visualElement.makeItem = MakeItem;
visualElement.bindItem = BindItem;
}
public void Log(string context)
{
texts.Add(context);
}
public async void Log(string title, string context)
{
//await UniTask.SwitchToThreadPool();
textDict.Insert(title, context);
texts.Clear();
texts.AddRange(textDict.Select(x => $"{x.Key}:{x.Value}"));
try
{
await UniTask.SwitchToMainThread(BITApp.CancellationTokenSource.Token);
}
catch (System.OperationCanceledException)
{
}
catch (System.Exception)
{
throw;
}
}
VisualElement MakeItem()
{
return new Label();
}
void BindItem(VisualElement element, int index)
{
var label = element as Label;
label.text = texts[index];
}
void Awake()
{
DI.Register<IDebuger>(this);
}
}
}