Component.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using ETModel;
  3. using MongoDB.Bson.Serialization.Attributes;
  4. #if UNITY_EDITOR
  5. using UnityEngine;
  6. #endif
  7. namespace ETHotfix
  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. if (this.parent.GameObject != null)
  68. {
  69. this.GameObject.transform.SetParent(this.parent.GameObject.transform, false);
  70. }
  71. }
  72. #endif
  73. }
  74. }
  75. public T GetParent<T>() where T : Component
  76. {
  77. return this.Parent as T;
  78. }
  79. [BsonIgnore]
  80. public Entity Entity
  81. {
  82. get
  83. {
  84. return this.Parent as Entity;
  85. }
  86. }
  87. protected Component()
  88. {
  89. this.InstanceId = IdGenerater.GenerateId();
  90. #if !SERVER
  91. if (!this.GetType().IsDefined(typeof(HideInHierarchy), true))
  92. {
  93. this.GameObject = new GameObject();
  94. this.GameObject.name = this.GetType().Name;
  95. this.GameObject.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN);
  96. this.GameObject.AddComponent<ComponentView>().Component = this;
  97. }
  98. #endif
  99. }
  100. public virtual void Dispose()
  101. {
  102. if (this.IsDisposed)
  103. {
  104. return;
  105. }
  106. // 触发Destroy事件
  107. Game.EventSystem.Destroy(this);
  108. Game.EventSystem.Remove(this.InstanceId);
  109. this.InstanceId = 0;
  110. if (this.IsFromPool)
  111. {
  112. Game.ObjectPool.Recycle(this);
  113. }
  114. else
  115. {
  116. #if !SERVER
  117. UnityEngine.Object.Destroy(this.GameObject);
  118. #endif
  119. }
  120. }
  121. public override void EndInit()
  122. {
  123. Game.EventSystem.Deserialize(this);
  124. }
  125. public override string ToString()
  126. {
  127. return MongoHelper.ToJson(this);
  128. }
  129. }
  130. }