EntityManager.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  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.ObjectPool;
  8. using GameFramework.Resource;
  9. using System;
  10. using System.Collections.Generic;
  11. namespace GameFramework.Entity
  12. {
  13. /// <summary>
  14. /// 实体管理器。
  15. /// </summary>
  16. internal sealed partial class EntityManager : GameFrameworkModule, IEntityManager
  17. {
  18. private readonly Dictionary<int, EntityInfo> m_EntityInfos;
  19. private readonly Dictionary<string, EntityGroup> m_EntityGroups;
  20. private readonly Dictionary<int, int> m_EntitiesBeingLoaded;
  21. private readonly HashSet<int> m_EntitiesToReleaseOnLoad;
  22. private readonly Queue<EntityInfo> m_RecycleQueue;
  23. private readonly LoadAssetCallbacks m_LoadAssetCallbacks;
  24. private IObjectPoolManager m_ObjectPoolManager;
  25. private IResourceManager m_ResourceManager;
  26. private IEntityHelper m_EntityHelper;
  27. private int m_Serial;
  28. private bool m_IsShutdown;
  29. private EventHandler<ShowEntitySuccessEventArgs> m_ShowEntitySuccessEventHandler;
  30. private EventHandler<ShowEntityFailureEventArgs> m_ShowEntityFailureEventHandler;
  31. private EventHandler<ShowEntityUpdateEventArgs> m_ShowEntityUpdateEventHandler;
  32. private EventHandler<ShowEntityDependencyAssetEventArgs> m_ShowEntityDependencyAssetEventHandler;
  33. private EventHandler<HideEntityCompleteEventArgs> m_HideEntityCompleteEventHandler;
  34. /// <summary>
  35. /// 初始化实体管理器的新实例。
  36. /// </summary>
  37. public EntityManager()
  38. {
  39. m_EntityInfos = new Dictionary<int, EntityInfo>();
  40. m_EntityGroups = new Dictionary<string, EntityGroup>(StringComparer.Ordinal);
  41. m_EntitiesBeingLoaded = new Dictionary<int, int>();
  42. m_EntitiesToReleaseOnLoad = new HashSet<int>();
  43. m_RecycleQueue = new Queue<EntityInfo>();
  44. m_LoadAssetCallbacks = new LoadAssetCallbacks(LoadAssetSuccessCallback, LoadAssetFailureCallback, LoadAssetUpdateCallback, LoadAssetDependencyAssetCallback);
  45. m_ObjectPoolManager = null;
  46. m_ResourceManager = null;
  47. m_EntityHelper = null;
  48. m_Serial = 0;
  49. m_IsShutdown = false;
  50. m_ShowEntitySuccessEventHandler = null;
  51. m_ShowEntityFailureEventHandler = null;
  52. m_ShowEntityUpdateEventHandler = null;
  53. m_ShowEntityDependencyAssetEventHandler = null;
  54. m_HideEntityCompleteEventHandler = null;
  55. }
  56. /// <summary>
  57. /// 获取实体数量。
  58. /// </summary>
  59. public int EntityCount
  60. {
  61. get
  62. {
  63. return m_EntityInfos.Count;
  64. }
  65. }
  66. /// <summary>
  67. /// 获取实体组数量。
  68. /// </summary>
  69. public int EntityGroupCount
  70. {
  71. get
  72. {
  73. return m_EntityGroups.Count;
  74. }
  75. }
  76. /// <summary>
  77. /// 显示实体成功事件。
  78. /// </summary>
  79. public event EventHandler<ShowEntitySuccessEventArgs> ShowEntitySuccess
  80. {
  81. add
  82. {
  83. m_ShowEntitySuccessEventHandler += value;
  84. }
  85. remove
  86. {
  87. m_ShowEntitySuccessEventHandler -= value;
  88. }
  89. }
  90. /// <summary>
  91. /// 显示实体失败事件。
  92. /// </summary>
  93. public event EventHandler<ShowEntityFailureEventArgs> ShowEntityFailure
  94. {
  95. add
  96. {
  97. m_ShowEntityFailureEventHandler += value;
  98. }
  99. remove
  100. {
  101. m_ShowEntityFailureEventHandler -= value;
  102. }
  103. }
  104. /// <summary>
  105. /// 显示实体更新事件。
  106. /// </summary>
  107. public event EventHandler<ShowEntityUpdateEventArgs> ShowEntityUpdate
  108. {
  109. add
  110. {
  111. m_ShowEntityUpdateEventHandler += value;
  112. }
  113. remove
  114. {
  115. m_ShowEntityUpdateEventHandler -= value;
  116. }
  117. }
  118. /// <summary>
  119. /// 显示实体时加载依赖资源事件。
  120. /// </summary>
  121. public event EventHandler<ShowEntityDependencyAssetEventArgs> ShowEntityDependencyAsset
  122. {
  123. add
  124. {
  125. m_ShowEntityDependencyAssetEventHandler += value;
  126. }
  127. remove
  128. {
  129. m_ShowEntityDependencyAssetEventHandler -= value;
  130. }
  131. }
  132. /// <summary>
  133. /// 隐藏实体完成事件。
  134. /// </summary>
  135. public event EventHandler<HideEntityCompleteEventArgs> HideEntityComplete
  136. {
  137. add
  138. {
  139. m_HideEntityCompleteEventHandler += value;
  140. }
  141. remove
  142. {
  143. m_HideEntityCompleteEventHandler -= value;
  144. }
  145. }
  146. /// <summary>
  147. /// 实体管理器轮询。
  148. /// </summary>
  149. /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
  150. /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
  151. internal override void Update(float elapseSeconds, float realElapseSeconds)
  152. {
  153. while (m_RecycleQueue.Count > 0)
  154. {
  155. EntityInfo entityInfo = m_RecycleQueue.Dequeue();
  156. IEntity entity = entityInfo.Entity;
  157. EntityGroup entityGroup = (EntityGroup)entity.EntityGroup;
  158. if (entityGroup == null)
  159. {
  160. throw new GameFrameworkException("Entity group is invalid.");
  161. }
  162. entityInfo.Status = EntityStatus.WillRecycle;
  163. entity.OnRecycle();
  164. entityInfo.Status = EntityStatus.Recycled;
  165. entityGroup.UnspawnEntity(entity);
  166. ReferencePool.Release(entityInfo);
  167. }
  168. foreach (KeyValuePair<string, EntityGroup> entityGroup in m_EntityGroups)
  169. {
  170. entityGroup.Value.Update(elapseSeconds, realElapseSeconds);
  171. }
  172. }
  173. /// <summary>
  174. /// 关闭并清理实体管理器。
  175. /// </summary>
  176. internal override void Shutdown()
  177. {
  178. m_IsShutdown = true;
  179. HideAllLoadedEntities();
  180. m_EntityGroups.Clear();
  181. m_EntitiesBeingLoaded.Clear();
  182. m_EntitiesToReleaseOnLoad.Clear();
  183. m_RecycleQueue.Clear();
  184. }
  185. /// <summary>
  186. /// 设置对象池管理器。
  187. /// </summary>
  188. /// <param name="objectPoolManager">对象池管理器。</param>
  189. public void SetObjectPoolManager(IObjectPoolManager objectPoolManager)
  190. {
  191. if (objectPoolManager == null)
  192. {
  193. throw new GameFrameworkException("Object pool manager is invalid.");
  194. }
  195. m_ObjectPoolManager = objectPoolManager;
  196. }
  197. /// <summary>
  198. /// 设置资源管理器。
  199. /// </summary>
  200. /// <param name="resourceManager">资源管理器。</param>
  201. public void SetResourceManager(IResourceManager resourceManager)
  202. {
  203. if (resourceManager == null)
  204. {
  205. throw new GameFrameworkException("Resource manager is invalid.");
  206. }
  207. m_ResourceManager = resourceManager;
  208. }
  209. /// <summary>
  210. /// 设置实体辅助器。
  211. /// </summary>
  212. /// <param name="entityHelper">实体辅助器。</param>
  213. public void SetEntityHelper(IEntityHelper entityHelper)
  214. {
  215. if (entityHelper == null)
  216. {
  217. throw new GameFrameworkException("Entity helper is invalid.");
  218. }
  219. m_EntityHelper = entityHelper;
  220. }
  221. /// <summary>
  222. /// 是否存在实体组。
  223. /// </summary>
  224. /// <param name="entityGroupName">实体组名称。</param>
  225. /// <returns>是否存在实体组。</returns>
  226. public bool HasEntityGroup(string entityGroupName)
  227. {
  228. if (string.IsNullOrEmpty(entityGroupName))
  229. {
  230. throw new GameFrameworkException("Entity group name is invalid.");
  231. }
  232. return m_EntityGroups.ContainsKey(entityGroupName);
  233. }
  234. /// <summary>
  235. /// 获取实体组。
  236. /// </summary>
  237. /// <param name="entityGroupName">实体组名称。</param>
  238. /// <returns>要获取的实体组。</returns>
  239. public IEntityGroup GetEntityGroup(string entityGroupName)
  240. {
  241. if (string.IsNullOrEmpty(entityGroupName))
  242. {
  243. throw new GameFrameworkException("Entity group name is invalid.");
  244. }
  245. EntityGroup entityGroup = null;
  246. if (m_EntityGroups.TryGetValue(entityGroupName, out entityGroup))
  247. {
  248. return entityGroup;
  249. }
  250. return null;
  251. }
  252. /// <summary>
  253. /// 获取所有实体组。
  254. /// </summary>
  255. /// <returns>所有实体组。</returns>
  256. public IEntityGroup[] GetAllEntityGroups()
  257. {
  258. int index = 0;
  259. IEntityGroup[] results = new IEntityGroup[m_EntityGroups.Count];
  260. foreach (KeyValuePair<string, EntityGroup> entityGroup in m_EntityGroups)
  261. {
  262. results[index++] = entityGroup.Value;
  263. }
  264. return results;
  265. }
  266. /// <summary>
  267. /// 获取所有实体组。
  268. /// </summary>
  269. /// <param name="results">所有实体组。</param>
  270. public void GetAllEntityGroups(List<IEntityGroup> results)
  271. {
  272. if (results == null)
  273. {
  274. throw new GameFrameworkException("Results is invalid.");
  275. }
  276. results.Clear();
  277. foreach (KeyValuePair<string, EntityGroup> entityGroup in m_EntityGroups)
  278. {
  279. results.Add(entityGroup.Value);
  280. }
  281. }
  282. /// <summary>
  283. /// 增加实体组。
  284. /// </summary>
  285. /// <param name="entityGroupName">实体组名称。</param>
  286. /// <param name="instanceAutoReleaseInterval">实体实例对象池自动释放可释放对象的间隔秒数。</param>
  287. /// <param name="instanceCapacity">实体实例对象池容量。</param>
  288. /// <param name="instanceExpireTime">实体实例对象池对象过期秒数。</param>
  289. /// <param name="instancePriority">实体实例对象池的优先级。</param>
  290. /// <param name="entityGroupHelper">实体组辅助器。</param>
  291. /// <returns>是否增加实体组成功。</returns>
  292. public bool AddEntityGroup(string entityGroupName, float instanceAutoReleaseInterval, int instanceCapacity, float instanceExpireTime, int instancePriority, IEntityGroupHelper entityGroupHelper)
  293. {
  294. if (string.IsNullOrEmpty(entityGroupName))
  295. {
  296. throw new GameFrameworkException("Entity group name is invalid.");
  297. }
  298. if (entityGroupHelper == null)
  299. {
  300. throw new GameFrameworkException("Entity group helper is invalid.");
  301. }
  302. if (m_ObjectPoolManager == null)
  303. {
  304. throw new GameFrameworkException("You must set object pool manager first.");
  305. }
  306. if (HasEntityGroup(entityGroupName))
  307. {
  308. return false;
  309. }
  310. m_EntityGroups.Add(entityGroupName, new EntityGroup(entityGroupName, instanceAutoReleaseInterval, instanceCapacity, instanceExpireTime, instancePriority, entityGroupHelper, m_ObjectPoolManager));
  311. return true;
  312. }
  313. /// <summary>
  314. /// 是否存在实体。
  315. /// </summary>
  316. /// <param name="entityId">实体编号。</param>
  317. /// <returns>是否存在实体。</returns>
  318. public bool HasEntity(int entityId)
  319. {
  320. return m_EntityInfos.ContainsKey(entityId);
  321. }
  322. /// <summary>
  323. /// 是否存在实体。
  324. /// </summary>
  325. /// <param name="entityAssetName">实体资源名称。</param>
  326. /// <returns>是否存在实体。</returns>
  327. public bool HasEntity(string entityAssetName)
  328. {
  329. if (string.IsNullOrEmpty(entityAssetName))
  330. {
  331. throw new GameFrameworkException("Entity asset name is invalid.");
  332. }
  333. foreach (KeyValuePair<int, EntityInfo> entityInfo in m_EntityInfos)
  334. {
  335. if (entityInfo.Value.Entity.EntityAssetName == entityAssetName)
  336. {
  337. return true;
  338. }
  339. }
  340. return false;
  341. }
  342. /// <summary>
  343. /// 获取实体。
  344. /// </summary>
  345. /// <param name="entityId">实体编号。</param>
  346. /// <returns>要获取的实体。</returns>
  347. public IEntity GetEntity(int entityId)
  348. {
  349. EntityInfo entityInfo = GetEntityInfo(entityId);
  350. if (entityInfo == null)
  351. {
  352. return null;
  353. }
  354. return entityInfo.Entity;
  355. }
  356. /// <summary>
  357. /// 获取实体。
  358. /// </summary>
  359. /// <param name="entityAssetName">实体资源名称。</param>
  360. /// <returns>要获取的实体。</returns>
  361. public IEntity GetEntity(string entityAssetName)
  362. {
  363. if (string.IsNullOrEmpty(entityAssetName))
  364. {
  365. throw new GameFrameworkException("Entity asset name is invalid.");
  366. }
  367. foreach (KeyValuePair<int, EntityInfo> entityInfo in m_EntityInfos)
  368. {
  369. if (entityInfo.Value.Entity.EntityAssetName == entityAssetName)
  370. {
  371. return entityInfo.Value.Entity;
  372. }
  373. }
  374. return null;
  375. }
  376. /// <summary>
  377. /// 获取实体。
  378. /// </summary>
  379. /// <param name="entityAssetName">实体资源名称。</param>
  380. /// <returns>要获取的实体。</returns>
  381. public IEntity[] GetEntities(string entityAssetName)
  382. {
  383. if (string.IsNullOrEmpty(entityAssetName))
  384. {
  385. throw new GameFrameworkException("Entity asset name is invalid.");
  386. }
  387. List<IEntity> results = new List<IEntity>();
  388. foreach (KeyValuePair<int, EntityInfo> entityInfo in m_EntityInfos)
  389. {
  390. if (entityInfo.Value.Entity.EntityAssetName == entityAssetName)
  391. {
  392. results.Add(entityInfo.Value.Entity);
  393. }
  394. }
  395. return results.ToArray();
  396. }
  397. /// <summary>
  398. /// 获取实体。
  399. /// </summary>
  400. /// <param name="entityAssetName">实体资源名称。</param>
  401. /// <param name="results">要获取的实体。</param>
  402. public void GetEntities(string entityAssetName, List<IEntity> results)
  403. {
  404. if (string.IsNullOrEmpty(entityAssetName))
  405. {
  406. throw new GameFrameworkException("Entity asset name is invalid.");
  407. }
  408. if (results == null)
  409. {
  410. throw new GameFrameworkException("Results is invalid.");
  411. }
  412. results.Clear();
  413. foreach (KeyValuePair<int, EntityInfo> entityInfo in m_EntityInfos)
  414. {
  415. if (entityInfo.Value.Entity.EntityAssetName == entityAssetName)
  416. {
  417. results.Add(entityInfo.Value.Entity);
  418. }
  419. }
  420. }
  421. /// <summary>
  422. /// 获取所有已加载的实体。
  423. /// </summary>
  424. /// <returns>所有已加载的实体。</returns>
  425. public IEntity[] GetAllLoadedEntities()
  426. {
  427. int index = 0;
  428. IEntity[] results = new IEntity[m_EntityInfos.Count];
  429. foreach (KeyValuePair<int, EntityInfo> entityInfo in m_EntityInfos)
  430. {
  431. results[index++] = entityInfo.Value.Entity;
  432. }
  433. return results;
  434. }
  435. /// <summary>
  436. /// 获取所有已加载的实体。
  437. /// </summary>
  438. /// <param name="results">所有已加载的实体。</param>
  439. public void GetAllLoadedEntities(List<IEntity> results)
  440. {
  441. if (results == null)
  442. {
  443. throw new GameFrameworkException("Results is invalid.");
  444. }
  445. results.Clear();
  446. foreach (KeyValuePair<int, EntityInfo> entityInfo in m_EntityInfos)
  447. {
  448. results.Add(entityInfo.Value.Entity);
  449. }
  450. }
  451. /// <summary>
  452. /// 获取所有正在加载实体的编号。
  453. /// </summary>
  454. /// <returns>所有正在加载实体的编号。</returns>
  455. public int[] GetAllLoadingEntityIds()
  456. {
  457. int index = 0;
  458. int[] results = new int[m_EntitiesBeingLoaded.Count];
  459. foreach (KeyValuePair<int, int> entityBeingLoaded in m_EntitiesBeingLoaded)
  460. {
  461. results[index++] = entityBeingLoaded.Key;
  462. }
  463. return results;
  464. }
  465. /// <summary>
  466. /// 获取所有正在加载实体的编号。
  467. /// </summary>
  468. /// <param name="results">所有正在加载实体的编号。</param>
  469. public void GetAllLoadingEntityIds(List<int> results)
  470. {
  471. if (results == null)
  472. {
  473. throw new GameFrameworkException("Results is invalid.");
  474. }
  475. results.Clear();
  476. foreach (KeyValuePair<int, int> entityBeingLoaded in m_EntitiesBeingLoaded)
  477. {
  478. results.Add(entityBeingLoaded.Key);
  479. }
  480. }
  481. /// <summary>
  482. /// 是否正在加载实体。
  483. /// </summary>
  484. /// <param name="entityId">实体编号。</param>
  485. /// <returns>是否正在加载实体。</returns>
  486. public bool IsLoadingEntity(int entityId)
  487. {
  488. return m_EntitiesBeingLoaded.ContainsKey(entityId);
  489. }
  490. /// <summary>
  491. /// 是否是合法的实体。
  492. /// </summary>
  493. /// <param name="entity">实体。</param>
  494. /// <returns>实体是否合法。</returns>
  495. public bool IsValidEntity(IEntity entity)
  496. {
  497. if (entity == null)
  498. {
  499. return false;
  500. }
  501. return HasEntity(entity.Id);
  502. }
  503. /// <summary>
  504. /// 显示实体。
  505. /// </summary>
  506. /// <param name="entityId">实体编号。</param>
  507. /// <param name="entityAssetName">实体资源名称。</param>
  508. /// <param name="entityGroupName">实体组名称。</param>
  509. public void ShowEntity(int entityId, string entityAssetName, string entityGroupName)
  510. {
  511. ShowEntity(entityId, entityAssetName, entityGroupName, Constant.DefaultPriority, null);
  512. }
  513. /// <summary>
  514. /// 显示实体。
  515. /// </summary>
  516. /// <param name="entityId">实体编号。</param>
  517. /// <param name="entityAssetName">实体资源名称。</param>
  518. /// <param name="entityGroupName">实体组名称。</param>
  519. /// <param name="priority">加载实体资源的优先级。</param>
  520. public void ShowEntity(int entityId, string entityAssetName, string entityGroupName, int priority)
  521. {
  522. ShowEntity(entityId, entityAssetName, entityGroupName, priority, null);
  523. }
  524. /// <summary>
  525. /// 显示实体。
  526. /// </summary>
  527. /// <param name="entityId">实体编号。</param>
  528. /// <param name="entityAssetName">实体资源名称。</param>
  529. /// <param name="entityGroupName">实体组名称。</param>
  530. /// <param name="userData">用户自定义数据。</param>
  531. public void ShowEntity(int entityId, string entityAssetName, string entityGroupName, object userData)
  532. {
  533. ShowEntity(entityId, entityAssetName, entityGroupName, Constant.DefaultPriority, userData);
  534. }
  535. /// <summary>
  536. /// 显示实体。
  537. /// </summary>
  538. /// <param name="entityId">实体编号。</param>
  539. /// <param name="entityAssetName">实体资源名称。</param>
  540. /// <param name="entityGroupName">实体组名称。</param>
  541. /// <param name="priority">加载实体资源的优先级。</param>
  542. /// <param name="userData">用户自定义数据。</param>
  543. public void ShowEntity(int entityId, string entityAssetName, string entityGroupName, int priority, object userData)
  544. {
  545. if (m_ResourceManager == null)
  546. {
  547. throw new GameFrameworkException("You must set resource manager first.");
  548. }
  549. if (m_EntityHelper == null)
  550. {
  551. throw new GameFrameworkException("You must set entity helper first.");
  552. }
  553. if (string.IsNullOrEmpty(entityAssetName))
  554. {
  555. throw new GameFrameworkException("Entity asset name is invalid.");
  556. }
  557. if (string.IsNullOrEmpty(entityGroupName))
  558. {
  559. throw new GameFrameworkException("Entity group name is invalid.");
  560. }
  561. if (HasEntity(entityId))
  562. {
  563. throw new GameFrameworkException(Utility.Text.Format("Entity id '{0}' is already exist.", entityId));
  564. }
  565. if (IsLoadingEntity(entityId))
  566. {
  567. throw new GameFrameworkException(Utility.Text.Format("Entity '{0}' is already being loaded.", entityId));
  568. }
  569. EntityGroup entityGroup = (EntityGroup)GetEntityGroup(entityGroupName);
  570. if (entityGroup == null)
  571. {
  572. throw new GameFrameworkException(Utility.Text.Format("Entity group '{0}' is not exist.", entityGroupName));
  573. }
  574. EntityInstanceObject entityInstanceObject = entityGroup.SpawnEntityInstanceObject(entityAssetName);
  575. if (entityInstanceObject == null)
  576. {
  577. int serialId = ++m_Serial;
  578. m_EntitiesBeingLoaded.Add(entityId, serialId);
  579. m_ResourceManager.LoadAsset(entityAssetName, priority, m_LoadAssetCallbacks, ShowEntityInfo.Create(serialId, entityId, entityGroup, userData));
  580. return;
  581. }
  582. InternalShowEntity(entityId, entityAssetName, entityGroup, entityInstanceObject.Target, false, 0f, userData);
  583. }
  584. /// <summary>
  585. /// 隐藏实体。
  586. /// </summary>
  587. /// <param name="entityId">实体编号。</param>
  588. public void HideEntity(int entityId)
  589. {
  590. HideEntity(entityId, null);
  591. }
  592. /// <summary>
  593. /// 隐藏实体。
  594. /// </summary>
  595. /// <param name="entityId">实体编号。</param>
  596. /// <param name="userData">用户自定义数据。</param>
  597. public void HideEntity(int entityId, object userData)
  598. {
  599. if (IsLoadingEntity(entityId))
  600. {
  601. m_EntitiesToReleaseOnLoad.Add(m_EntitiesBeingLoaded[entityId]);
  602. m_EntitiesBeingLoaded.Remove(entityId);
  603. return;
  604. }
  605. EntityInfo entityInfo = GetEntityInfo(entityId);
  606. if (entityInfo == null)
  607. {
  608. throw new GameFrameworkException(Utility.Text.Format("Can not find entity '{0}'.", entityId));
  609. }
  610. InternalHideEntity(entityInfo, userData);
  611. }
  612. /// <summary>
  613. /// 隐藏实体。
  614. /// </summary>
  615. /// <param name="entity">实体。</param>
  616. public void HideEntity(IEntity entity)
  617. {
  618. HideEntity(entity, null);
  619. }
  620. /// <summary>
  621. /// 隐藏实体。
  622. /// </summary>
  623. /// <param name="entity">实体。</param>
  624. /// <param name="userData">用户自定义数据。</param>
  625. public void HideEntity(IEntity entity, object userData)
  626. {
  627. if (entity == null)
  628. {
  629. throw new GameFrameworkException("Entity is invalid.");
  630. }
  631. HideEntity(entity.Id, userData);
  632. }
  633. /// <summary>
  634. /// 隐藏所有已加载的实体。
  635. /// </summary>
  636. public void HideAllLoadedEntities()
  637. {
  638. HideAllLoadedEntities(null);
  639. }
  640. /// <summary>
  641. /// 隐藏所有已加载的实体。
  642. /// </summary>
  643. /// <param name="userData">用户自定义数据。</param>
  644. public void HideAllLoadedEntities(object userData)
  645. {
  646. while (m_EntityInfos.Count > 0)
  647. {
  648. foreach (KeyValuePair<int, EntityInfo> entityInfo in m_EntityInfos)
  649. {
  650. InternalHideEntity(entityInfo.Value, userData);
  651. break;
  652. }
  653. }
  654. }
  655. /// <summary>
  656. /// 隐藏所有正在加载的实体。
  657. /// </summary>
  658. public void HideAllLoadingEntities()
  659. {
  660. foreach (KeyValuePair<int, int> entityBeingLoaded in m_EntitiesBeingLoaded)
  661. {
  662. m_EntitiesToReleaseOnLoad.Add(entityBeingLoaded.Value);
  663. }
  664. m_EntitiesBeingLoaded.Clear();
  665. }
  666. /// <summary>
  667. /// 获取父实体。
  668. /// </summary>
  669. /// <param name="childEntityId">要获取父实体的子实体的实体编号。</param>
  670. /// <returns>子实体的父实体。</returns>
  671. public IEntity GetParentEntity(int childEntityId)
  672. {
  673. EntityInfo childEntityInfo = GetEntityInfo(childEntityId);
  674. if (childEntityInfo == null)
  675. {
  676. throw new GameFrameworkException(Utility.Text.Format("Can not find child entity '{0}'.", childEntityId));
  677. }
  678. return childEntityInfo.ParentEntity;
  679. }
  680. /// <summary>
  681. /// 获取父实体。
  682. /// </summary>
  683. /// <param name="childEntity">要获取父实体的子实体。</param>
  684. /// <returns>子实体的父实体。</returns>
  685. public IEntity GetParentEntity(IEntity childEntity)
  686. {
  687. if (childEntity == null)
  688. {
  689. throw new GameFrameworkException("Child entity is invalid.");
  690. }
  691. return GetParentEntity(childEntity.Id);
  692. }
  693. /// <summary>
  694. /// 获取子实体数量。
  695. /// </summary>
  696. /// <param name="parentEntityId">要获取子实体数量的父实体的实体编号。</param>
  697. /// <returns>子实体数量。</returns>
  698. public int GetChildEntityCount(int parentEntityId)
  699. {
  700. EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
  701. if (parentEntityInfo == null)
  702. {
  703. throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
  704. }
  705. return parentEntityInfo.ChildEntityCount;
  706. }
  707. /// <summary>
  708. /// 获取子实体。
  709. /// </summary>
  710. /// <param name="parentEntityId">要获取子实体的父实体的实体编号。</param>
  711. /// <returns>子实体。</returns>
  712. public IEntity GetChildEntity(int parentEntityId)
  713. {
  714. EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
  715. if (parentEntityInfo == null)
  716. {
  717. throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
  718. }
  719. return parentEntityInfo.GetChildEntity();
  720. }
  721. /// <summary>
  722. /// 获取子实体。
  723. /// </summary>
  724. /// <param name="parentEntity">要获取子实体的父实体。</param>
  725. /// <returns>子实体。</returns>
  726. public IEntity GetChildEntity(IEntity parentEntity)
  727. {
  728. if (parentEntity == null)
  729. {
  730. throw new GameFrameworkException("Parent entity is invalid.");
  731. }
  732. return GetChildEntity(parentEntity.Id);
  733. }
  734. /// <summary>
  735. /// 获取所有子实体。
  736. /// </summary>
  737. /// <param name="parentEntityId">要获取所有子实体的父实体的实体编号。</param>
  738. /// <returns>所有子实体。</returns>
  739. public IEntity[] GetChildEntities(int parentEntityId)
  740. {
  741. EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
  742. if (parentEntityInfo == null)
  743. {
  744. throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
  745. }
  746. return parentEntityInfo.GetChildEntities();
  747. }
  748. /// <summary>
  749. /// 获取所有子实体。
  750. /// </summary>
  751. /// <param name="parentEntityId">要获取所有子实体的父实体的实体编号。</param>
  752. /// <param name="results">所有子实体。</param>
  753. public void GetChildEntities(int parentEntityId, List<IEntity> results)
  754. {
  755. EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
  756. if (parentEntityInfo == null)
  757. {
  758. throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
  759. }
  760. parentEntityInfo.GetChildEntities(results);
  761. }
  762. /// <summary>
  763. /// 获取所有子实体。
  764. /// </summary>
  765. /// <param name="parentEntity">要获取所有子实体的父实体。</param>
  766. /// <returns>所有子实体。</returns>
  767. public IEntity[] GetChildEntities(IEntity parentEntity)
  768. {
  769. if (parentEntity == null)
  770. {
  771. throw new GameFrameworkException("Parent entity is invalid.");
  772. }
  773. return GetChildEntities(parentEntity.Id);
  774. }
  775. /// <summary>
  776. /// 获取所有子实体。
  777. /// </summary>
  778. /// <param name="parentEntity">要获取所有子实体的父实体。</param>
  779. /// <param name="results">所有子实体。</param>
  780. public void GetChildEntities(IEntity parentEntity, List<IEntity> results)
  781. {
  782. if (parentEntity == null)
  783. {
  784. throw new GameFrameworkException("Parent entity is invalid.");
  785. }
  786. GetChildEntities(parentEntity.Id, results);
  787. }
  788. /// <summary>
  789. /// 附加子实体。
  790. /// </summary>
  791. /// <param name="childEntityId">要附加的子实体的实体编号。</param>
  792. /// <param name="parentEntityId">被附加的父实体的实体编号。</param>
  793. public void AttachEntity(int childEntityId, int parentEntityId)
  794. {
  795. AttachEntity(childEntityId, parentEntityId, null);
  796. }
  797. /// <summary>
  798. /// 附加子实体。
  799. /// </summary>
  800. /// <param name="childEntityId">要附加的子实体的实体编号。</param>
  801. /// <param name="parentEntityId">被附加的父实体的实体编号。</param>
  802. /// <param name="userData">用户自定义数据。</param>
  803. public void AttachEntity(int childEntityId, int parentEntityId, object userData)
  804. {
  805. if (childEntityId == parentEntityId)
  806. {
  807. throw new GameFrameworkException(Utility.Text.Format("Can not attach entity when child entity id equals to parent entity id '{0}'.", parentEntityId));
  808. }
  809. EntityInfo childEntityInfo = GetEntityInfo(childEntityId);
  810. if (childEntityInfo == null)
  811. {
  812. throw new GameFrameworkException(Utility.Text.Format("Can not find child entity '{0}'.", childEntityId));
  813. }
  814. if (childEntityInfo.Status >= EntityStatus.WillHide)
  815. {
  816. throw new GameFrameworkException(Utility.Text.Format("Can not attach entity when child entity status is '{0}'.", childEntityInfo.Status));
  817. }
  818. EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
  819. if (parentEntityInfo == null)
  820. {
  821. throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
  822. }
  823. if (parentEntityInfo.Status >= EntityStatus.WillHide)
  824. {
  825. throw new GameFrameworkException(Utility.Text.Format("Can not attach entity when parent entity status is '{0}'.", parentEntityInfo.Status));
  826. }
  827. IEntity childEntity = childEntityInfo.Entity;
  828. IEntity parentEntity = parentEntityInfo.Entity;
  829. DetachEntity(childEntity.Id, userData);
  830. childEntityInfo.ParentEntity = parentEntity;
  831. parentEntityInfo.AddChildEntity(childEntity);
  832. parentEntity.OnAttached(childEntity, userData);
  833. childEntity.OnAttachTo(parentEntity, userData);
  834. }
  835. /// <summary>
  836. /// 附加子实体。
  837. /// </summary>
  838. /// <param name="childEntityId">要附加的子实体的实体编号。</param>
  839. /// <param name="parentEntity">被附加的父实体。</param>
  840. public void AttachEntity(int childEntityId, IEntity parentEntity)
  841. {
  842. AttachEntity(childEntityId, parentEntity, null);
  843. }
  844. /// <summary>
  845. /// 附加子实体。
  846. /// </summary>
  847. /// <param name="childEntityId">要附加的子实体的实体编号。</param>
  848. /// <param name="parentEntity">被附加的父实体。</param>
  849. /// <param name="userData">用户自定义数据。</param>
  850. public void AttachEntity(int childEntityId, IEntity parentEntity, object userData)
  851. {
  852. if (parentEntity == null)
  853. {
  854. throw new GameFrameworkException("Parent entity is invalid.");
  855. }
  856. AttachEntity(childEntityId, parentEntity.Id, userData);
  857. }
  858. /// <summary>
  859. /// 附加子实体。
  860. /// </summary>
  861. /// <param name="childEntity">要附加的子实体。</param>
  862. /// <param name="parentEntityId">被附加的父实体的实体编号。</param>
  863. public void AttachEntity(IEntity childEntity, int parentEntityId)
  864. {
  865. AttachEntity(childEntity, parentEntityId, null);
  866. }
  867. /// <summary>
  868. /// 附加子实体。
  869. /// </summary>
  870. /// <param name="childEntity">要附加的子实体。</param>
  871. /// <param name="parentEntityId">被附加的父实体的实体编号。</param>
  872. /// <param name="userData">用户自定义数据。</param>
  873. public void AttachEntity(IEntity childEntity, int parentEntityId, object userData)
  874. {
  875. if (childEntity == null)
  876. {
  877. throw new GameFrameworkException("Child entity is invalid.");
  878. }
  879. AttachEntity(childEntity.Id, parentEntityId, userData);
  880. }
  881. /// <summary>
  882. /// 附加子实体。
  883. /// </summary>
  884. /// <param name="childEntity">要附加的子实体。</param>
  885. /// <param name="parentEntity">被附加的父实体。</param>
  886. public void AttachEntity(IEntity childEntity, IEntity parentEntity)
  887. {
  888. AttachEntity(childEntity, parentEntity, null);
  889. }
  890. /// <summary>
  891. /// 附加子实体。
  892. /// </summary>
  893. /// <param name="childEntity">要附加的子实体。</param>
  894. /// <param name="parentEntity">被附加的父实体。</param>
  895. /// <param name="userData">用户自定义数据。</param>
  896. public void AttachEntity(IEntity childEntity, IEntity parentEntity, object userData)
  897. {
  898. if (childEntity == null)
  899. {
  900. throw new GameFrameworkException("Child entity is invalid.");
  901. }
  902. if (parentEntity == null)
  903. {
  904. throw new GameFrameworkException("Parent entity is invalid.");
  905. }
  906. AttachEntity(childEntity.Id, parentEntity.Id, userData);
  907. }
  908. /// <summary>
  909. /// 解除子实体。
  910. /// </summary>
  911. /// <param name="childEntityId">要解除的子实体的实体编号。</param>
  912. public void DetachEntity(int childEntityId)
  913. {
  914. DetachEntity(childEntityId, null);
  915. }
  916. /// <summary>
  917. /// 解除子实体。
  918. /// </summary>
  919. /// <param name="childEntityId">要解除的子实体的实体编号。</param>
  920. /// <param name="userData">用户自定义数据。</param>
  921. public void DetachEntity(int childEntityId, object userData)
  922. {
  923. EntityInfo childEntityInfo = GetEntityInfo(childEntityId);
  924. if (childEntityInfo == null)
  925. {
  926. throw new GameFrameworkException(Utility.Text.Format("Can not find child entity '{0}'.", childEntityId));
  927. }
  928. IEntity parentEntity = childEntityInfo.ParentEntity;
  929. if (parentEntity == null)
  930. {
  931. return;
  932. }
  933. EntityInfo parentEntityInfo = GetEntityInfo(parentEntity.Id);
  934. if (parentEntityInfo == null)
  935. {
  936. throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntity.Id));
  937. }
  938. IEntity childEntity = childEntityInfo.Entity;
  939. childEntityInfo.ParentEntity = null;
  940. parentEntityInfo.RemoveChildEntity(childEntity);
  941. parentEntity.OnDetached(childEntity, userData);
  942. childEntity.OnDetachFrom(parentEntity, userData);
  943. }
  944. /// <summary>
  945. /// 解除子实体。
  946. /// </summary>
  947. /// <param name="childEntity">要解除的子实体。</param>
  948. public void DetachEntity(IEntity childEntity)
  949. {
  950. DetachEntity(childEntity, null);
  951. }
  952. /// <summary>
  953. /// 解除子实体。
  954. /// </summary>
  955. /// <param name="childEntity">要解除的子实体。</param>
  956. /// <param name="userData">用户自定义数据。</param>
  957. public void DetachEntity(IEntity childEntity, object userData)
  958. {
  959. if (childEntity == null)
  960. {
  961. throw new GameFrameworkException("Child entity is invalid.");
  962. }
  963. DetachEntity(childEntity.Id, userData);
  964. }
  965. /// <summary>
  966. /// 解除所有子实体。
  967. /// </summary>
  968. /// <param name="parentEntityId">被解除的父实体的实体编号。</param>
  969. public void DetachChildEntities(int parentEntityId)
  970. {
  971. DetachChildEntities(parentEntityId, null);
  972. }
  973. /// <summary>
  974. /// 解除所有子实体。
  975. /// </summary>
  976. /// <param name="parentEntityId">被解除的父实体的实体编号。</param>
  977. /// <param name="userData">用户自定义数据。</param>
  978. public void DetachChildEntities(int parentEntityId, object userData)
  979. {
  980. EntityInfo parentEntityInfo = GetEntityInfo(parentEntityId);
  981. if (parentEntityInfo == null)
  982. {
  983. throw new GameFrameworkException(Utility.Text.Format("Can not find parent entity '{0}'.", parentEntityId));
  984. }
  985. while (parentEntityInfo.ChildEntityCount > 0)
  986. {
  987. IEntity childEntity = parentEntityInfo.GetChildEntity();
  988. DetachEntity(childEntity.Id, userData);
  989. }
  990. }
  991. /// <summary>
  992. /// 解除所有子实体。
  993. /// </summary>
  994. /// <param name="parentEntity">被解除的父实体。</param>
  995. public void DetachChildEntities(IEntity parentEntity)
  996. {
  997. DetachChildEntities(parentEntity, null);
  998. }
  999. /// <summary>
  1000. /// 解除所有子实体。
  1001. /// </summary>
  1002. /// <param name="parentEntity">被解除的父实体。</param>
  1003. /// <param name="userData">用户自定义数据。</param>
  1004. public void DetachChildEntities(IEntity parentEntity, object userData)
  1005. {
  1006. if (parentEntity == null)
  1007. {
  1008. throw new GameFrameworkException("Parent entity is invalid.");
  1009. }
  1010. DetachChildEntities(parentEntity.Id, userData);
  1011. }
  1012. /// <summary>
  1013. /// 获取实体信息。
  1014. /// </summary>
  1015. /// <param name="entityId">实体编号。</param>
  1016. /// <returns>实体信息。</returns>
  1017. private EntityInfo GetEntityInfo(int entityId)
  1018. {
  1019. EntityInfo entityInfo = null;
  1020. if (m_EntityInfos.TryGetValue(entityId, out entityInfo))
  1021. {
  1022. return entityInfo;
  1023. }
  1024. return null;
  1025. }
  1026. private void InternalShowEntity(int entityId, string entityAssetName, EntityGroup entityGroup, object entityInstance, bool isNewInstance, float duration, object userData)
  1027. {
  1028. try
  1029. {
  1030. IEntity entity = m_EntityHelper.CreateEntity(entityInstance, entityGroup, userData);
  1031. if (entity == null)
  1032. {
  1033. throw new GameFrameworkException("Can not create entity in entity helper.");
  1034. }
  1035. EntityInfo entityInfo = EntityInfo.Create(entity);
  1036. m_EntityInfos.Add(entityId, entityInfo);
  1037. entityInfo.Status = EntityStatus.WillInit;
  1038. entity.OnInit(entityId, entityAssetName, entityGroup, isNewInstance, userData);
  1039. entityInfo.Status = EntityStatus.Inited;
  1040. entityGroup.AddEntity(entity);
  1041. entityInfo.Status = EntityStatus.WillShow;
  1042. entity.OnShow(userData);
  1043. entityInfo.Status = EntityStatus.Showed;
  1044. if (m_ShowEntitySuccessEventHandler != null)
  1045. {
  1046. ShowEntitySuccessEventArgs showEntitySuccessEventArgs = ShowEntitySuccessEventArgs.Create(entity, duration, userData);
  1047. m_ShowEntitySuccessEventHandler(this, showEntitySuccessEventArgs);
  1048. ReferencePool.Release(showEntitySuccessEventArgs);
  1049. }
  1050. }
  1051. catch (Exception exception)
  1052. {
  1053. if (m_ShowEntityFailureEventHandler != null)
  1054. {
  1055. ShowEntityFailureEventArgs showEntityFailureEventArgs = ShowEntityFailureEventArgs.Create(entityId, entityAssetName, entityGroup.Name, exception.ToString(), userData);
  1056. m_ShowEntityFailureEventHandler(this, showEntityFailureEventArgs);
  1057. ReferencePool.Release(showEntityFailureEventArgs);
  1058. return;
  1059. }
  1060. throw;
  1061. }
  1062. }
  1063. private void InternalHideEntity(EntityInfo entityInfo, object userData)
  1064. {
  1065. while (entityInfo.ChildEntityCount > 0)
  1066. {
  1067. IEntity childEntity = entityInfo.GetChildEntity();
  1068. HideEntity(childEntity.Id, userData);
  1069. }
  1070. if (entityInfo.Status == EntityStatus.Hidden)
  1071. {
  1072. return;
  1073. }
  1074. IEntity entity = entityInfo.Entity;
  1075. DetachEntity(entity.Id, userData);
  1076. entityInfo.Status = EntityStatus.WillHide;
  1077. entity.OnHide(m_IsShutdown, userData);
  1078. entityInfo.Status = EntityStatus.Hidden;
  1079. EntityGroup entityGroup = (EntityGroup)entity.EntityGroup;
  1080. if (entityGroup == null)
  1081. {
  1082. throw new GameFrameworkException("Entity group is invalid.");
  1083. }
  1084. entityGroup.RemoveEntity(entity);
  1085. if (!m_EntityInfos.Remove(entity.Id))
  1086. {
  1087. throw new GameFrameworkException("Entity info is unmanaged.");
  1088. }
  1089. if (m_HideEntityCompleteEventHandler != null)
  1090. {
  1091. HideEntityCompleteEventArgs hideEntityCompleteEventArgs = HideEntityCompleteEventArgs.Create(entity.Id, entity.EntityAssetName, entityGroup, userData);
  1092. m_HideEntityCompleteEventHandler(this, hideEntityCompleteEventArgs);
  1093. ReferencePool.Release(hideEntityCompleteEventArgs);
  1094. }
  1095. m_RecycleQueue.Enqueue(entityInfo);
  1096. }
  1097. private void LoadAssetSuccessCallback(string entityAssetName, object entityAsset, float duration, object userData)
  1098. {
  1099. ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
  1100. if (showEntityInfo == null)
  1101. {
  1102. throw new GameFrameworkException("Show entity info is invalid.");
  1103. }
  1104. if (m_EntitiesToReleaseOnLoad.Contains(showEntityInfo.SerialId))
  1105. {
  1106. m_EntitiesToReleaseOnLoad.Remove(showEntityInfo.SerialId);
  1107. ReferencePool.Release(showEntityInfo);
  1108. m_EntityHelper.ReleaseEntity(entityAsset, null);
  1109. return;
  1110. }
  1111. m_EntitiesBeingLoaded.Remove(showEntityInfo.EntityId);
  1112. EntityInstanceObject entityInstanceObject = EntityInstanceObject.Create(entityAssetName, entityAsset, m_EntityHelper.InstantiateEntity(entityAsset), m_EntityHelper);
  1113. showEntityInfo.EntityGroup.RegisterEntityInstanceObject(entityInstanceObject, true);
  1114. InternalShowEntity(showEntityInfo.EntityId, entityAssetName, showEntityInfo.EntityGroup, entityInstanceObject.Target, true, duration, showEntityInfo.UserData);
  1115. ReferencePool.Release(showEntityInfo);
  1116. }
  1117. private void LoadAssetFailureCallback(string entityAssetName, LoadResourceStatus status, string errorMessage, object userData)
  1118. {
  1119. ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
  1120. if (showEntityInfo == null)
  1121. {
  1122. throw new GameFrameworkException("Show entity info is invalid.");
  1123. }
  1124. if (m_EntitiesToReleaseOnLoad.Contains(showEntityInfo.SerialId))
  1125. {
  1126. m_EntitiesToReleaseOnLoad.Remove(showEntityInfo.SerialId);
  1127. return;
  1128. }
  1129. m_EntitiesBeingLoaded.Remove(showEntityInfo.EntityId);
  1130. string appendErrorMessage = Utility.Text.Format("Load entity failure, asset name '{0}', status '{1}', error message '{2}'.", entityAssetName, status, errorMessage);
  1131. if (m_ShowEntityFailureEventHandler != null)
  1132. {
  1133. ShowEntityFailureEventArgs showEntityFailureEventArgs = ShowEntityFailureEventArgs.Create(showEntityInfo.EntityId, entityAssetName, showEntityInfo.EntityGroup.Name, appendErrorMessage, showEntityInfo.UserData);
  1134. m_ShowEntityFailureEventHandler(this, showEntityFailureEventArgs);
  1135. ReferencePool.Release(showEntityFailureEventArgs);
  1136. return;
  1137. }
  1138. throw new GameFrameworkException(appendErrorMessage);
  1139. }
  1140. private void LoadAssetUpdateCallback(string entityAssetName, float progress, object userData)
  1141. {
  1142. ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
  1143. if (showEntityInfo == null)
  1144. {
  1145. throw new GameFrameworkException("Show entity info is invalid.");
  1146. }
  1147. if (m_ShowEntityUpdateEventHandler != null)
  1148. {
  1149. ShowEntityUpdateEventArgs showEntityUpdateEventArgs = ShowEntityUpdateEventArgs.Create(showEntityInfo.EntityId, entityAssetName, showEntityInfo.EntityGroup.Name, progress, showEntityInfo.UserData);
  1150. m_ShowEntityUpdateEventHandler(this, showEntityUpdateEventArgs);
  1151. ReferencePool.Release(showEntityUpdateEventArgs);
  1152. }
  1153. }
  1154. private void LoadAssetDependencyAssetCallback(string entityAssetName, string dependencyAssetName, int loadedCount, int totalCount, object userData)
  1155. {
  1156. ShowEntityInfo showEntityInfo = (ShowEntityInfo)userData;
  1157. if (showEntityInfo == null)
  1158. {
  1159. throw new GameFrameworkException("Show entity info is invalid.");
  1160. }
  1161. if (m_ShowEntityDependencyAssetEventHandler != null)
  1162. {
  1163. ShowEntityDependencyAssetEventArgs showEntityDependencyAssetEventArgs = ShowEntityDependencyAssetEventArgs.Create(showEntityInfo.EntityId, entityAssetName, showEntityInfo.EntityGroup.Name, dependencyAssetName, loadedCount, totalCount, showEntityInfo.UserData);
  1164. m_ShowEntityDependencyAssetEventHandler(this, showEntityDependencyAssetEventArgs);
  1165. ReferencePool.Release(showEntityDependencyAssetEventArgs);
  1166. }
  1167. }
  1168. }
  1169. }