1
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"name": "BITKit.MarkSystem.Runtime",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:0b8afac07cb6a864385843f87eaa0e3f"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8915fe727b166284f9a8cbe958b03a05
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
8
Src/Unity/Scripts/MarkSystem/Core.meta
Normal file
8
Src/Unity/Scripts/MarkSystem/Core.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 208a5380beb526044819d512687a0eaf
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -2,14 +2,11 @@
|
||||
"name": "BITKit.MarkSystem",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:a209c53514018594f9f482516f2a6781",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:66d2ae14764cc7d49aad4b16930747c0",
|
||||
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:be17a8778dbfe454890ed8279279e153",
|
||||
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
|
||||
"GUID:9400d40641bab5b4a9702f65bf5c6eb5"
|
||||
"GUID:6ef4ed8ff60a7aa4bb60a8030e6f4008",
|
||||
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
|
||||
"GUID:f51ebe6a0ceec4240a699833d6309b23",
|
||||
"GUID:d8b63aba1907145bea998dd612889d6b"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
@@ -17,9 +14,7 @@
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [
|
||||
"ODIN_INSPECTOR"
|
||||
],
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
28
Src/Unity/Scripts/MarkSystem/Core/IMarkSystem.cs
Normal file
28
Src/Unity/Scripts/MarkSystem/Core/IMarkSystem.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.MarkSystem
|
||||
{
|
||||
public interface IMarkObject
|
||||
{
|
||||
public int Id { get; }
|
||||
public Vector3 Position { get; }
|
||||
public object Object { get; }
|
||||
}
|
||||
public interface IMarkSystem
|
||||
{
|
||||
public void Register(IMarkObject markObject);
|
||||
public void UnRegister(IMarkObject markObject);
|
||||
}
|
||||
public class MarkObject : IMarkObject
|
||||
{
|
||||
public override bool Equals(object obj) => obj is MarkObject x && x.Id == Id;
|
||||
public override int GetHashCode() => Id.GetHashCode();
|
||||
private static int count;
|
||||
public int Id { get; } = count++;
|
||||
public Vector3 Position { get; set; }
|
||||
public object Object { get; set; }
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7869b4bffebc8f6439819f03f741da2b
|
||||
guid: 26e45143692a3de45b4efef997c74811
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
@@ -1,125 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using BITKit.SubSystems;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit.Mark
|
||||
{
|
||||
|
||||
public interface IMarkObject
|
||||
{
|
||||
string GetID();
|
||||
string GetDisplay();
|
||||
Vector3 GetPostion();
|
||||
bool GetAcitve();
|
||||
}
|
||||
|
||||
public interface IMarkSystem
|
||||
{
|
||||
event Action<IMarkObject> OnAdd;
|
||||
event Action<IMarkObject> OnUpdate;
|
||||
event Action<IMarkObject> OnRemove;
|
||||
void Add(IMarkObject markObject);
|
||||
void Remove(IMarkObject markObject);
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class MarkSystemMonoProxy:IMarkSystem
|
||||
{
|
||||
[SerializeReference] private MonoBehaviour monoBehaviour;
|
||||
private IMarkSystem _markSystemImplementation=>monoBehaviour as IMarkSystem;
|
||||
public event Action<IMarkObject> OnAdd
|
||||
{
|
||||
add => _markSystemImplementation.OnAdd += value;
|
||||
remove => _markSystemImplementation.OnAdd -= value;
|
||||
}
|
||||
|
||||
public event Action<IMarkObject> OnUpdate
|
||||
{
|
||||
add => _markSystemImplementation.OnUpdate += value;
|
||||
remove => _markSystemImplementation.OnUpdate -= value;
|
||||
}
|
||||
|
||||
public event Action<IMarkObject> OnRemove
|
||||
{
|
||||
add => _markSystemImplementation.OnRemove += value;
|
||||
remove => _markSystemImplementation.OnRemove -= value;
|
||||
}
|
||||
|
||||
public void Add(IMarkObject markObject)
|
||||
{
|
||||
_markSystemImplementation.Add(markObject);
|
||||
}
|
||||
|
||||
public void Remove(IMarkObject markObject)
|
||||
{
|
||||
_markSystemImplementation.Remove(markObject);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class MarkSystemSingleton:IMarkSystem
|
||||
{
|
||||
private IMarkSystem _markSystemImplementation=>MarkSystem.Singleton;
|
||||
public event Action<IMarkObject> OnAdd
|
||||
{
|
||||
add => _markSystemImplementation.OnAdd += value;
|
||||
remove => _markSystemImplementation.OnAdd -= value;
|
||||
}
|
||||
|
||||
public event Action<IMarkObject> OnUpdate
|
||||
{
|
||||
add => _markSystemImplementation.OnUpdate += value;
|
||||
remove => _markSystemImplementation.OnUpdate -= value;
|
||||
}
|
||||
|
||||
public event Action<IMarkObject> OnRemove
|
||||
{
|
||||
add => _markSystemImplementation.OnRemove += value;
|
||||
remove => _markSystemImplementation.OnRemove -= value;
|
||||
}
|
||||
|
||||
public void Add(IMarkObject markObject)
|
||||
{
|
||||
_markSystemImplementation.Add(markObject);
|
||||
}
|
||||
|
||||
public void Remove(IMarkObject markObject)
|
||||
{
|
||||
_markSystemImplementation.Remove(markObject);
|
||||
}
|
||||
}
|
||||
public class MarkSystem : MonoBehaviour,IMarkSystem
|
||||
{
|
||||
internal static IMarkSystem Singleton { get; private set; }
|
||||
public event Action<IMarkObject> OnAdd;
|
||||
public event Action<IMarkObject> OnUpdate;
|
||||
public event Action<IMarkObject> OnRemove;
|
||||
List<IMarkObject> markObjects = new();
|
||||
public void Add(IMarkObject markObject)
|
||||
{
|
||||
markObjects.Add(markObject);
|
||||
OnAdd?.Invoke(markObject);
|
||||
}
|
||||
public void Remove(IMarkObject markObject)
|
||||
{
|
||||
markObjects.TryRemove(markObject);
|
||||
OnRemove?.Invoke(markObject);
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Singleton = this;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
foreach (var markObject in markObjects.ToArray())
|
||||
{
|
||||
OnUpdate?.Invoke(markObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,129 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using BITKit.UX;
|
||||
using BITKit.SubSystems;
|
||||
using UnityEngine.UIElements;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
namespace BITKit.Mark
|
||||
{
|
||||
public class MarkSystemUIToolkit : MonoBehaviour
|
||||
{
|
||||
public UXElement element;
|
||||
[SerializeField,SerializeReference, SubclassSelector] private References className;
|
||||
[SerializeField, SerializeReference, SubclassSelector]
|
||||
private IMarkSystem markSystem;
|
||||
Dictionary<string, Label> dictionary = new();
|
||||
IPanel panel;
|
||||
CancellationToken cancellationToken;
|
||||
void Awake()
|
||||
{
|
||||
cancellationToken = gameObject.GetCancellationTokenOnDestroy();
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
panel = element.GetVisualElement().panel;
|
||||
if (markSystem is not null)
|
||||
{
|
||||
markSystem.OnAdd += OnAdd;
|
||||
markSystem.OnUpdate += OnUpdate;
|
||||
markSystem.OnRemove += OnRemove;
|
||||
}
|
||||
}
|
||||
void OnDestroy()
|
||||
{
|
||||
if (markSystem is not null)
|
||||
{
|
||||
markSystem.OnAdd -= OnAdd;
|
||||
markSystem.OnUpdate -= OnUpdate;
|
||||
markSystem.OnRemove -= OnRemove;
|
||||
}
|
||||
}
|
||||
void OnAdd(IMarkObject markObject)
|
||||
{
|
||||
GetOrCreate(markObject.GetID());
|
||||
}
|
||||
async void OnRemove(IMarkObject markObject)
|
||||
{
|
||||
try
|
||||
{
|
||||
await UniTask.SwitchToMainThread(cancellationToken);
|
||||
if (dictionary.TryGetValue(markObject.GetID(), out var label))
|
||||
{
|
||||
element.GetVisualElement().Remove(label);
|
||||
dictionary.Remove(markObject.GetID());
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
if (e is not OperationCanceledException)
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
async void OnUpdate(IMarkObject markObject)
|
||||
{
|
||||
var cameraTrans = Camera.main.transform;
|
||||
try
|
||||
{
|
||||
await UniTask.SwitchToMainThread();
|
||||
var active = markObject.GetAcitve();
|
||||
var label = GetOrCreate(markObject.GetID());
|
||||
if (active)
|
||||
{
|
||||
var pos = RuntimePanelUtils
|
||||
.CameraTransformWorldToPanel(panel, markObject.GetPostion(), Camera.main);
|
||||
pos.x = (pos.x - label.layout.width / 2);
|
||||
|
||||
Rect elementRect = new()
|
||||
{
|
||||
position = pos,
|
||||
size = label.layout.size,
|
||||
};
|
||||
label.text = markObject.GetDisplay();
|
||||
label.transform.position = pos;
|
||||
if (Vector3.Dot(cameraTrans.forward, markObject.GetPostion() - cameraTrans.position) > 0)
|
||||
{
|
||||
label.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
label.SetActive(false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
label.SetActive(false);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
}
|
||||
catch (System.Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
Label GetOrCreate(string id)
|
||||
{
|
||||
if (dictionary.TryGetValue(id, out var label))
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
label = new();
|
||||
label.AddToClassList(className);
|
||||
label.style.position = Position.Absolute;
|
||||
dictionary.Add(id, label);
|
||||
element.GetVisualElement().Add(label);
|
||||
}
|
||||
return label;
|
||||
}
|
||||
}
|
||||
}
|
52
Src/Unity/Scripts/MarkSystem/UXMarkSystem.cs
Normal file
52
Src/Unity/Scripts/MarkSystem/UXMarkSystem.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a9619851d703d5b4fba4db3eefcdbf57
|
||||
guid: 3cd5204b8318b3d408d1e2fc2818bf79
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
Reference in New Issue
Block a user