update conncetors

This commit is contained in:
Mikhail 2024-03-10 10:20:44 +08:00
parent c865955e8b
commit 3f1c38a510
5 changed files with 60 additions and 15 deletions

View File

@ -6,6 +6,33 @@ using UnityEngine;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public static class EcsConnect
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Connect(GameObject go, entlong entity, bool applyTemplates)
{
Connect(entity, go, applyTemplates);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Connect(entlong entity, GameObject go, bool applyTemplates)
{
if (go.TryGetComponent(out EcsEntityConnect connect) == false)
{
connect = go.AddComponent<EcsEntityConnect>();
}
connect.ConnectWith(entity, applyTemplates);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Connect(EcsEntityConnect connect, entlong entity, bool applyTemplates)
{
Connect(entity, connect, applyTemplates);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Connect(entlong entity, EcsEntityConnect connect, bool applyTemplates)
{
connect.ConnectWith(entity, applyTemplates);
}
}
[DisallowMultipleComponent] [DisallowMultipleComponent]
public class EcsEntityConnect : MonoBehaviour public class EcsEntityConnect : MonoBehaviour
{ {
@ -37,7 +64,7 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _world; } get { return _world; }
} }
public bool IsConected public bool IsConnected
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _entity.IsAlive; } get { return _entity.IsAlive; }
@ -59,22 +86,23 @@ namespace DCFApixels.DragonECS
#region Connect #region Connect
public void ConnectWith(entlong entity, bool applyTemplates) public void ConnectWith(entlong entity, bool applyTemplates)
{ {
if (_entity.TryGetID(out int oldEntityID) && _world != null) Disconnect();
{
var a = _world.GetAspect<Aspect>();
a.unityGameObjects.TryDel(oldEntityID);
}
_world = null;
if (entity.TryUnpack(out int newEntityID, out EcsWorld world)) if (entity.TryUnpack(out int newEntityID, out EcsWorld world))
{ {
_entity = entity; _entity = entity;
_world = world; _world = world;
var a = _world.GetAspect<Aspect>(); var a = _world.GetAspect<Aspect>();
if (a.unityGameObjects.Has(newEntityID) == false) if (a.unityGameObjects.Has(newEntityID))
{ {
a.unityGameObjects.Add(newEntityID) = new UnityGameObjectConnect(this); ref readonly var uconnect = ref a.unityGameObjects.Read(newEntityID);
if (uconnect.IsConnected)
{
uconnect.connect.Disconnect();
} }
}
a.unityGameObjects.TryAddOrGet(newEntityID) = new UnityGameObjectConnect(this);
if (applyTemplates) if (applyTemplates)
{ {
ApplyTemplatesFor(world.id, newEntityID); ApplyTemplatesFor(world.id, newEntityID);
@ -85,6 +113,15 @@ namespace DCFApixels.DragonECS
_entity = entlong.NULL; _entity = entlong.NULL;
} }
} }
public void Disconnect()
{
if (_entity.TryGetID(out int oldEntityID) && _world != null)
{
var a = _world.GetAspect<Aspect>();
a.unityGameObjects.TryDel(oldEntityID);
}
_world = null;
}
#endregion #endregion
#region ApplyTemplates #region ApplyTemplates

View File

@ -109,7 +109,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
for (int i = 0; i < targets.Length; i++) for (int i = 0; i < targets.Length; i++)
{ {
if (targets[i].IsConected == true) if (targets[i].IsConnected == true)
{ {
EditorGUILayout.HelpBox("Multiple component editing is not available.", MessageType.Warning); EditorGUILayout.HelpBox("Multiple component editing is not available.", MessageType.Warning);
return; return;

View File

@ -14,9 +14,19 @@ namespace DCFApixels.DragonECS
public string Name public string Name
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => connect.name; get { return connect.name; }
} }
public UnityGameObjectConnect(EcsEntityConnect connect) public bool IsUnidirectional
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return connect == null; }
}
public bool IsConnected
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return transform != null; }
}
internal UnityGameObjectConnect(EcsEntityConnect connect)
{ {
this.connect = connect; this.connect = connect;
transform = connect.transform; transform = connect.transform;

View File

@ -2,9 +2,7 @@
using DCFApixels.DragonECS.Unity.Internal; using DCFApixels.DragonECS.Unity.Internal;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Reflection;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
@ -181,7 +179,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
GUI.EndScrollView(); GUI.EndScrollView();
Rect r = GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth, EditorGUIUtility.singleLineHeight); Rect r = GUILayoutUtility.GetRect(EditorGUIUtility.currentViewWidth, EditorGUIUtility.singleLineHeight);
if(_selectedPointMeta.process != null && _selectedPointMeta.system != null) if (_selectedPointMeta.process != null && _selectedPointMeta.system != null)
{ {
GUI.Label(r, $"{_selectedPointMeta.process.Name}-{_selectedPointMeta.system.Name}"); GUI.Label(r, $"{_selectedPointMeta.process.Name}-{_selectedPointMeta.system.Name}");
} }