Entitas
This commit is contained in:
ALEXTANG
2023-07-17 15:28:27 +08:00
parent 31d4d6c0b8
commit a273e9d5f8
64 changed files with 994 additions and 13 deletions

View File

@@ -0,0 +1,15 @@
#if UNITY_EDITOR
using UnityEngine;
namespace TEngine
{
public class ComponentView: MonoBehaviour
{
public Entity Component
{
get;
set;
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 24bef9ebd1ca4a1cb8db0fba17ec5b2b
timeCreated: 1689576818

View File

@@ -28,6 +28,11 @@ namespace TEngine
public abstract class Entity : IDisposable
{
#if UNITY_EDITOR
[UnityEngine.HideInInspector]
public UnityEngine.GameObject ViewGO;
#endif
#region Status
[BsonIgnore]
[IgnoreDataMember]
@@ -107,13 +112,7 @@ namespace TEngine
[BsonIgnore]
[IgnoreDataMember]
protected virtual string ViewName
{
get
{
return this.GetType().FullName;
}
}
protected virtual string ViewName => this.GetType().Name;
#endregion
@@ -222,8 +221,17 @@ namespace TEngine
EntitiesSystem.Instance.Awake(entity);
EntitiesSystem.Instance.StartUpdate(entity);
}
entity.IsCreated = true;
entity.IsNew = true;
entity.OnCreate();
#if UNITY_EDITOR
entity.ViewGO = new UnityEngine.GameObject(entity.ViewName);
entity.ViewGO.AddComponent<ComponentView>().Component = entity;
entity.ViewGO.transform.SetParent(entity.Parent == null?
UnityEngine.GameObject.Find("[EntitySystem]").transform : entity.Parent.ViewGO.transform);
#endif
return entity;
}
@@ -246,6 +254,13 @@ namespace TEngine
}
entity.IsCreated = true;
entity.IsNew = true;
entity.OnCreate();
#if UNITY_EDITOR
entity.ViewGO = new UnityEngine.GameObject(entity.ViewName);
entity.ViewGO.AddComponent<ComponentView>().Component = entity;
entity.ViewGO.transform.SetParent(entity.Parent == null?
UnityEngine.GameObject.Find("[EntitySystem]").transform : entity.Parent.ViewGO.transform);
#endif
return entity;
}
@@ -375,6 +390,15 @@ namespace TEngine
component.Parent = this;
component.Scene = Scene;
component.IsComponent = true;
#if UNITY_EDITOR
if (component.ViewGO == null)
{
Log.Error($"{component} s component.ViewGO is null");
return;
}
component.ViewGO.transform.SetParent(component.Parent == null?
UnityEngine.GameObject.Find("[EntitySystem]").transform : component.Parent.ViewGO.transform);
#endif
}
#endregion
@@ -570,6 +594,10 @@ namespace TEngine
#endregion
#region OnCreate
public virtual void OnCreate() { }
#endregion
#region Dispose
public virtual void Dispose()
@@ -638,6 +666,15 @@ namespace TEngine
Entities.Remove(runtimeId);
Scene = null;
Return(this);
#if UNITY_EDITOR
if (this.ViewGO == null)
{
Log.Error($"{this} s ViewGO is null");
return;
}
UnityEngine.Object.Destroy(this.ViewGO);;
#endif
}
#endregion

View File

@@ -1,8 +1,12 @@
#if TENGINE_NET
using System.ComponentModel;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Conventions;
using TrueSync;
using Unity.Mathematics;
using MongoHelper = TEngine.Core.MongoHelper;
namespace TEngine.Core;
@@ -15,10 +19,21 @@ public sealed class MongoHelper : Singleton<MongoHelper>
// 自动注册IgnoreExtraElements
var conventionPack = new ConventionPack {new IgnoreExtraElementsConvention(true)};
ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true);
BsonSerializer.TryRegisterSerializer(typeof(float2), new StructBsonSerialize<float2>());
BsonSerializer.TryRegisterSerializer(typeof(float3), new StructBsonSerialize<float3>());
BsonSerializer.TryRegisterSerializer(typeof(float4), new StructBsonSerialize<float4>());
BsonSerializer.TryRegisterSerializer(typeof(quaternion), new StructBsonSerialize<quaternion>());
RegisterStruct<float2>();
RegisterStruct<float3>();
RegisterStruct<float4>();
RegisterStruct<quaternion>();
RegisterStruct<FP>();
RegisterStruct<TSVector>();
RegisterStruct<TSVector2>();
RegisterStruct<TSVector4>();
RegisterStruct<TSQuaternion>();
}
public static void RegisterStruct<T>() where T : struct
{
BsonSerializer.RegisterSerializer(typeof (T), new StructBsonSerialize<T>());
}
protected override void OnLoad(int assemblyName)
@@ -43,6 +58,43 @@ public sealed class MongoHelper : Singleton<MongoHelper>
}
});
}
private static readonly JsonWriterSettings defaultSettings = new() { OutputMode = JsonOutputMode.RelaxedExtendedJson };
public static string ToJson(object obj)
{
if (obj is ISupportInitialize supportInitialize)
{
supportInitialize.BeginInit();
}
return obj.ToJson(defaultSettings);
}
public static string ToJson(object obj, JsonWriterSettings settings)
{
if (obj is ISupportInitialize supportInitialize)
{
supportInitialize.BeginInit();
}
return obj.ToJson(settings);
}
public static T FromJson<T>(string str)
{
try
{
return BsonSerializer.Deserialize<T>(str);
}
catch (Exception e)
{
throw new Exception($"{str}\n{e}");
}
}
public static object FromJson(Type type, string str)
{
return BsonSerializer.Deserialize(str, type);
}
public T Deserialize<T>(byte[] bytes)
{

View File

@@ -13,7 +13,7 @@ namespace TEngine
{
// 初始化框架
GameContext.Init();
new GameObject("[TEngine.Unity]").AddComponent<GameSystem>();
new GameObject("[EntitySystem]").AddComponent<GameSystem>();
// 框架需要一个Scene来驱动、所以要创建一个Scene、后面所有的框架都会在这个Scene下
// 也就是把这个Scene给卸载掉、框架的东西都会清除掉
return Scene.Create("Unity");