BITFALL/Assets/BITKit/Unity/Scripts/UX/DataBind/UXDataBindingService.cs

94 lines
2.3 KiB
C#
Raw Normal View History

2024-01-27 04:09:57 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
2024-03-14 22:20:09 +08:00
using System.Security.Cryptography;
using BITKit.UX;
using Newtonsoft.Json;
using Unity.Collections;
2024-01-27 04:09:57 +08:00
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.IData
{
2024-03-14 22:20:09 +08:00
public class UXDataBindingService : MonoBehaviour
2024-01-27 04:09:57 +08:00
{
2024-03-14 22:20:09 +08:00
[RuntimeInitializeOnLoadMethod]
private static void Reload()
2024-01-27 04:09:57 +08:00
{
2024-03-14 22:20:09 +08:00
_Binders.Clear();
_Instances.Clear();
2024-01-27 04:09:57 +08:00
}
2024-03-14 22:20:09 +08:00
private static readonly Dictionary<Type,IUXDataBinder> _Binders = new();
private static readonly CacheList<IUXDataBinder> _Instances = new();
public static IUXDataBinder Create(object target,FieldInfo fieldInfo)
2024-01-27 04:09:57 +08:00
{
2024-03-14 22:20:09 +08:00
var value = fieldInfo.GetValue(target);
var type = value?.GetType() ?? fieldInfo.FieldType;
2024-01-27 04:09:57 +08:00
2024-03-14 22:20:09 +08:00
// if (value is IEnumerable)
// {
// //type = typeof(IEnumerable<>).MakeGenericType(fieldInfo.FieldType.GetElementType());
// //type = typeof(IEnumerable<>).MakeGenericType(type.GetGenericArguments());
// }
2024-01-27 04:09:57 +08:00
switch (value)
{
2024-03-14 22:20:09 +08:00
case Array:
type = typeof(IEnumerable<>).MakeGenericType(fieldInfo.FieldType.GetElementType());
2024-01-27 04:09:57 +08:00
break;
2024-03-14 22:20:09 +08:00
case IList:
type = typeof(IEnumerable<>).MakeGenericType(type.GetGenericArguments());
2024-01-27 04:09:57 +08:00
break;
}
2024-03-14 22:20:09 +08:00
if (_Binders.TryGetValue(type, out var binder) is false)
throw new NotImplementedException($"未支持的类型:{type.Name}");
var instance = (IUXDataBinder)Activator.CreateInstance(binder.GetType());
_Instances.Add(instance);
return instance;
2024-01-27 04:09:57 +08:00
}
2024-03-14 22:20:09 +08:00
[SerializeReference, SubclassSelector] private ITicker ticker;
2024-01-27 04:09:57 +08:00
2024-03-14 22:20:09 +08:00
private async void Start()
2024-01-27 04:09:57 +08:00
{
2024-03-14 22:20:09 +08:00
var binders = await ReflectionHelper.GetTypes<IUXDataBinder>();
if(destroyCancellationToken.IsCancellationRequested)return;
foreach (var x in binders)
{
var instance = (IUXDataBinder)Activator.CreateInstance(x);
if (_Binders.TryAdd(instance.TargetType, instance)) ;
}
2024-01-27 04:09:57 +08:00
ticker.Add(OnTick);
2024-03-14 22:20:09 +08:00
destroyCancellationToken.Register(() =>
{
ticker.Remove(OnTick);
});
2024-01-27 04:09:57 +08:00
}
2024-03-14 22:20:09 +08:00
private static void OnTick(float obj)
2024-01-27 04:09:57 +08:00
{
2024-03-14 22:20:09 +08:00
var array = _Instances.ValueArray;
foreach (var x in array)
2024-01-27 04:09:57 +08:00
{
2024-03-14 22:20:09 +08:00
if (x.VisualElement.panel is null)
2024-01-27 04:09:57 +08:00
{
2024-03-14 22:20:09 +08:00
_Instances.Remove(x);
continue;
2024-01-27 04:09:57 +08:00
}
2024-03-14 22:20:09 +08:00
x.OnTick(obj);
2024-01-27 04:09:57 +08:00
}
}
}
}