EntitySystem

EntitySystem
This commit is contained in:
ALEXTANG
2022-08-08 17:01:52 +08:00
parent 5d7b4e20e9
commit 464d1b230c
8 changed files with 86 additions and 25 deletions

View File

@@ -1,33 +1,47 @@
using TEngine.EntityModule; using TEngine.EntityModule;
using UnityEngine; using UnityEngine;
public class EcsDemoApp : MonoBehaviour namespace TEngine
{ {
public GameObject @object; public class EcsDemoApp : MonoBehaviour
void Start()
{ {
var entity = Entity.Create<Entity>(); public GameObject @object;
GameObjectCmpt actor = entity.AddComponent<GameObjectCmpt>();
actor.Name = typeof(GameObjectCmpt).ToString();
actor.gameObject = Instantiate(@object);
actor.transform = actor.gameObject.GetComponent<Transform>();
entity.AddComponent<EntityComponent>();
entity.CheckDebugInfo(actor.gameObject);
Debug.Log(entity.ToString());
var entity2 = Entity.Create<Entity>(); void Start()
GameObjectCmpt actor2 = entity2.AddComponent<GameObjectCmpt>(); {
actor2.Name = typeof(GameObjectCmpt).ToString(); var entity = Entity.Create<Entity>();
actor2.gameObject = Instantiate(@object); entity.AddComponent<TestUpdateCmpt>();
actor2.transform = actor2.gameObject.GetComponent<Transform>(); GameObjectCmpt actor = entity.AddComponent<GameObjectCmpt>();
entity2.AddComponent<EntityComponent>(); actor.Name = typeof(GameObjectCmpt).ToString();
entity2.CheckDebugInfo(actor2.gameObject); actor.gameObject = Instantiate(@object);
Debug.Log(entity2.ToString()); actor.transform = actor.gameObject.GetComponent<Transform>();
} entity.AddComponent<EntityComponent>();
entity.CheckDebugInfo(actor.gameObject);
Debug.Log(entity.ToString());
void Update() var entity2 = Entity.Create<Entity>();
{ GameObjectCmpt actor2 = entity2.AddComponent<GameObjectCmpt>();
EntitySystem.Instance.Update(); actor2.Name = typeof(GameObjectCmpt).ToString();
actor2.gameObject = Instantiate(@object);
actor2.transform = actor2.gameObject.GetComponent<Transform>();
entity2.AddComponent<EntityComponent>();
entity2.CheckDebugInfo(actor2.gameObject);
Debug.Log(entity2.ToString());
TimerMgr.Instance.AddTimer((args =>
{
Entity.Destroy(entity);
Entity.Destroy(entity2);
}), 3f,false);
}
void Update()
{
EntitySystem.Instance.Update();
}
} }
} }

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TEngine;
using TEngine.EntityModule;
public class TestUpdateCmpt : EntityComponent,IUpdate
{
public void Update()
{
TLogger.LogInfo("update");
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bb79036f03a4ecc4f9e5445e185abe50
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -60,6 +60,9 @@ namespace TEngine.EntityModule
{ {
if (ecsObject is EntityComponent entityComponent) if (ecsObject is EntityComponent entityComponent)
{ {
#if UNITY_EDITOR
TLogger.LogWarning($"Destroy ID {entityComponent.InstanceId} EntityComponent{entityComponent.ToString()},reuse {reuse}");
#endif
entityComponent.Entity.Components.Remove(entityComponent); entityComponent.Entity.Components.Remove(entityComponent);
if (entityComponent is IUpdate update) if (entityComponent is IUpdate update)
{ {
@@ -81,6 +84,9 @@ namespace TEngine.EntityModule
} }
else if (ecsObject is Entity entity) else if (ecsObject is Entity entity)
{ {
#if UNITY_EDITOR
TLogger.LogWarning($"Destroy ID {entity.InstanceId} Entity{entity.ToString()},reuse {reuse}");
#endif
entity.System.RemoveEntity(entity); entity.System.RemoveEntity(entity);
entity.OnDestroy(); entity.OnDestroy();
while (entity.Components.Count > 0) while (entity.Components.Count > 0)

View File

@@ -165,6 +165,9 @@ namespace TEngine.EntityModule
public static T Create<T>() where T : Entity, new() public static T Create<T>() where T : Entity, new()
{ {
var entity = EntitySystem.Instance.Create<T>(); var entity = EntitySystem.Instance.Create<T>();
#if UNITY_EDITOR
TLogger.LogWarning($"Create ID {entity.InstanceId} EntityComponent{entity.ToString()}");
#endif
return entity; return entity;
} }
#endregion #endregion

View File

@@ -12,6 +12,9 @@
public static T Create<T>() where T : EntityComponent, new() public static T Create<T>() where T : EntityComponent, new()
{ {
var entity = EntitySystem.Instance.CreateComponent<T>(); var entity = EntitySystem.Instance.CreateComponent<T>();
#if UNITY_EDITOR
TLogger.LogInfoSuccessd($"Create ID {entity.InstanceId} EntityComponent{ entity.ToString()}");
#endif
return entity; return entity;
} }
#endregion #endregion

View File

@@ -26,6 +26,7 @@ namespace TEngine.EntityModule
} }
private EntitySystem() private EntitySystem()
{ {
Update(true);
} }
/// <summary> /// <summary>
/// Key -> HashSet(type) / Value -> Stack(EcsObject) /// Key -> HashSet(type) / Value -> Stack(EcsObject)

View File

@@ -8,5 +8,13 @@
public string Name; public string Name;
public UnityEngine.GameObject gameObject; public UnityEngine.GameObject gameObject;
public UnityEngine.Transform transform; public UnityEngine.Transform transform;
public override void OnDestroy()
{
if (gameObject != null)
{
UnityEngine.GameObject.Destroy(gameObject);
}
}
} }
} }