Net.Like.Xue.Tokyo/Assets/BITKit/Unity/Scripts/UX/Service/UI Toolkit/UIToolkitOverlay.cs

54 lines
1.8 KiB
C#
Raw Normal View History

2024-11-03 16:42:23 +08:00
using System;
using System.Threading;
using BITKit.Mod;
using Cysharp.Threading.Tasks;
using UnityEngine.UIElements;
// ReSharper disable MemberCanBePrivate.Global
namespace BITKit.UX
{
public abstract class UIToolkitOverlay:IDisposable
{
protected readonly CancellationTokenSource CancellationToken;
protected readonly IUXService UXService;
protected VisualElement RootVisualElement { get; set; }
2024-12-28 23:19:55 +08:00
protected bool IsInitialized { get; private set; }
2024-11-03 16:42:23 +08:00
protected UIToolkitOverlay(IUXService uxService, CancellationTokenSource cancellationToken)
{
UXService = uxService;
CancellationToken = cancellationToken;
CancellationToken.Token.Register(Dispose);
}
protected abstract string DocumentPath { get; }
// ReSharper disable once MemberCanBeProtected.Global
public virtual async UniTask InitializeAsync()
{
2024-12-28 23:19:55 +08:00
if(IsInitialized)return;
2024-11-03 16:42:23 +08:00
var asset =await ModService.LoadAsset<VisualTreeAsset>(DocumentPath);
var root= RootVisualElement = UXService.Root.As<VisualElement>().Create(asset);
RootVisualElement.BringToFront();
RootVisualElement.name = GetType().Name;
RootVisualElement.pickingMode = PickingMode.Ignore;
root.style.position = Position.Absolute;
root.style.top = 0;
root.style.bottom = 0;
root.style.left = 0;
root.style.right = 0;
UXUtils.Inject(this);
2024-12-28 23:19:55 +08:00
IsInitialized = true;
2024-11-03 16:42:23 +08:00
}
public virtual void Dispose()
{
if(RootVisualElement is null)return;
2024-12-28 23:19:55 +08:00
IsInitialized = false;
2024-11-03 16:42:23 +08:00
RootVisualElement.RemoveFromHierarchy();
RootVisualElement = null;
}
}
}