Entity.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using GameFramework.Entity;
  9. using System;
  10. using UnityEngine;
  11. namespace UnityGameFramework.Runtime
  12. {
  13. /// <summary>
  14. /// 实体。
  15. /// </summary>
  16. public sealed class Entity : MonoBehaviour, IEntity
  17. {
  18. private int m_Id;
  19. private string m_EntityAssetName;
  20. private IEntityGroup m_EntityGroup;
  21. private EntityLogic m_EntityLogic;
  22. /// <summary>
  23. /// 获取实体编号。
  24. /// </summary>
  25. public int Id
  26. {
  27. get
  28. {
  29. return m_Id;
  30. }
  31. }
  32. /// <summary>
  33. /// 获取实体资源名称。
  34. /// </summary>
  35. public string EntityAssetName
  36. {
  37. get
  38. {
  39. return m_EntityAssetName;
  40. }
  41. }
  42. /// <summary>
  43. /// 获取实体实例。
  44. /// </summary>
  45. public object Handle
  46. {
  47. get
  48. {
  49. return gameObject;
  50. }
  51. }
  52. /// <summary>
  53. /// 获取实体所属的实体组。
  54. /// </summary>
  55. public IEntityGroup EntityGroup
  56. {
  57. get
  58. {
  59. return m_EntityGroup;
  60. }
  61. }
  62. /// <summary>
  63. /// 获取实体逻辑。
  64. /// </summary>
  65. public EntityLogic Logic
  66. {
  67. get
  68. {
  69. return m_EntityLogic;
  70. }
  71. }
  72. /// <summary>
  73. /// 实体初始化。
  74. /// </summary>
  75. /// <param name="entityId">实体编号。</param>
  76. /// <param name="entityAssetName">实体资源名称。</param>
  77. /// <param name="entityGroup">实体所属的实体组。</param>
  78. /// <param name="isNewInstance">是否是新实例。</param>
  79. /// <param name="userData">用户自定义数据。</param>
  80. public void OnInit(int entityId, string entityAssetName, IEntityGroup entityGroup, bool isNewInstance, object userData)
  81. {
  82. m_Id = entityId;
  83. m_EntityAssetName = entityAssetName;
  84. if (isNewInstance)
  85. {
  86. m_EntityGroup = entityGroup;
  87. }
  88. else if (m_EntityGroup != entityGroup)
  89. {
  90. Log.Error("Entity group is inconsistent for non-new-instance entity.");
  91. return;
  92. }
  93. ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
  94. Type entityLogicType = showEntityInfo.EntityLogicType;
  95. if (entityLogicType == null)
  96. {
  97. Log.Error("Entity logic type is invalid.");
  98. return;
  99. }
  100. if (m_EntityLogic != null)
  101. {
  102. if (m_EntityLogic.GetType() == entityLogicType)
  103. {
  104. m_EntityLogic.enabled = true;
  105. return;
  106. }
  107. Destroy(m_EntityLogic);
  108. m_EntityLogic = null;
  109. }
  110. m_EntityLogic = gameObject.AddComponent(entityLogicType) as EntityLogic;
  111. if (m_EntityLogic == null)
  112. {
  113. Log.Error("Entity '{0}' can not add entity logic.", entityAssetName);
  114. return;
  115. }
  116. try
  117. {
  118. m_EntityLogic.OnInit(showEntityInfo.UserData);
  119. }
  120. catch (Exception exception)
  121. {
  122. Log.Error("Entity '[{0}]{1}' OnInit with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString());
  123. }
  124. }
  125. /// <summary>
  126. /// 实体回收。
  127. /// </summary>
  128. public void OnRecycle()
  129. {
  130. try
  131. {
  132. m_EntityLogic.OnRecycle();
  133. m_EntityLogic.enabled = false;
  134. }
  135. catch (Exception exception)
  136. {
  137. Log.Error("Entity '[{0}]{1}' OnRecycle with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString());
  138. }
  139. m_Id = 0;
  140. }
  141. /// <summary>
  142. /// 实体显示。
  143. /// </summary>
  144. /// <param name="userData">用户自定义数据。</param>
  145. public void OnShow(object userData)
  146. {
  147. ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
  148. try
  149. {
  150. m_EntityLogic.OnShow(showEntityInfo.UserData);
  151. }
  152. catch (Exception exception)
  153. {
  154. Log.Error("Entity '[{0}]{1}' OnShow with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString());
  155. }
  156. }
  157. /// <summary>
  158. /// 实体隐藏。
  159. /// </summary>
  160. /// <param name="isShutdown">是否是关闭实体管理器时触发。</param>
  161. /// <param name="userData">用户自定义数据。</param>
  162. public void OnHide(bool isShutdown, object userData)
  163. {
  164. try
  165. {
  166. m_EntityLogic.OnHide(isShutdown, userData);
  167. }
  168. catch (Exception exception)
  169. {
  170. Log.Error("Entity '[{0}]{1}' OnHide with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString());
  171. }
  172. }
  173. /// <summary>
  174. /// 实体附加子实体。
  175. /// </summary>
  176. /// <param name="childEntity">附加的子实体。</param>
  177. /// <param name="userData">用户自定义数据。</param>
  178. public void OnAttached(IEntity childEntity, object userData)
  179. {
  180. AttachEntityInfo attachEntityInfo = (AttachEntityInfo)userData;
  181. try
  182. {
  183. m_EntityLogic.OnAttached(((Entity)childEntity).Logic, attachEntityInfo.ParentTransform, attachEntityInfo.UserData);
  184. }
  185. catch (Exception exception)
  186. {
  187. Log.Error("Entity '[{0}]{1}' OnAttached with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString());
  188. }
  189. }
  190. /// <summary>
  191. /// 实体解除子实体。
  192. /// </summary>
  193. /// <param name="childEntity">解除的子实体。</param>
  194. /// <param name="userData">用户自定义数据。</param>
  195. public void OnDetached(IEntity childEntity, object userData)
  196. {
  197. try
  198. {
  199. m_EntityLogic.OnDetached(((Entity)childEntity).Logic, userData);
  200. }
  201. catch (Exception exception)
  202. {
  203. Log.Error("Entity '[{0}]{1}' OnDetached with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString());
  204. }
  205. }
  206. /// <summary>
  207. /// 实体附加子实体。
  208. /// </summary>
  209. /// <param name="parentEntity">被附加的父实体。</param>
  210. /// <param name="userData">用户自定义数据。</param>
  211. public void OnAttachTo(IEntity parentEntity, object userData)
  212. {
  213. AttachEntityInfo attachEntityInfo = (AttachEntityInfo)userData;
  214. try
  215. {
  216. m_EntityLogic.OnAttachTo(((Entity)parentEntity).Logic, attachEntityInfo.ParentTransform, attachEntityInfo.UserData);
  217. }
  218. catch (Exception exception)
  219. {
  220. Log.Error("Entity '[{0}]{1}' OnAttachTo with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString());
  221. }
  222. ReferencePool.Release(attachEntityInfo);
  223. }
  224. /// <summary>
  225. /// 实体解除子实体。
  226. /// </summary>
  227. /// <param name="parentEntity">被解除的父实体。</param>
  228. /// <param name="userData">用户自定义数据。</param>
  229. public void OnDetachFrom(IEntity parentEntity, object userData)
  230. {
  231. try
  232. {
  233. m_EntityLogic.OnDetachFrom(((Entity)parentEntity).Logic, userData);
  234. }
  235. catch (Exception exception)
  236. {
  237. Log.Error("Entity '[{0}]{1}' OnDetachFrom with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString());
  238. }
  239. }
  240. /// <summary>
  241. /// 实体轮询。
  242. /// </summary>
  243. /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
  244. /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
  245. public void OnUpdate(float elapseSeconds, float realElapseSeconds)
  246. {
  247. try
  248. {
  249. m_EntityLogic.OnUpdate(elapseSeconds, realElapseSeconds);
  250. }
  251. catch (Exception exception)
  252. {
  253. Log.Error("Entity '[{0}]{1}' OnUpdate with exception '{2}'.", m_Id.ToString(), m_EntityAssetName, exception.ToString());
  254. }
  255. }
  256. }
  257. }