Просмотр исходного кода

让Component的id不进行序列化

tanghai 8 лет назад
Родитель
Сommit
129aae9342

+ 8 - 8
Config/StartConfig/127.0.0.1.txt

@@ -1,8 +1,8 @@
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10000, "Host2" : null }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("98565862653976"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("98565862653977"), "Host" : "127.0.0.1", "Port" : 10001 }], "AppId" : 2, "AppType" : "Location", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20001 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10002, "Host2" : null }], "AppId" : 3, "AppType" : "Realm", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20002 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10003, "Host2" : null }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20003 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10004, "Host2" : null }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20004 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10005, "Host2" : null }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "127.0.0.1", "Port" : 20005 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "127.0.0.1", "Port" : 10006, "Host2" : null }], "AppId" : 7, "AppType" : "Gate", "ServerIP" : "*" }
-{ "_t" : "StartConfig", "_id" : NumberLong("98565723062316"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("98565723062317"), "Host" : "127.0.0.1", "Port" : 20006 }], "AppId" : 8, "AppType" : "Map", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10000, "Host2" : null }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("98565862653976"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 10001 }], "AppId" : 2, "AppType" : "Location", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20001 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10002, "Host2" : null }], "AppId" : 3, "AppType" : "Realm", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20002 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10003, "Host2" : null }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20003 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10004, "Host2" : null }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20004 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10005, "Host2" : null }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20005 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10006, "Host2" : null }], "AppId" : 7, "AppType" : "Gate", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("98565723062316"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20006 }], "AppId" : 8, "AppType" : "Map", "ServerIP" : "*" }

+ 6 - 6
Config/StartConfig/192.168.12.112.txt

@@ -1,6 +1,6 @@
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.12.112", "Port" : 20000 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.12.188", "Port" : 10000 }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "192.168.12.112" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.12.112", "Port" : 20001 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.12.188", "Port" : 10001 }], "AppId" : 2, "AppType" : "Realm", "ServerIP" : "192.168.12.112" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.12.112", "Port" : 20002 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.12.188", "Port" : 10002 }], "AppId" : 3, "AppType" : "Gate", "ServerIP" : "192.168.12.112" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.12.112", "Port" : 20003 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.12.188", "Port" : 10003 }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "192.168.12.112" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.12.112", "Port" : 20004 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.12.188", "Port" : 10004 }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "192.168.12.112" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.12.112", "Port" : 20005 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.12.188", "Port" : 10005 }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "192.168.12.112" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.12.112", "Port" : 20000 }, { "_t" : "OuterConfig", "Host" : "192.168.12.188", "Port" : 10000, "Host2" : null }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "192.168.12.112" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.12.112", "Port" : 20001 }, { "_t" : "OuterConfig", "Host" : "192.168.12.188", "Port" : 10001, "Host2" : null }], "AppId" : 2, "AppType" : "Realm", "ServerIP" : "192.168.12.112" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.12.112", "Port" : 20002 }, { "_t" : "OuterConfig", "Host" : "192.168.12.188", "Port" : 10002, "Host2" : null }], "AppId" : 3, "AppType" : "Gate", "ServerIP" : "192.168.12.112" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.12.112", "Port" : 20003 }, { "_t" : "OuterConfig", "Host" : "192.168.12.188", "Port" : 10003, "Host2" : null }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "192.168.12.112" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.12.112", "Port" : 20004 }, { "_t" : "OuterConfig", "Host" : "192.168.12.188", "Port" : 10004, "Host2" : null }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "192.168.12.112" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.12.112", "Port" : 20005 }, { "_t" : "OuterConfig", "Host" : "192.168.12.188", "Port" : 10005, "Host2" : null }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "192.168.12.112" }

+ 6 - 6
Config/StartConfig/192.168.9.182.txt

