mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
EntitySystem
EntitySystem
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
15
Assets/TEngine/Runtime/Entity/Demo/TestUpdateCmpt.cs
Normal file
15
Assets/TEngine/Runtime/Entity/Demo/TestUpdateCmpt.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
11
Assets/TEngine/Runtime/Entity/Demo/TestUpdateCmpt.cs.meta
Normal file
11
Assets/TEngine/Runtime/Entity/Demo/TestUpdateCmpt.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: bb79036f03a4ecc4f9e5445e185abe50
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
@@ -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)
|
||||||
|
@@ -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
|
||||||
|
@@ -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
|
||||||
|
@@ -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)
|
||||||
|
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user