update EcsEntityConnect add GetConnectByInstanceID

This commit is contained in:
Mikhail 2024-05-25 22:28:38 +08:00
parent 87f70d9927
commit 784b743b2a
3 changed files with 35 additions and 4 deletions

View File

@ -4,6 +4,7 @@ using System.Runtime.CompilerServices;
using UnityEngine; using UnityEngine;
#region UNITY_EDITOR #region UNITY_EDITOR
using UnityEditor; using UnityEditor;
using DCFApixels.DragonECS.Unity.Internal;
#endregion #endregion
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
@ -52,6 +53,7 @@ namespace DCFApixels.DragonECS
} }
} }
[SelectionBase]
[DisallowMultipleComponent] [DisallowMultipleComponent]
[AddComponentMenu(EcsConsts.FRAMEWORK_NAME + "/" + nameof(EcsEntityConnect), 30)] [AddComponentMenu(EcsConsts.FRAMEWORK_NAME + "/" + nameof(EcsEntityConnect), 30)]
public class EcsEntityConnect : MonoBehaviour public class EcsEntityConnect : MonoBehaviour
@ -59,6 +61,8 @@ namespace DCFApixels.DragonECS
private entlong _entity; private entlong _entity;
private EcsWorld _world; private EcsWorld _world;
private static SparseArray<EcsEntityConnect> _connectedEntities = new SparseArray<EcsEntityConnect>();
[SerializeField] [SerializeField]
private bool _deleteEntiityWithDestroy = false; private bool _deleteEntiityWithDestroy = false;
[SerializeField] [SerializeField]
@ -108,6 +112,7 @@ namespace DCFApixels.DragonECS
_isConnectInvoked = true; _isConnectInvoked = true;
_entity = entity; _entity = entity;
_world = world; _world = world;
_connectedEntities.Add(GetInstanceID(), this);
var goConnects = world.GetPool<GameObjectConnect>(); var goConnects = world.GetPool<GameObjectConnect>();
if (goConnects.Has(newEntityID)) if (goConnects.Has(newEntityID))
{ {
@ -136,6 +141,7 @@ namespace DCFApixels.DragonECS
{ {
var unityGameObjects = _world.GetPool<GameObjectConnect>(); var unityGameObjects = _world.GetPool<GameObjectConnect>();
unityGameObjects.TryDel(oldEntityID); unityGameObjects.TryDel(oldEntityID);
_connectedEntities.Remove(GetInstanceID());
} }
_world = null; _world = null;
_entity = entlong.NULL; _entity = entlong.NULL;
@ -161,10 +167,35 @@ namespace DCFApixels.DragonECS
{ {
entlong ent = _entity; entlong ent = _entity;
Disconnect(); Disconnect();
if (_deleteEntiityWithDestroy && ent.TryUnpack(out int id, out EcsWorld world))
if (_deleteEntiityWithDestroy == false)
{ {
world.DelEntity(id); return;
} }
ent.UnpackUnchecked(out int e, out short gen, out short worldID);
var world = EcsWorld.GetWorld(worldID);
if (world != null && world.IsAlive(e, gen))
{
world.DelEntity(e);
}
//if (_deleteEntiityWithDestroy && ent.TryUnpack(out int id, out EcsWorld world))
//{
// world.DelEntity(id);
//}
}
#endregion
#region Other
public static EcsEntityConnect GetConnectByInstanceID(int instanceID)
{
return _connectedEntities[instanceID];
}
public static bool TryGetConnectByInstanceID(int instanceID, out EcsEntityConnect conncet)
{
return _connectedEntities.TryGetValue(instanceID, out conncet);
} }
#endregion #endregion

View File

@ -82,7 +82,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{ {
if(property.propertyType == SerializedPropertyType.ManagedReference == false) if (property.propertyType == SerializedPropertyType.ManagedReference == false)
{ {
return EditorGUI.GetPropertyHeight(property, label); return EditorGUI.GetPropertyHeight(property, label);
} }

View File

@ -71,6 +71,7 @@ namespace DCFApixels.DragonECS.Unity.Editors
return; return;
} }
} }
int index = componentsProp.arraySize; int index = componentsProp.arraySize;
componentsProp.InsertArrayElementAtIndex(index); componentsProp.InsertArrayElementAtIndex(index);
componentsProp.GetArrayElementAtIndex(index).managedReferenceValue = ((IComponentTemplate)obj).Clone(); componentsProp.GetArrayElementAtIndex(index).managedReferenceValue = ((IComponentTemplate)obj).Clone();
@ -107,7 +108,6 @@ namespace DCFApixels.DragonECS.Unity.Editors
{ {
DrawComponentData(componentsProp.GetArrayElementAtIndex(i), componentsProp.arraySize, i); DrawComponentData(componentsProp.GetArrayElementAtIndex(i), componentsProp.arraySize, i);
} }
GUILayout.EndVertical(); GUILayout.EndVertical();
} }
private void DrawTop(ITemplateInternal target) private void DrawTop(ITemplateInternal target)