@@ -1,6 +1,6 @@
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.9.182", "Port" : 20000 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.9.182", "Port" : 10000 }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "192.168.9.182" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.9.182", "Port" : 20001 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.9.182", "Port" : 10001 }], "AppId" : 2, "AppType" : "Realm", "ServerIP" : "192.168.9.182" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.9.182", "Port" : 20002 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.9.182", "Port" : 10002 }], "AppId" : 3, "AppType" : "Gate", "ServerIP" : "192.168.9.182" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.9.182", "Port" : 20003 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.9.182", "Port" : 10003 }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "192.168.9.182" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.9.182", "Port" : 20004 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.9.182", "Port" : 10004 }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "192.168.9.182" }
-{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "_id" : NumberLong("96832366182402"), "Host" : "192.168.9.182", "Port" : 20005 }, { "_t" : "OuterConfig", "_id" : NumberLong("96832366182403"), "Host" : "192.168.9.182", "Port" : 10005 }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "192.168.9.182" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.9.182", "Port" : 20000 }, { "_t" : "OuterConfig", "Host" : "192.168.9.182", "Port" : 10000, "Host2" : null }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "192.168.9.182" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.9.182", "Port" : 20001 }, { "_t" : "OuterConfig", "Host" : "192.168.9.182", "Port" : 10001, "Host2" : null }], "AppId" : 2, "AppType" : "Realm", "ServerIP" : "192.168.9.182" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.9.182", "Port" : 20002 }, { "_t" : "OuterConfig", "Host" : "192.168.9.182", "Port" : 10002, "Host2" : null }], "AppId" : 3, "AppType" : "Gate", "ServerIP" : "192.168.9.182" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.9.182", "Port" : 20003 }, { "_t" : "OuterConfig", "Host" : "192.168.9.182", "Port" : 10003, "Host2" : null }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "192.168.9.182" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.9.182", "Port" : 20004 }, { "_t" : "OuterConfig", "Host" : "192.168.9.182", "Port" : 10004, "Host2" : null }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "192.168.9.182" }
+{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "192.168.9.182", "Port" : 20005 }, { "_t" : "OuterConfig", "Host" : "192.168.9.182", "Port" : 10005, "Host2" : null }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "192.168.9.182" }

+ 1 - 1
Config/StartConfig/LocalAllServer.txt

