UIManager.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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.UI
  12. {
  13. /// <summary>
  14. /// 界面管理器。
  15. /// </summary>
  16. internal sealed partial class UIManager : GameFrameworkModule, IUIManager
  17. {
  18. private readonly Dictionary<string, UIGroup> m_UIGroups;
  19. private readonly Dictionary<int, string> m_UIFormsBeingLoaded;
  20. private readonly HashSet<int> m_UIFormsToReleaseOnLoad;
  21. private readonly Queue<IUIForm> m_RecycleQueue;
  22. private readonly LoadAssetCallbacks m_LoadAssetCallbacks;
  23. private IObjectPoolManager m_ObjectPoolManager;
  24. private IResourceManager m_ResourceManager;
  25. private IObjectPool<UIFormInstanceObject> m_InstancePool;
  26. private IUIFormHelper m_UIFormHelper;
  27. private int m_Serial;
  28. private bool m_IsShutdown;
  29. private EventHandler<OpenUIFormSuccessEventArgs> m_OpenUIFormSuccessEventHandler;
  30. private EventHandler<OpenUIFormFailureEventArgs> m_OpenUIFormFailureEventHandler;
  31. private EventHandler<OpenUIFormUpdateEventArgs> m_OpenUIFormUpdateEventHandler;
  32. private EventHandler<OpenUIFormDependencyAssetEventArgs> m_OpenUIFormDependencyAssetEventHandler;
  33. private EventHandler<CloseUIFormCompleteEventArgs> m_CloseUIFormCompleteEventHandler;
  34. /// <summary>
  35. /// 初始化界面管理器的新实例。
  36. /// </summary>
  37. public UIManager()
  38. {
  39. m_UIGroups = new Dictionary<string, UIGroup>(StringComparer.Ordinal);
  40. m_UIFormsBeingLoaded = new Dictionary<int, string>();
  41. m_UIFormsToReleaseOnLoad = new HashSet<int>();
  42. m_RecycleQueue = new Queue<IUIForm>();
  43. m_LoadAssetCallbacks = new LoadAssetCallbacks(LoadAssetSuccessCallback, LoadAssetFailureCallback, LoadAssetUpdateCallback, LoadAssetDependencyAssetCallback);
  44. m_ObjectPoolManager = null;
  45. m_ResourceManager = null;
  46. m_InstancePool = null;
  47. m_UIFormHelper = null;
  48. m_Serial = 0;
  49. m_IsShutdown = false;
  50. m_OpenUIFormSuccessEventHandler = null;
  51. m_OpenUIFormFailureEventHandler = null;
  52. m_OpenUIFormUpdateEventHandler = null;
  53. m_OpenUIFormDependencyAssetEventHandler = null;
  54. m_CloseUIFormCompleteEventHandler = null;
  55. }
  56. /// <summary>
  57. /// 获取界面组数量。
  58. /// </summary>
  59. public int UIGroupCount
  60. {
  61. get
  62. {
  63. return m_UIGroups.Count;
  64. }
  65. }
  66. /// <summary>
  67. /// 获取或设置界面实例对象池自动释放可释放对象的间隔秒数。
  68. /// </summary>
  69. public float InstanceAutoReleaseInterval
  70. {
  71. get
  72. {
  73. return m_InstancePool.AutoReleaseInterval;
  74. }
  75. set
  76. {
  77. m_InstancePool.AutoReleaseInterval = value;
  78. }
  79. }
  80. /// <summary>
  81. /// 获取或设置界面实例对象池的容量。
  82. /// </summary>
  83. public int InstanceCapacity
  84. {
  85. get
  86. {
  87. return m_InstancePool.Capacity;
  88. }
  89. set
  90. {
  91. m_InstancePool.Capacity = value;
  92. }
  93. }
  94. /// <summary>
  95. /// 获取或设置界面实例对象池对象过期秒数。
  96. /// </summary>
  97. public float InstanceExpireTime
  98. {
  99. get
  100. {
  101. return m_InstancePool.ExpireTime;
  102. }
  103. set
  104. {
  105. m_InstancePool.ExpireTime = value;
  106. }
  107. }
  108. /// <summary>
  109. /// 获取或设置界面实例对象池的优先级。
  110. /// </summary>
  111. public int InstancePriority
  112. {
  113. get
  114. {
  115. return m_InstancePool.Priority;
  116. }
  117. set
  118. {
  119. m_InstancePool.Priority = value;
  120. }
  121. }
  122. /// <summary>
  123. /// 打开界面成功事件。
  124. /// </summary>
  125. public event EventHandler<OpenUIFormSuccessEventArgs> OpenUIFormSuccess
  126. {
  127. add
  128. {
  129. m_OpenUIFormSuccessEventHandler += value;
  130. }
  131. remove
  132. {
  133. m_OpenUIFormSuccessEventHandler -= value;
  134. }
  135. }
  136. /// <summary>
  137. /// 打开界面失败事件。
  138. /// </summary>
  139. public event EventHandler<OpenUIFormFailureEventArgs> OpenUIFormFailure
  140. {
  141. add
  142. {
  143. m_OpenUIFormFailureEventHandler += value;
  144. }
  145. remove
  146. {
  147. m_OpenUIFormFailureEventHandler -= value;
  148. }
  149. }
  150. /// <summary>
  151. /// 打开界面更新事件。
  152. /// </summary>
  153. public event EventHandler<OpenUIFormUpdateEventArgs> OpenUIFormUpdate
  154. {
  155. add
  156. {
  157. m_OpenUIFormUpdateEventHandler += value;
  158. }
  159. remove
  160. {
  161. m_OpenUIFormUpdateEventHandler -= value;
  162. }
  163. }
  164. /// <summary>
  165. /// 打开界面时加载依赖资源事件。
  166. /// </summary>
  167. public event EventHandler<OpenUIFormDependencyAssetEventArgs> OpenUIFormDependencyAsset
  168. {
  169. add
  170. {
  171. m_OpenUIFormDependencyAssetEventHandler += value;
  172. }
  173. remove
  174. {
  175. m_OpenUIFormDependencyAssetEventHandler -= value;
  176. }
  177. }
  178. /// <summary>
  179. /// 关闭界面完成事件。
  180. /// </summary>
  181. public event EventHandler<CloseUIFormCompleteEventArgs> CloseUIFormComplete
  182. {
  183. add
  184. {
  185. m_CloseUIFormCompleteEventHandler += value;
  186. }
  187. remove
  188. {
  189. m_CloseUIFormCompleteEventHandler -= value;
  190. }
  191. }
  192. /// <summary>
  193. /// 界面管理器轮询。
  194. /// </summary>
  195. /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
  196. /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
  197. internal override void Update(float elapseSeconds, float realElapseSeconds)
  198. {
  199. while (m_RecycleQueue.Count > 0)
  200. {
  201. IUIForm uiForm = m_RecycleQueue.Dequeue();
  202. uiForm.OnRecycle();
  203. m_InstancePool.Unspawn(uiForm.Handle);
  204. }
  205. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  206. {
  207. uiGroup.Value.Update(elapseSeconds, realElapseSeconds);
  208. }
  209. }
  210. /// <summary>
  211. /// 关闭并清理界面管理器。
  212. /// </summary>
  213. internal override void Shutdown()
  214. {
  215. m_IsShutdown = true;
  216. CloseAllLoadedUIForms();
  217. m_UIGroups.Clear();
  218. m_UIFormsBeingLoaded.Clear();
  219. m_UIFormsToReleaseOnLoad.Clear();
  220. m_RecycleQueue.Clear();
  221. }
  222. /// <summary>
  223. /// 设置对象池管理器。
  224. /// </summary>
  225. /// <param name="objectPoolManager">对象池管理器。</param>
  226. public void SetObjectPoolManager(IObjectPoolManager objectPoolManager)
  227. {
  228. if (objectPoolManager == null)
  229. {
  230. throw new GameFrameworkException("Object pool manager is invalid.");
  231. }
  232. m_ObjectPoolManager = objectPoolManager;
  233. m_InstancePool = m_ObjectPoolManager.CreateSingleSpawnObjectPool<UIFormInstanceObject>("UI Instance Pool");
  234. }
  235. /// <summary>
  236. /// 设置资源管理器。
  237. /// </summary>
  238. /// <param name="resourceManager">资源管理器。</param>
  239. public void SetResourceManager(IResourceManager resourceManager)
  240. {
  241. if (resourceManager == null)
  242. {
  243. throw new GameFrameworkException("Resource manager is invalid.");
  244. }
  245. m_ResourceManager = resourceManager;
  246. }
  247. /// <summary>
  248. /// 设置界面辅助器。
  249. /// </summary>
  250. /// <param name="uiFormHelper">界面辅助器。</param>
  251. public void SetUIFormHelper(IUIFormHelper uiFormHelper)
  252. {
  253. if (uiFormHelper == null)
  254. {
  255. throw new GameFrameworkException("UI form helper is invalid.");
  256. }
  257. m_UIFormHelper = uiFormHelper;
  258. }
  259. /// <summary>
  260. /// 是否存在界面组。
  261. /// </summary>
  262. /// <param name="uiGroupName">界面组名称。</param>
  263. /// <returns>是否存在界面组。</returns>
  264. public bool HasUIGroup(string uiGroupName)
  265. {
  266. if (string.IsNullOrEmpty(uiGroupName))
  267. {
  268. throw new GameFrameworkException("UI group name is invalid.");
  269. }
  270. return m_UIGroups.ContainsKey(uiGroupName);
  271. }
  272. /// <summary>
  273. /// 获取界面组。
  274. /// </summary>
  275. /// <param name="uiGroupName">界面组名称。</param>
  276. /// <returns>要获取的界面组。</returns>
  277. public IUIGroup GetUIGroup(string uiGroupName)
  278. {
  279. if (string.IsNullOrEmpty(uiGroupName))
  280. {
  281. throw new GameFrameworkException("UI group name is invalid.");
  282. }
  283. UIGroup uiGroup = null;
  284. if (m_UIGroups.TryGetValue(uiGroupName, out uiGroup))
  285. {
  286. return uiGroup;
  287. }
  288. return null;
  289. }
  290. /// <summary>
  291. /// 获取所有界面组。
  292. /// </summary>
  293. /// <returns>所有界面组。</returns>
  294. public IUIGroup[] GetAllUIGroups()
  295. {
  296. int index = 0;
  297. IUIGroup[] results = new IUIGroup[m_UIGroups.Count];
  298. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  299. {
  300. results[index++] = uiGroup.Value;
  301. }
  302. return results;
  303. }
  304. /// <summary>
  305. /// 获取所有界面组。
  306. /// </summary>
  307. /// <param name="results">所有界面组。</param>
  308. public void GetAllUIGroups(List<IUIGroup> results)
  309. {
  310. if (results == null)
  311. {
  312. throw new GameFrameworkException("Results is invalid.");
  313. }
  314. results.Clear();
  315. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  316. {
  317. results.Add(uiGroup.Value);
  318. }
  319. }
  320. /// <summary>
  321. /// 增加界面组。
  322. /// </summary>
  323. /// <param name="uiGroupName">界面组名称。</param>
  324. /// <param name="uiGroupHelper">界面组辅助器。</param>
  325. /// <returns>是否增加界面组成功。</returns>
  326. public bool AddUIGroup(string uiGroupName, IUIGroupHelper uiGroupHelper)
  327. {
  328. return AddUIGroup(uiGroupName, 0, uiGroupHelper);
  329. }
  330. /// <summary>
  331. /// 增加界面组。
  332. /// </summary>
  333. /// <param name="uiGroupName">界面组名称。</param>
  334. /// <param name="uiGroupDepth">界面组深度。</param>
  335. /// <param name="uiGroupHelper">界面组辅助器。</param>
  336. /// <returns>是否增加界面组成功。</returns>
  337. public bool AddUIGroup(string uiGroupName, int uiGroupDepth, IUIGroupHelper uiGroupHelper)
  338. {
  339. if (string.IsNullOrEmpty(uiGroupName))
  340. {
  341. throw new GameFrameworkException("UI group name is invalid.");
  342. }
  343. if (uiGroupHelper == null)
  344. {
  345. throw new GameFrameworkException("UI group helper is invalid.");
  346. }
  347. if (HasUIGroup(uiGroupName))
  348. {
  349. return false;
  350. }
  351. m_UIGroups.Add(uiGroupName, new UIGroup(uiGroupName, uiGroupDepth, uiGroupHelper));
  352. return true;
  353. }
  354. /// <summary>
  355. /// 是否存在界面。
  356. /// </summary>
  357. /// <param name="serialId">界面序列编号。</param>
  358. /// <returns>是否存在界面。</returns>
  359. public bool HasUIForm(int serialId)
  360. {
  361. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  362. {
  363. if (uiGroup.Value.HasUIForm(serialId))
  364. {
  365. return true;
  366. }
  367. }
  368. return false;
  369. }
  370. /// <summary>
  371. /// 是否存在界面。
  372. /// </summary>
  373. /// <param name="uiFormAssetName">界面资源名称。</param>
  374. /// <returns>是否存在界面。</returns>
  375. public bool HasUIForm(string uiFormAssetName)
  376. {
  377. if (string.IsNullOrEmpty(uiFormAssetName))
  378. {
  379. throw new GameFrameworkException("UI form asset name is invalid.");
  380. }
  381. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  382. {
  383. if (uiGroup.Value.HasUIForm(uiFormAssetName))
  384. {
  385. return true;
  386. }
  387. }
  388. return false;
  389. }
  390. /// <summary>
  391. /// 获取界面。
  392. /// </summary>
  393. /// <param name="serialId">界面序列编号。</param>
  394. /// <returns>要获取的界面。</returns>
  395. public IUIForm GetUIForm(int serialId)
  396. {
  397. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  398. {
  399. IUIForm uiForm = uiGroup.Value.GetUIForm(serialId);
  400. if (uiForm != null)
  401. {
  402. return uiForm;
  403. }
  404. }
  405. return null;
  406. }
  407. /// <summary>
  408. /// 获取界面。
  409. /// </summary>
  410. /// <param name="uiFormAssetName">界面资源名称。</param>
  411. /// <returns>要获取的界面。</returns>
  412. public IUIForm GetUIForm(string uiFormAssetName)
  413. {
  414. if (string.IsNullOrEmpty(uiFormAssetName))
  415. {
  416. throw new GameFrameworkException("UI form asset name is invalid.");
  417. }
  418. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  419. {
  420. IUIForm uiForm = uiGroup.Value.GetUIForm(uiFormAssetName);
  421. if (uiForm != null)
  422. {
  423. return uiForm;
  424. }
  425. }
  426. return null;
  427. }
  428. /// <summary>
  429. /// 获取界面。
  430. /// </summary>
  431. /// <param name="uiFormAssetName">界面资源名称。</param>
  432. /// <returns>要获取的界面。</returns>
  433. public IUIForm[] GetUIForms(string uiFormAssetName)
  434. {
  435. if (string.IsNullOrEmpty(uiFormAssetName))
  436. {
  437. throw new GameFrameworkException("UI form asset name is invalid.");
  438. }
  439. List<IUIForm> results = new List<IUIForm>();
  440. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  441. {
  442. results.AddRange(uiGroup.Value.GetUIForms(uiFormAssetName));
  443. }
  444. return results.ToArray();
  445. }
  446. /// <summary>
  447. /// 获取界面。
  448. /// </summary>
  449. /// <param name="uiFormAssetName">界面资源名称。</param>
  450. /// <param name="results">要获取的界面。</param>
  451. public void GetUIForms(string uiFormAssetName, List<IUIForm> results)
  452. {
  453. if (string.IsNullOrEmpty(uiFormAssetName))
  454. {
  455. throw new GameFrameworkException("UI form asset name is invalid.");
  456. }
  457. if (results == null)
  458. {
  459. throw new GameFrameworkException("Results is invalid.");
  460. }
  461. results.Clear();
  462. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  463. {
  464. uiGroup.Value.InternalGetUIForms(uiFormAssetName, results);
  465. }
  466. }
  467. /// <summary>
  468. /// 获取所有已加载的界面。
  469. /// </summary>
  470. /// <returns>所有已加载的界面。</returns>
  471. public IUIForm[] GetAllLoadedUIForms()
  472. {
  473. List<IUIForm> results = new List<IUIForm>();
  474. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  475. {
  476. results.AddRange(uiGroup.Value.GetAllUIForms());
  477. }
  478. return results.ToArray();
  479. }
  480. /// <summary>
  481. /// 获取所有已加载的界面。
  482. /// </summary>
  483. /// <param name="results">所有已加载的界面。</param>
  484. public void GetAllLoadedUIForms(List<IUIForm> results)
  485. {
  486. if (results == null)
  487. {
  488. throw new GameFrameworkException("Results is invalid.");
  489. }
  490. results.Clear();
  491. foreach (KeyValuePair<string, UIGroup> uiGroup in m_UIGroups)
  492. {
  493. uiGroup.Value.InternalGetAllUIForms(results);
  494. }
  495. }
  496. /// <summary>
  497. /// 获取所有正在加载界面的序列编号。
  498. /// </summary>
  499. /// <returns>所有正在加载界面的序列编号。</returns>
  500. public int[] GetAllLoadingUIFormSerialIds()
  501. {
  502. int index = 0;
  503. int[] results = new int[m_UIFormsBeingLoaded.Count];
  504. foreach (KeyValuePair<int, string> uiFormBeingLoaded in m_UIFormsBeingLoaded)
  505. {
  506. results[index++] = uiFormBeingLoaded.Key;
  507. }
  508. return results;
  509. }
  510. /// <summary>
  511. /// 获取所有正在加载界面的序列编号。
  512. /// </summary>
  513. /// <param name="results">所有正在加载界面的序列编号。</param>
  514. public void GetAllLoadingUIFormSerialIds(List<int> results)
  515. {
  516. if (results == null)
  517. {
  518. throw new GameFrameworkException("Results is invalid.");
  519. }
  520. results.Clear();
  521. foreach (KeyValuePair<int, string> uiFormBeingLoaded in m_UIFormsBeingLoaded)
  522. {
  523. results.Add(uiFormBeingLoaded.Key);
  524. }
  525. }
  526. /// <summary>
  527. /// 是否正在加载界面。
  528. /// </summary>
  529. /// <param name="serialId">界面序列编号。</param>
  530. /// <returns>是否正在加载界面。</returns>
  531. public bool IsLoadingUIForm(int serialId)
  532. {
  533. return m_UIFormsBeingLoaded.ContainsKey(serialId);
  534. }
  535. /// <summary>
  536. /// 是否正在加载界面。
  537. /// </summary>
  538. /// <param name="uiFormAssetName">界面资源名称。</param>
  539. /// <returns>是否正在加载界面。</returns>
  540. public bool IsLoadingUIForm(string uiFormAssetName)
  541. {
  542. if (string.IsNullOrEmpty(uiFormAssetName))
  543. {
  544. throw new GameFrameworkException("UI form asset name is invalid.");
  545. }
  546. return m_UIFormsBeingLoaded.ContainsValue(uiFormAssetName);
  547. }
  548. /// <summary>
  549. /// 是否是合法的界面。
  550. /// </summary>
  551. /// <param name="uiForm">界面。</param>
  552. /// <returns>界面是否合法。</returns>
  553. public bool IsValidUIForm(IUIForm uiForm)
  554. {
  555. if (uiForm == null)
  556. {
  557. return false;
  558. }
  559. return HasUIForm(uiForm.SerialId);
  560. }
  561. /// <summary>
  562. /// 打开界面。
  563. /// </summary>
  564. /// <param name="uiFormAssetName">界面资源名称。</param>
  565. /// <param name="uiGroupName">界面组名称。</param>
  566. /// <returns>界面的序列编号。</returns>
  567. public int OpenUIForm(string uiFormAssetName, string uiGroupName)
  568. {
  569. return OpenUIForm(uiFormAssetName, uiGroupName, Constant.DefaultPriority, false, null);
  570. }
  571. /// <summary>
  572. /// 打开界面。
  573. /// </summary>
  574. /// <param name="uiFormAssetName">界面资源名称。</param>
  575. /// <param name="uiGroupName">界面组名称。</param>
  576. /// <param name="priority">加载界面资源的优先级。</param>
  577. /// <returns>界面的序列编号。</returns>
  578. public int OpenUIForm(string uiFormAssetName, string uiGroupName, int priority)
  579. {
  580. return OpenUIForm(uiFormAssetName, uiGroupName, priority, false, null);
  581. }
  582. /// <summary>
  583. /// 打开界面。
  584. /// </summary>
  585. /// <param name="uiFormAssetName">界面资源名称。</param>
  586. /// <param name="uiGroupName">界面组名称。</param>
  587. /// <param name="pauseCoveredUIForm">是否暂停被覆盖的界面。</param>
  588. /// <returns>界面的序列编号。</returns>
  589. public int OpenUIForm(string uiFormAssetName, string uiGroupName, bool pauseCoveredUIForm)
  590. {
  591. return OpenUIForm(uiFormAssetName, uiGroupName, Constant.DefaultPriority, pauseCoveredUIForm, null);
  592. }
  593. /// <summary>
  594. /// 打开界面。
  595. /// </summary>
  596. /// <param name="uiFormAssetName">界面资源名称。</param>
  597. /// <param name="uiGroupName">界面组名称。</param>
  598. /// <param name="userData">用户自定义数据。</param>
  599. /// <returns>界面的序列编号。</returns>
  600. public int OpenUIForm(string uiFormAssetName, string uiGroupName, object userData)
  601. {
  602. return OpenUIForm(uiFormAssetName, uiGroupName, Constant.DefaultPriority, false, userData);
  603. }
  604. /// <summary>
  605. /// 打开界面。
  606. /// </summary>
  607. /// <param name="uiFormAssetName">界面资源名称。</param>
  608. /// <param name="uiGroupName">界面组名称。</param>
  609. /// <param name="priority">加载界面资源的优先级。</param>
  610. /// <param name="pauseCoveredUIForm">是否暂停被覆盖的界面。</param>
  611. /// <returns>界面的序列编号。</returns>
  612. public int OpenUIForm(string uiFormAssetName, string uiGroupName, int priority, bool pauseCoveredUIForm)
  613. {
  614. return OpenUIForm(uiFormAssetName, uiGroupName, priority, pauseCoveredUIForm, null);
  615. }
  616. /// <summary>
  617. /// 打开界面。
  618. /// </summary>
  619. /// <param name="uiFormAssetName">界面资源名称。</param>
  620. /// <param name="uiGroupName">界面组名称。</param>
  621. /// <param name="priority">加载界面资源的优先级。</param>
  622. /// <param name="userData">用户自定义数据。</param>
  623. /// <returns>界面的序列编号。</returns>
  624. public int OpenUIForm(string uiFormAssetName, string uiGroupName, int priority, object userData)
  625. {
  626. return OpenUIForm(uiFormAssetName, uiGroupName, priority, false, userData);
  627. }
  628. /// <summary>
  629. /// 打开界面。
  630. /// </summary>
  631. /// <param name="uiFormAssetName">界面资源名称。</param>
  632. /// <param name="uiGroupName">界面组名称。</param>
  633. /// <param name="pauseCoveredUIForm">是否暂停被覆盖的界面。</param>
  634. /// <param name="userData">用户自定义数据。</param>
  635. /// <returns>界面的序列编号。</returns>
  636. public int OpenUIForm(string uiFormAssetName, string uiGroupName, bool pauseCoveredUIForm, object userData)
  637. {
  638. return OpenUIForm(uiFormAssetName, uiGroupName, Constant.DefaultPriority, pauseCoveredUIForm, userData);
  639. }
  640. /// <summary>
  641. /// 打开界面。
  642. /// </summary>
  643. /// <param name="uiFormAssetName">界面资源名称。</param>
  644. /// <param name="uiGroupName">界面组名称。</param>
  645. /// <param name="priority">加载界面资源的优先级。</param>
  646. /// <param name="pauseCoveredUIForm">是否暂停被覆盖的界面。</param>
  647. /// <param name="userData">用户自定义数据。</param>
  648. /// <returns>界面的序列编号。</returns>
  649. public int OpenUIForm(string uiFormAssetName, string uiGroupName, int priority, bool pauseCoveredUIForm, object userData)
  650. {
  651. if (m_ResourceManager == null)
  652. {
  653. throw new GameFrameworkException("You must set resource manager first.");
  654. }
  655. if (m_UIFormHelper == null)
  656. {
  657. throw new GameFrameworkException("You must set UI form helper first.");
  658. }
  659. if (string.IsNullOrEmpty(uiFormAssetName))
  660. {
  661. throw new GameFrameworkException("UI form asset name is invalid.");
  662. }
  663. if (string.IsNullOrEmpty(uiGroupName))
  664. {
  665. throw new GameFrameworkException("UI group name is invalid.");
  666. }
  667. UIGroup uiGroup = (UIGroup)GetUIGroup(uiGroupName);
  668. if (uiGroup == null)
  669. {
  670. throw new GameFrameworkException(Utility.Text.Format("UI group '{0}' is not exist.", uiGroupName));
  671. }
  672. int serialId = ++m_Serial;
  673. UIFormInstanceObject uiFormInstanceObject = m_InstancePool.Spawn(uiFormAssetName);
  674. if (uiFormInstanceObject == null)
  675. {
  676. m_UIFormsBeingLoaded.Add(serialId, uiFormAssetName);
  677. m_ResourceManager.LoadAsset(uiFormAssetName, priority, m_LoadAssetCallbacks, OpenUIFormInfo.Create(serialId, uiGroup, pauseCoveredUIForm, userData));
  678. }
  679. else
  680. {
  681. InternalOpenUIForm(serialId, uiFormAssetName, uiGroup, uiFormInstanceObject.Target, pauseCoveredUIForm, false, 0f, userData);
  682. }
  683. return serialId;
  684. }
  685. /// <summary>
  686. /// 关闭界面。
  687. /// </summary>
  688. /// <param name="serialId">要关闭界面的序列编号。</param>
  689. public void CloseUIForm(int serialId)
  690. {
  691. CloseUIForm(serialId, null);
  692. }
  693. /// <summary>
  694. /// 关闭界面。
  695. /// </summary>
  696. /// <param name="serialId">要关闭界面的序列编号。</param>
  697. /// <param name="userData">用户自定义数据。</param>
  698. public void CloseUIForm(int serialId, object userData)
  699. {
  700. if (IsLoadingUIForm(serialId))
  701. {
  702. m_UIFormsToReleaseOnLoad.Add(serialId);
  703. m_UIFormsBeingLoaded.Remove(serialId);
  704. return;
  705. }
  706. IUIForm uiForm = GetUIForm(serialId);
  707. if (uiForm == null)
  708. {
  709. throw new GameFrameworkException(Utility.Text.Format("Can not find UI form '{0}'.", serialId));
  710. }
  711. CloseUIForm(uiForm, userData);
  712. }
  713. /// <summary>
  714. /// 关闭界面。
  715. /// </summary>
  716. /// <param name="uiForm">要关闭的界面。</param>
  717. public void CloseUIForm(IUIForm uiForm)
  718. {
  719. CloseUIForm(uiForm, null);
  720. }
  721. /// <summary>
  722. /// 关闭界面。
  723. /// </summary>
  724. /// <param name="uiForm">要关闭的界面。</param>
  725. /// <param name="userData">用户自定义数据。</param>
  726. public void CloseUIForm(IUIForm uiForm, object userData)
  727. {
  728. if (uiForm == null)
  729. {
  730. throw new GameFrameworkException("UI form is invalid.");
  731. }
  732. UIGroup uiGroup = (UIGroup)uiForm.UIGroup;
  733. if (uiGroup == null)
  734. {
  735. throw new GameFrameworkException("UI group is invalid.");
  736. }
  737. uiGroup.RemoveUIForm(uiForm);
  738. uiForm.OnClose(m_IsShutdown, userData);
  739. uiGroup.Refresh();
  740. if (m_CloseUIFormCompleteEventHandler != null)
  741. {
  742. CloseUIFormCompleteEventArgs closeUIFormCompleteEventArgs = CloseUIFormCompleteEventArgs.Create(uiForm.SerialId, uiForm.UIFormAssetName, uiGroup, userData);
  743. m_CloseUIFormCompleteEventHandler(this, closeUIFormCompleteEventArgs);
  744. ReferencePool.Release(closeUIFormCompleteEventArgs);
  745. }
  746. m_RecycleQueue.Enqueue(uiForm);
  747. }
  748. /// <summary>
  749. /// 关闭所有已加载的界面。
  750. /// </summary>
  751. public void CloseAllLoadedUIForms()
  752. {
  753. CloseAllLoadedUIForms(null);
  754. }
  755. /// <summary>
  756. /// 关闭所有已加载的界面。
  757. /// </summary>
  758. /// <param name="userData">用户自定义数据。</param>
  759. public void CloseAllLoadedUIForms(object userData)
  760. {
  761. IUIForm[] uiForms = GetAllLoadedUIForms();
  762. foreach (IUIForm uiForm in uiForms)
  763. {
  764. if (!HasUIForm(uiForm.SerialId))
  765. {
  766. continue;
  767. }
  768. CloseUIForm(uiForm, userData);
  769. }
  770. }
  771. /// <summary>
  772. /// 关闭所有正在加载的界面。
  773. /// </summary>
  774. public void CloseAllLoadingUIForms()
  775. {
  776. foreach (KeyValuePair<int, string> uiFormBeingLoaded in m_UIFormsBeingLoaded)
  777. {
  778. m_UIFormsToReleaseOnLoad.Add(uiFormBeingLoaded.Key);
  779. }
  780. m_UIFormsBeingLoaded.Clear();
  781. }
  782. /// <summary>
  783. /// 激活界面。
  784. /// </summary>
  785. /// <param name="uiForm">要激活的界面。</param>
  786. public void RefocusUIForm(IUIForm uiForm)
  787. {
  788. RefocusUIForm(uiForm, null);
  789. }
  790. /// <summary>
  791. /// 激活界面。
  792. /// </summary>
  793. /// <param name="uiForm">要激活的界面。</param>
  794. /// <param name="userData">用户自定义数据。</param>
  795. public void RefocusUIForm(IUIForm uiForm, object userData)
  796. {
  797. if (uiForm == null)
  798. {
  799. throw new GameFrameworkException("UI form is invalid.");
  800. }
  801. UIGroup uiGroup = (UIGroup)uiForm.UIGroup;
  802. if (uiGroup == null)
  803. {
  804. throw new GameFrameworkException("UI group is invalid.");
  805. }
  806. uiGroup.RefocusUIForm(uiForm, userData);
  807. uiGroup.Refresh();
  808. uiForm.OnRefocus(userData);
  809. }
  810. /// <summary>
  811. /// 设置界面实例是否被加锁。
  812. /// </summary>
  813. /// <param name="uiFormInstance">要设置是否被加锁的界面实例。</param>
  814. /// <param name="locked">界面实例是否被加锁。</param>
  815. public void SetUIFormInstanceLocked(object uiFormInstance, bool locked)
  816. {
  817. if (uiFormInstance == null)
  818. {
  819. throw new GameFrameworkException("UI form instance is invalid.");
  820. }
  821. m_InstancePool.SetLocked(uiFormInstance, locked);
  822. }
  823. /// <summary>
  824. /// 设置界面实例的优先级。
  825. /// </summary>
  826. /// <param name="uiFormInstance">要设置优先级的界面实例。</param>
  827. /// <param name="priority">界面实例优先级。</param>
  828. public void SetUIFormInstancePriority(object uiFormInstance, int priority)
  829. {
  830. if (uiFormInstance == null)
  831. {
  832. throw new GameFrameworkException("UI form instance is invalid.");
  833. }
  834. m_InstancePool.SetPriority(uiFormInstance, priority);
  835. }
  836. private void InternalOpenUIForm(int serialId, string uiFormAssetName, UIGroup uiGroup, object uiFormInstance, bool pauseCoveredUIForm, bool isNewInstance, float duration, object userData)
  837. {
  838. try
  839. {
  840. IUIForm uiForm = m_UIFormHelper.CreateUIForm(uiFormInstance, uiGroup, userData);
  841. if (uiForm == null)
  842. {
  843. throw new GameFrameworkException("Can not create UI form in UI form helper.");
  844. }
  845. uiForm.OnInit(serialId, uiFormAssetName, uiGroup, pauseCoveredUIForm, isNewInstance, userData);
  846. uiGroup.AddUIForm(uiForm);
  847. uiForm.OnOpen(userData);
  848. uiGroup.Refresh();
  849. if (m_OpenUIFormSuccessEventHandler != null)
  850. {
  851. OpenUIFormSuccessEventArgs openUIFormSuccessEventArgs = OpenUIFormSuccessEventArgs.Create(uiForm, duration, userData);
  852. m_OpenUIFormSuccessEventHandler(this, openUIFormSuccessEventArgs);
  853. ReferencePool.Release(openUIFormSuccessEventArgs);
  854. }
  855. }
  856. catch (Exception exception)
  857. {
  858. if (m_OpenUIFormFailureEventHandler != null)
  859. {
  860. OpenUIFormFailureEventArgs openUIFormFailureEventArgs = OpenUIFormFailureEventArgs.Create(serialId, uiFormAssetName, uiGroup.Name, pauseCoveredUIForm, exception.ToString(), userData);
  861. m_OpenUIFormFailureEventHandler(this, openUIFormFailureEventArgs);
  862. ReferencePool.Release(openUIFormFailureEventArgs);
  863. return;
  864. }
  865. throw;
  866. }
  867. }
  868. private void LoadAssetSuccessCallback(string uiFormAssetName, object uiFormAsset, float duration, object userData)
  869. {
  870. OpenUIFormInfo openUIFormInfo = (OpenUIFormInfo)userData;
  871. if (openUIFormInfo == null)
  872. {
  873. throw new GameFrameworkException("Open UI form info is invalid.");
  874. }
  875. if (m_UIFormsToReleaseOnLoad.Contains(openUIFormInfo.SerialId))
  876. {
  877. m_UIFormsToReleaseOnLoad.Remove(openUIFormInfo.SerialId);
  878. ReferencePool.Release(openUIFormInfo);
  879. m_UIFormHelper.ReleaseUIForm(uiFormAsset, null);
  880. return;
  881. }
  882. m_UIFormsBeingLoaded.Remove(openUIFormInfo.SerialId);
  883. UIFormInstanceObject uiFormInstanceObject = UIFormInstanceObject.Create(uiFormAssetName, uiFormAsset, m_UIFormHelper.InstantiateUIForm(uiFormAsset), m_UIFormHelper);
  884. m_InstancePool.Register(uiFormInstanceObject, true);
  885. InternalOpenUIForm(openUIFormInfo.SerialId, uiFormAssetName, openUIFormInfo.UIGroup, uiFormInstanceObject.Target, openUIFormInfo.PauseCoveredUIForm, true, duration, openUIFormInfo.UserData);
  886. ReferencePool.Release(openUIFormInfo);
  887. }
  888. private void LoadAssetFailureCallback(string uiFormAssetName, LoadResourceStatus status, string errorMessage, object userData)
  889. {
  890. OpenUIFormInfo openUIFormInfo = (OpenUIFormInfo)userData;
  891. if (openUIFormInfo == null)
  892. {
  893. throw new GameFrameworkException("Open UI form info is invalid.");
  894. }
  895. if (m_UIFormsToReleaseOnLoad.Contains(openUIFormInfo.SerialId))
  896. {
  897. m_UIFormsToReleaseOnLoad.Remove(openUIFormInfo.SerialId);
  898. return;
  899. }
  900. m_UIFormsBeingLoaded.Remove(openUIFormInfo.SerialId);
  901. string appendErrorMessage = Utility.Text.Format("Load UI form failure, asset name '{0}', status '{1}', error message '{2}'.", uiFormAssetName, status, errorMessage);
  902. if (m_OpenUIFormFailureEventHandler != null)
  903. {
  904. OpenUIFormFailureEventArgs openUIFormFailureEventArgs = OpenUIFormFailureEventArgs.Create(openUIFormInfo.SerialId, uiFormAssetName, openUIFormInfo.UIGroup.Name, openUIFormInfo.PauseCoveredUIForm, appendErrorMessage, openUIFormInfo.UserData);
  905. m_OpenUIFormFailureEventHandler(this, openUIFormFailureEventArgs);
  906. ReferencePool.Release(openUIFormFailureEventArgs);
  907. return;
  908. }
  909. throw new GameFrameworkException(appendErrorMessage);
  910. }
  911. private void LoadAssetUpdateCallback(string uiFormAssetName, float progress, object userData)
  912. {
  913. OpenUIFormInfo openUIFormInfo = (OpenUIFormInfo)userData;
  914. if (openUIFormInfo == null)
  915. {
  916. throw new GameFrameworkException("Open UI form info is invalid.");
  917. }
  918. if (m_OpenUIFormUpdateEventHandler != null)
  919. {
  920. OpenUIFormUpdateEventArgs openUIFormUpdateEventArgs = OpenUIFormUpdateEventArgs.Create(openUIFormInfo.SerialId, uiFormAssetName, openUIFormInfo.UIGroup.Name, openUIFormInfo.PauseCoveredUIForm, progress, openUIFormInfo.UserData);
  921. m_OpenUIFormUpdateEventHandler(this, openUIFormUpdateEventArgs);
  922. ReferencePool.Release(openUIFormUpdateEventArgs);
  923. }
  924. }
  925. private void LoadAssetDependencyAssetCallback(string uiFormAssetName, string dependencyAssetName, int loadedCount, int totalCount, object userData)
  926. {
  927. OpenUIFormInfo openUIFormInfo = (OpenUIFormInfo)userData;
  928. if (openUIFormInfo == null)
  929. {
  930. throw new GameFrameworkException("Open UI form info is invalid.");
  931. }
  932. if (m_OpenUIFormDependencyAssetEventHandler != null)
  933. {
  934. OpenUIFormDependencyAssetEventArgs openUIFormDependencyAssetEventArgs = OpenUIFormDependencyAssetEventArgs.Create(openUIFormInfo.SerialId, uiFormAssetName, openUIFormInfo.UIGroup.Name, openUIFormInfo.PauseCoveredUIForm, dependencyAssetName, loadedCount, totalCount, openUIFormInfo.UserData);
  935. m_OpenUIFormDependencyAssetEventHandler(this, openUIFormDependencyAssetEventArgs);
  936. ReferencePool.Release(openUIFormDependencyAssetEventArgs);
  937. }
  938. }
  939. }
  940. }