Component.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using ETModel;
  3. using MongoDB.Bson.Serialization.Attributes;
  4. #if !SERVER
  5. using UnityEngine;
  6. #endif
  7. namespace ETModel
  8. {
  9. [BsonIgnoreExtraElements]
  10. public abstract class Component : Object, IDisposable
  11. {
  12. [BsonIgnore]
  13. public long InstanceId { get; protected set; }
  14. #if !SERVER
  15. public static GameObject Global { get; } = GameObject.Find("/Global");
  16. [BsonIgnore]
  17. public GameObject GameObject { get; protected set; }
  18. #endif
  19. [BsonIgnore]
  20. private bool isFromPool;
  21. [BsonIgnore]
  22. public bool IsFromPool
  23. {
  24. get
  25. {
  26. return this.isFromPool;
  27. }
  28. set
  29. {
  30. this.isFromPool = value;
  31. if (!this.isFromPool)
  32. {
  33. return;
  34. }
  35. if (this.InstanceId == 0)
  36. {
  37. this.InstanceId = IdGenerater.GenerateId();
  38. }
  39. }
  40. }
  41. [BsonIgnore]
  42. public bool IsDisposed
  43. {
  44. get
  45. {
  46. return this.InstanceId == 0;
  47. }
  48. }
  49. private Component parent;
  50. [BsonIgnore]
  51. public Component Parent
  52. {
  53. get
  54. {
  55. return this.parent;
  56. }
  57. set
  58. {
  59. this.parent = value;
  60. #if !SERVER
  61. if (this.parent == null)
  62. {
  63. this.GameObject.transform.SetParent(Global.transform, false);
  64. return;
  65. }
  66. if (this.GameObject != null && this.parent.GameObject != null)
  67. {
  68. this.GameObject.transform.SetParent(this.parent.GameObject.transform, false);
  69. }
  70. #endif
  71. }
  72. }
  73. public T GetParent<T>() where T : Component
  74. {
  75. return this.Parent as T;
  76. }
  77. [BsonIgnore]
  78. public Entity Entity
  79. {
  80. get
  81. {
  82. return this.Parent as Entity;
  83. }
  84. }
  85. protected Component()
  86. {
  87. this.InstanceId = IdGenerater.GenerateId();
  88. #if !SERVER
  89. if (!this.GetType().IsDefined(typeof(HideInHierarchy), true))
  90. {
  91. this.GameObject = new GameObject();
  92. this.GameObject.name = this.GetType().Name;
  93. this.GameObject.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN);
  94. this.GameObject.transform.SetParent(Global.transform, false);
  95. this.GameObject.AddComponent<ComponentView>().Component = this;
  96. }
  97. #endif
  98. }
  99. public virtual void Dispose()
  100. {
  101. if (this.IsDisposed)
  102. {
  103. return;
  104. }
  105. // 触发Destroy事件
  106. Game.EventSystem.Destroy(this);
  107. Game.EventSystem.Remove(this.InstanceId);
  108. this.InstanceId = 0;
  109. if (this.IsFromPool)
  110. {
  111. Game.ObjectPool.Recycle(this);
  112. }
  113. else
  114. {
  115. #if !SERVER
  116. if (this.GameObject != null)
  117. {
  118. UnityEngine.Object.Destroy(this.GameObject);
  119. }
  120. #endif
  121. }
  122. }
  123. public override void EndInit()
  124. {
  125. Game.EventSystem.Deserialize(this);
  126. }
  127. public override string ToString()
  128. {
  129. return MongoHelper.ToJson(this);
  130. }
  131. }
  132. }