@@ -1 +1 @@
-{ "_t" : "StartConfig", "_id" : NumberLong("98547768819754"), "components" : [{ "_t" : "OuterConfig", "_id" : NumberLong("98547768819755"), "Host" : "127.0.0.1", "Port" : 10002, "Host2" : null }, { "_t" : "InnerConfig", "_id" : NumberLong("98547768819756"), "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "HttpConfig", "_id" : NumberLong("98547768819757"), "Url" : "", "AppId" : 0, "AppKey" : "", "ManagerSystemUrl" : "" }, { "_t" : "DBConfig", "_id" : NumberLong("98547768819758"), "ConnectionString" : null, "DBName" : null }], "AppId" : 1, "AppType" : "AllServer", "ServerIP" : "*" }
+{ "_t" : "StartConfig", "_id" : NumberLong("98547768819754"), "components" : [{ "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10002, "Host2" : null }, { "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "HttpConfig", "Url" : "", "AppId" : 0, "AppKey" : "", "ManagerSystemUrl" : "" }, { "_t" : "DBConfig", "ConnectionString" : null, "DBName" : null }], "AppId" : 1, "AppType" : "AllServer", "ServerIP" : "*" }

+ 3 - 3
Server/Model/Entity/DBEntity.cs → Server/Model/Entity/EntityDB.cs

@@ -6,13 +6,13 @@ namespace Model
 	[BsonKnownTypes(typeof(RechargeRecord))]
 	[BsonKnownTypes(typeof(Recharge))]
 	[BsonKnownTypes(typeof(Location))]
-	public class DBEntity: Entity
+	public class EntityDB: Entity
 	{
-		protected DBEntity()
+		protected EntityDB()
 		{
 		}
 
-		protected DBEntity(long id): base(id)
+		protected EntityDB(long id): base(id)
 		{
 		}
 	}

+ 2 - 2
Server/Model/Entity/Http.cs

@@ -1,7 +1,7 @@
 namespace Model
 {
 	// 充值流水
-	public sealed class RechargeRecord : DBEntity
+	public sealed class RechargeRecord : EntityDB
 	{
 		// 充值玩家
 		public int PlayerNO { get; set; }
@@ -18,7 +18,7 @@
 	}
 
 	// 保存玩家充值记录, 每个玩家只有一条
-	public sealed class Recharge : DBEntity
+	public sealed class Recharge : EntityDB
 	{
 		public int CardNumber { get; set; }
 		

+ 1 - 1
Server/Model/Entity/Location.cs

@@ -1,6 +1,6 @@
 namespace Model
 {
-	public class Location: DBEntity
+	public class Location: EntityDB
 	{
 		public string Address;
 

+ 1 - 1
Server/Model/Server.Model.csproj

@@ -178,7 +178,7 @@
     <Compile Include="Entity\Config\StartConfig.cs" />
     <Compile Include="Entity\ActorProxy.cs" />
     <Compile Include="Entity\SceneEntity.cs" />
-    <Compile Include="Entity\DBEntity.cs" />
+    <Compile Include="Entity\EntityDB.cs" />
     <Compile Include="Entity\DBTask.cs" />
     <Compile Include="Entity\DBTaskQueue.cs" />
     <Compile Include="Entity\Game.cs" />

+ 7 - 5
Unity/Assets/Scripts/Base/Object/Component.cs

@@ -2,9 +2,14 @@
 
 namespace Model
 {
+	[BsonIgnoreExtraElements]
 	[BsonKnownTypes(typeof(AConfigComponent))]
 	public abstract class Component: Disposer
 	{
+		[BsonId]
+		[BsonIgnore]
+		public sealed override long Id { get; set; }
+
 		[BsonIgnore]
 		public Entity Entity { get; set; }
 
@@ -15,12 +20,9 @@ namespace Model
 
 		protected Component()
 		{
+			this.Id = IdGenerater.GenerateId();
 		}
-
-		protected Component(long id): base(id)
-		{
-		}
-
+		
 		public T GetComponent<T>() where T : Component
 		{
 			return this.Entity.GetComponent<T>();

+ 5 - 14
Unity/Assets/Scripts/Base/Object/Disposer.cs

@@ -1,15 +1,14 @@
 using System;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
 	public abstract class Disposer : Object, IDisposable
 	{
-		protected Disposer() : base(IdGenerater.GenerateId())
-		{
-			ObjectEvents.Instance.Add(this);
-		}
-
-		protected Disposer(long id) : base(id)
+		[BsonIgnore]
+		public abstract long Id { get; set; }
+		
+		protected Disposer()
 		{
 			ObjectEvents.Instance.Add(this);
 		}
@@ -18,13 +17,5 @@ namespace Model
 		{
 			this.Id = 0;
 		}
-
-		public override void BeginInit()
-		{
-		}
-
-		public override void EndInit()
-		{
-		}
 	}
 }

+ 13 - 10
Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -1,17 +1,21 @@
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
-	public class Entity : Disposer
+	[BsonIgnoreExtraElements]
+	public class Entity : Disposer, ISupportInitialize
 	{
+		[BsonId]
+		[BsonElement(Order = 1)]
+		public sealed override long Id { get; set; }
+
 		[BsonIgnore]
 		public Entity Parent { get; set; }
-
-		public EntityType Type { get; set; }
-
+		
 		[BsonElement]
 		[BsonIgnoreIfNull]
 		private HashSet<Component> components = new HashSet<Component>();
@@ -21,10 +25,12 @@ namespace Model
 
 		protected Entity()
 		{
+			this.Id = IdGenerater.GenerateId();
 		}
 
-		protected Entity(long id) : base(id)
+		protected Entity(long id)
 		{
+			this.Id = id;
 		}
 
 		public override void Dispose()
@@ -186,17 +192,14 @@ namespace Model
 			return this.componentDict.Values.ToArray();
 		}
 
-		public override void BeginInit()
+		public virtual void BeginInit()
 		{
-			base.BeginInit();
 			this.components = new HashSet<Component>();
 			this.componentDict = new Dictionary<Type, Component>();
 		}
 
-		public override void EndInit()
+		public virtual void EndInit()
 		{
-			base.EndInit();
-
 			ObjectEvents.Instance.Add(this);
 
 			if (this.components != null && this.components.Count == 0)

+ 2 - 23
Unity/Assets/Scripts/Base/Object/Object.cs

@@ -1,27 +1,6 @@
-using System.ComponentModel;
-
-namespace Model
+namespace Model
 {
-	public abstract class Object: ISupportInitialize
+	public abstract class Object
 	{
-		public long Id { get; set; }
-
-		protected Object()
-		{
-			Id = IdGenerater.GenerateId();
-		}
-
-		protected Object(long id)
-		{
-			this.Id = id;
-		}
-
-		public virtual void BeginInit()
-		{
-		}
-
-		public virtual void EndInit()
-		{
-		}
 	}
 }

+ 16 - 11
Unity/Hotfix/Base/Object/Component.cs

@@ -1,25 +1,30 @@
-namespace Hotfix
+using Model;
+using MongoDB.Bson.Serialization.Attributes;
+
+namespace Hotfix
 {
-	public abstract class Component: Disposer
+	public abstract class Component : Disposer
 	{
-		public Entity Owner { get; set; }
+		[BsonId]
+		[BsonIgnore]
+		public sealed override long Id { get; set; }
 
-		public T GetOwner<T>() where T : Entity
-		{
-			return this.Owner as T;
-		}
+		[BsonIgnore]
+		public Entity Entity { get; set; }
 
-		protected Component()
+		public T GetEntity<T>() where T : Entity
 		{
+			return this.Entity as T;
 		}
 
-		protected Component(long id): base(id)
+		protected Component()
 		{
+			this.Id = IdGenerater.GenerateId();
 		}
 
 		public T GetComponent<T>() where T : Component
 		{
-			return this.Owner.GetComponent<T>();
+			return this.Entity.GetComponent<T>();
 		}
 
 		public override void Dispose()
@@ -31,7 +36,7 @@
 
 			base.Dispose();
 
-			this.Owner.RemoveComponent(this.GetType());
+			this.Entity?.RemoveComponent(this.GetType());
 		}
 	}
 }

+ 4 - 4
Unity/Hotfix/Base/Object/ComponentFactory.cs

@@ -7,7 +7,7 @@ namespace Hotfix
 		public static T Create<T>(Entity entity) where T : Component
 		{
 			T disposer = (T)Activator.CreateInstance(typeof(T));
-			disposer.Owner = entity;
+			disposer.Entity = entity;
 			ObjectEvents.Instance.Awake(disposer);
 			return disposer;
 		}
@@ -15,7 +15,7 @@ namespace Hotfix
 		public static T Create<T, A>(Entity entity, A a) where T : Component
 		{
 			T disposer = (T)Activator.CreateInstance(typeof(T));
-			disposer.Owner = entity;
+			disposer.Entity = entity;
 			ObjectEvents.Instance.Awake(disposer, a);
 			return disposer;
 		}
@@ -23,7 +23,7 @@ namespace Hotfix
 		public static T Create<T, A, B>(Entity entity, A a, B b) where T : Component
 		{
 			T disposer = (T)Activator.CreateInstance(typeof(T));
-			disposer.Owner = entity;
+			disposer.Entity = entity;
 			ObjectEvents.Instance.Awake(disposer, a, b);
 			return disposer;
 		}
@@ -31,7 +31,7 @@ namespace Hotfix
 		public static T Create<T, A, B, C>(Entity entity, A a, B b, C c) where T : Component
 		{
 			T disposer = (T)Activator.CreateInstance(typeof(T));
-			disposer.Owner = entity;
+			disposer.Entity = entity;
 			ObjectEvents.Instance.Awake(disposer, a, b, c);
 			return disposer;
 		}

+ 6 - 8
Unity/Hotfix/Base/Object/Disposer.cs

@@ -1,20 +1,18 @@
-using Model;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Hotfix
 {
-	public abstract class Disposer: Object
+	public abstract class Disposer : Object, IDisposable2
 	{
-		protected Disposer(): base(IdGenerater.GenerateId())
-		{
-			ObjectEvents.Instance.Add(this);
-		}
+		[BsonIgnore]
+		public abstract long Id { get; set; }
 
-		protected Disposer(long id): base(id)
+		protected Disposer()
 		{
 			ObjectEvents.Instance.Add(this);
 		}
 
-		public override void Dispose()
+		public virtual void Dispose()
 		{
 			this.Id = 0;
 		}

+ 34 - 4
Unity/Hotfix/Base/Object/Entity.cs

@@ -1,18 +1,22 @@
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
 using Model;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace Hotfix
 {
-	public class Entity : Disposer
+	[BsonIgnoreExtraElements]
+	public class Entity : Disposer, ISupportInitialize
 	{
+		[BsonId]
+		[BsonElement(Order = 1)]
+		public sealed override long Id { get; set; }
+
 		[BsonIgnore]
 		public Entity Parent { get; set; }
 
-		public EntityType Type { get; set; }
-
 		[BsonElement]
 		[BsonIgnoreIfNull]
 		private HashSet<Component> components = new HashSet<Component>();
@@ -22,10 +26,12 @@ namespace Hotfix
 
 		protected Entity()
 		{
+			this.Id = IdGenerater.GenerateId();
 		}
 
-		protected Entity(long id) : base(id)
+		protected Entity(long id)
 		{
+			this.Id = id;
 		}
 
 		public override void Dispose()
@@ -186,5 +192,29 @@ namespace Hotfix
 		{
 			return this.componentDict.Values.ToArray();
 		}
+
+		public virtual void BeginInit()
+		{
+			this.components = new HashSet<Component>();
+			this.componentDict = new Dictionary<Type, Component>();
+		}
+
+		public virtual void EndInit()
+		{
+			ObjectEvents.Instance.Add(this);
+
+			if (this.components != null && this.components.Count == 0)
+			{
+				this.components = null;
+			}
+			if (this.components != null)
+			{
+				foreach (Component component in this.components)
+				{
+					component.Entity = this;
+					this.componentDict.Add(component.GetType(), component);
+				}
+			}
+		}
 	}
 }

+ 2 - 19
Unity/Hotfix/Base/Object/Object.cs

@@ -1,28 +1,11 @@
-using Model;
-
-namespace Hotfix
+namespace Hotfix
 {
 	public interface IDisposable2
 	{
 		void Dispose();
 	}
 
-	public abstract class Object: IDisposable2
+	public abstract class Object
 	{
-		public long Id { get; set; }
-
-		protected Object()
-		{
-			Id = IdGenerater.GenerateId();
-		}
-
-		protected Object(long id)
-		{
-			this.Id = id;
-		}
-
-		public virtual void Dispose()
-		{
-		}
 	}
 }

+ 1 - 1
Unity/Hotfix/Component/UIComponent.cs

@@ -91,7 +91,7 @@ namespace Hotfix
 		{
 			try
 			{
-				UI ui = UiTypes[type].Create(GetOwner<Scene>(), type, Root);
+				UI ui = UiTypes[type].Create(this.GetEntity<Scene>(), type, Root);
                 uis.Add(type, ui);
 
 				// 设置canvas

+ 1 - 1
Unity/Hotfix/UI/UILogin/Component/UILoginComponent.cs

@@ -21,7 +21,7 @@ namespace Hotfix
 
 		public void Awake()
 		{
-			ReferenceCollector rc = this.GetOwner<UI>().GameObject.GetComponent<ReferenceCollector>();
+			ReferenceCollector rc = this.GetEntity<UI>().GameObject.GetComponent<ReferenceCollector>();
 			loginBtn = rc.Get<GameObject>("LoginBtn");
 			loginBtn.GetComponent<Button>().onClick.Add(OnLogin);