This commit is contained in:
CortexCore 2024-06-27 18:53:49 +08:00
parent 4a8c97d1fd
commit 21bbfe0617
5 changed files with 22 additions and 11 deletions

View File

@ -4,7 +4,7 @@ using System.Collections.Generic;
namespace BITKit.UX
{
public interface IUXWaitingHandle
public interface IUXWaitingHandle:IDisposable
{
string Message { get; set; }
object Container { get; }

View File

@ -11,7 +11,8 @@
"GUID:d525ad6bd40672747bde77962f1c401e",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:517785bb4600a5140b47eac5fa49b8fc",
"GUID:be17a8778dbfe454890ed8279279e153"
"GUID:be17a8778dbfe454890ed8279279e153",
"GUID:d8b63aba1907145bea998dd612889d6b"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@ -9,6 +9,8 @@ using BITKit.Entities;
using Cysharp.Threading.Tasks;
using UnityEngine.Profiling;
using UnityEngine.UIElements;
using Random = UnityEngine.Random;
// ReSharper disable RedundantTypeArgumentsOfMethod
namespace BITKit.Entities
@ -147,9 +149,9 @@ namespace BITKit.Entities
}
private void Awake()
{
if (Id.IsDefault())
Id = GetInstanceID();
CancellationToken = gameObject.GetCancellationTokenOnDestroy();
if (Id is 0)
Id = Guid.NewGuid().GetHashCode();
CancellationToken = destroyCancellationToken;
Set(CancellationToken);
if (useAwake)

View File

@ -89,6 +89,10 @@ namespace BITKit.Net.Kcp
address = m_host;
if(port is 27014 or 0)
port = m_port;
m_host = address;
m_port = port;
return _netClientImplementation.Connect(address, port);
}

View File

@ -37,6 +37,7 @@ namespace BITKit
public object Container => container;
internal VisualElement container;
internal Label label;
internal Action OnDispose;
public async void SetMessage(string message)
{
await UniTask.SwitchToMainThread();
@ -49,6 +50,8 @@ namespace BITKit
BIT4Log.Log<UXWaiting>("WaitingHandle is destroyed");
}
}
public void Dispose() => OnDispose?.Invoke();
}
[SerializeField] private VisualTreeAsset handleTemplate;
@ -59,14 +62,15 @@ namespace BITKit
[UXBindPath("waiting-root")]
private VisualElement _root;
private Pool<IUXWaitingHandle> _pool;
private readonly HashSet<IUXWaitingHandle> _handles = new();
private Pool<WaitingHandle> _pool;
private readonly HashSet<WaitingHandle> _handles = new();
public IUXWaitingHandle Get()
{
var handle = _pool.Take();
Active();
_handles.Add(handle);
handle.OnDispose = () => Release(handle);
return handle;
async void Active()
{
@ -79,8 +83,8 @@ namespace BITKit
{
await UniTask.SwitchToMainThread();
if (destroyCancellationToken.IsCancellationRequested) return;
_pool.Return(handle);
_handles.Remove(handle);
_pool.Return(handle.As<WaitingHandle>());
_handles.Remove(handle.As<WaitingHandle>());
Check();
}
protected override void Awake()
@ -88,7 +92,7 @@ namespace BITKit
base.Awake();
UXUtils.Inject(this);
_container.Clear();
_pool = new Pool<IUXWaitingHandle>(Create,OnReset,10);
_pool = new Pool<WaitingHandle>(Create,OnReset,10);
if (asGlobal)
{
DI.Register<IUXWaiting>(this);
@ -114,7 +118,7 @@ namespace BITKit
if(destroyCancellationToken.IsCancellationRequested) return;
_root.SetActive(_handles.Count>0);
}
private IUXWaitingHandle Create()
private WaitingHandle Create()
{
var handle = new WaitingHandle
{