Parcourir la source

Entity增加一个挂载已实例化的组件对象的方法

tanghai il y a 7 ans
Parent
commit
4962e5dc0b

+ 32 - 11
Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -51,6 +51,22 @@ namespace ETModel
 			this.components.Clear();
 			this.componentDict.Clear();
 		}
+		
+		public Component AddComponent(Component component)
+		{
+			Type type = component.GetType();
+			if (this.componentDict.ContainsKey(type))
+			{
+				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {type.Name}");
+			}
+
+			if (component is ISerializeToEntity)
+			{
+				this.components.Add(component);
+			}
+			this.componentDict.Add(type, component);
+			return component;
+		}
 
 		public Component AddComponent(Type type)
 		{
@@ -65,13 +81,14 @@ namespace ETModel
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public K AddComponent<K>() where K : Component, new()
 		{
-			if (this.componentDict.ContainsKey(typeof(K)))
+			Type type = typeof (K);
+			if (this.componentDict.ContainsKey(type))
 			{
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
@@ -82,13 +99,14 @@ namespace ETModel
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public K AddComponent<K, P1>(P1 p1) where K : Component, new()
 		{
-			if (this.componentDict.ContainsKey(typeof(K)))
+			Type type = typeof (K);
+			if (this.componentDict.ContainsKey(type))
 			{
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
@@ -99,13 +117,14 @@ namespace ETModel
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public K AddComponent<K, P1, P2>(P1 p1, P2 p2) where K : Component, new()
 		{
-			if (this.componentDict.ContainsKey(typeof(K)))
+			Type type = typeof (K);
+			if (this.componentDict.ContainsKey(type))
 			{
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
@@ -116,13 +135,14 @@ namespace ETModel
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public K AddComponent<K, P1, P2, P3>(P1 p1, P2 p2, P3 p3) where K : Component, new()
 		{
-			if (this.componentDict.ContainsKey(typeof(K)))
+			Type type = typeof (K);
+			if (this.componentDict.ContainsKey(type))
 			{
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
@@ -133,20 +153,21 @@ namespace ETModel
 			{
 				this.components.Add(component);
 			}
-			this.componentDict.Add(component.GetType(), component);
+			this.componentDict.Add(type, component);
 			return component;
 		}
 
 		public void RemoveComponent<K>() where K : Component
 		{
+			Type type = typeof (K);
 			Component component;
-			if (!this.componentDict.TryGetValue(typeof(K), out component))
+			if (!this.componentDict.TryGetValue(type, out component))
 			{
 				return;
 			}
 
 			this.components.Remove(component);
-			this.componentDict.Remove(typeof(K));
+			this.componentDict.Remove(type);
 
 			component.Dispose();
 		}

+ 1 - 1
Unity/Assets/Scripts/Module/Message/Network/AChannel.cs

@@ -29,7 +29,7 @@ namespace ETModel
 
 		public IPEndPoint RemoteAddress { get; protected set; }
 
-		private event Action<AChannel, SocketError> errorCallback;
+		private Action<AChannel, SocketError> errorCallback;
 
 		public event Action<AChannel, SocketError> ErrorCallback
 		{

+ 0 - 1
Unity/Assets/Scripts/Module/Message/Network/KCP/KChannel.cs

@@ -52,7 +52,6 @@ namespace ETModel
 			this.RemoteConn = remoteConn;
 			this.remoteEndPoint = remoteEndPoint;
 			this.socket = socket;
-			this.parser = new PacketParser(this.recvBuffer);
 			kcp = new Kcp(this.RemoteConn, this.Output);
 			kcp.SetMtu(512);
 			kcp.NoDelay(1, 10, 2, 1);  //fast

+ 2 - 4
Unity/Assets/Scripts/Module/Message/Network/TCP/TChannel.cs

@@ -215,14 +215,12 @@ namespace ETModel
 					}
 				}
 			}
-			catch (IOException e)
+			catch (IOException)
 			{
-				Log.Error(e);
 				this.OnError(SocketError.SocketError);
 			}
-			catch (ObjectDisposedException e)
+			catch (ObjectDisposedException)
 			{
-				Log.Error(e);
 				this.OnError(SocketError.SocketError);
 			}
 			catch (Exception e)