Component.cs 2.4 KB

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