BITKit/Src/Unity/Scripts/MarkSystem/UXMarkSystem.cs

53 lines
1.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using BITKit.MarkSystem;
using I18N.Other;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.UX
{
[CustomType(typeof(IMarkSystem))]
public class UXMarkSystem : MonoBehaviour,IMarkSystem
{
[SerializeField] private VisualTreeAsset template;
[UXBindPath("mark-container")]
private VisualElement _container;
private readonly CacheList<IMarkObject> _markObjects = new();
private readonly ConcurrentDictionary<int,VisualElement> _dictionary = new();
private void OnEnable()
{
DI.Register<IMarkSystem>(this);
UXUtils.Inject(this);
_container.Clear();
}
public void Register(IMarkObject markObject)
{
_markObjects.Add(markObject);
_dictionary.GetOrAdd(markObject.Id,Create);
}
public void UnRegister(IMarkObject markObject)
{
_markObjects.Remove(markObject);
_dictionary.TryRemove(markObject.Id,out var x);
x.RemoveFromHierarchy();
}
private void LateUpdate()
{
foreach (var x in _markObjects.ValueArray)
{
_dictionary[x.Id].SetPosition(x.Position);
}
}
private VisualElement Create(int id)
{
return _container.Create(template);
}
}
}