mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
EntitySystem
EntitySystem
This commit is contained in:
@@ -1,33 +1,47 @@
|
||||
using TEngine.EntityModule;
|
||||
using UnityEngine;
|
||||
|
||||
public class EcsDemoApp : MonoBehaviour
|
||||
namespace TEngine
|
||||
{
|
||||
public GameObject @object;
|
||||
|
||||
void Start()
|
||||
public class EcsDemoApp : MonoBehaviour
|
||||
{
|
||||
var entity = Entity.Create<Entity>();
|
||||
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());
|
||||
public GameObject @object;
|
||||
|
||||
var entity2 = Entity.Create<Entity>();
|
||||
GameObjectCmpt actor2 = entity2.AddComponent<GameObjectCmpt>();
|
||||
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());
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
var entity = Entity.Create<Entity>();
|
||||
entity.AddComponent<TestUpdateCmpt>();
|
||||
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());
|
||||
|
||||
void Update()
|
||||
{
|
||||
EntitySystem.Instance.Update();
|
||||
var entity2 = Entity.Create<Entity>();
|
||||
GameObjectCmpt actor2 = entity2.AddComponent<GameObjectCmpt>();
|
||||
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 UNITY_EDITOR
|
||||
TLogger.LogWarning($"Destroy ID {entityComponent.InstanceId} EntityComponent{entityComponent.ToString()},reuse {reuse}");
|
||||
#endif
|
||||
entityComponent.Entity.Components.Remove(entityComponent);
|
||||
if (entityComponent is IUpdate update)
|
||||
{
|
||||
@@ -81,6 +84,9 @@ namespace TEngine.EntityModule
|
||||
}
|
||||
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.OnDestroy();
|
||||
while (entity.Components.Count > 0)
|
||||
|
@@ -165,6 +165,9 @@ namespace TEngine.EntityModule
|
||||
public static T Create<T>() where T : Entity, new()
|
||||
{
|
||||
var entity = EntitySystem.Instance.Create<T>();
|
||||
#if UNITY_EDITOR
|
||||
TLogger.LogWarning($"Create ID {entity.InstanceId} EntityComponent{entity.ToString()}");
|
||||
#endif
|
||||
return entity;
|
||||
}
|
||||
#endregion
|
||||
|
@@ -12,6 +12,9 @@
|
||||
public static T Create<T>() where T : EntityComponent, new()
|
||||
{
|
||||
var entity = EntitySystem.Instance.CreateComponent<T>();
|
||||
#if UNITY_EDITOR
|
||||
TLogger.LogInfoSuccessd($"Create ID {entity.InstanceId} EntityComponent{ entity.ToString()}");
|
||||
#endif
|
||||
return entity;
|
||||
}
|
||||
#endregion
|
||||
|
@@ -26,6 +26,7 @@ namespace TEngine.EntityModule
|
||||
}
|
||||
private EntitySystem()
|
||||
{
|
||||
Update(true);
|
||||
}
|
||||
/// <summary>
|
||||
/// Key -> HashSet(type) / Value -> Stack(EcsObject)
|
||||
|
@@ -8,5 +8,13 @@
|
||||
public string Name;
|
||||
public UnityEngine.GameObject gameObject;
|
||||
public UnityEngine.Transform transform;
|
||||
|
||||
public override void OnDestroy()
|
||||
{
|
||||
if (gameObject != null)
|
||||
{
|
||||
UnityEngine.GameObject.Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user