EMRenderSupport.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. ///
  8. /// </summary>
  9. public interface EMRenderTarget
  10. {
  11. int EM_sortingOrder { get; }
  12. void EM_BeforeUpdate();
  13. void EM_Update(UpdateContext context);
  14. void EM_Reload();
  15. }
  16. /// <summary>
  17. /// 这是一个在编辑状态下渲染UI的功能类。EM=Edit Mode
  18. /// </summary>
  19. public class EMRenderSupport
  20. {
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public static bool orderChanged;
  25. static UpdateContext _updateContext;
  26. static List<EMRenderTarget> _targets = new List<EMRenderTarget>();
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. public static bool packageListReady { get; private set; }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. public static bool hasTarget
  35. {
  36. get { return _targets.Count > 0; }
  37. }
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. /// <param name="value"></param>
  42. public static void Add(EMRenderTarget value)
  43. {
  44. if (!_targets.Contains(value))
  45. _targets.Add(value);
  46. orderChanged = true;
  47. }
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. /// <param name="value"></param>
  52. public static void Remove(EMRenderTarget value)
  53. {
  54. _targets.Remove(value);
  55. }
  56. /// <summary>
  57. /// 由StageCamera调用
  58. /// </summary>
  59. public static void Update()
  60. {
  61. if (Application.isPlaying)
  62. return;
  63. if (_updateContext == null)
  64. _updateContext = new UpdateContext();
  65. if (orderChanged)
  66. {
  67. _targets.Sort(CompareDepth);
  68. orderChanged = false;
  69. }
  70. int cnt = _targets.Count;
  71. for (int i = 0; i < cnt; i++)
  72. {
  73. EMRenderTarget panel = _targets[i];
  74. panel.EM_BeforeUpdate();
  75. }
  76. if (packageListReady)
  77. {
  78. _updateContext.Begin();
  79. for (int i = 0; i < cnt; i++)
  80. {
  81. EMRenderTarget panel = _targets[i];
  82. panel.EM_Update(_updateContext);
  83. }
  84. _updateContext.End();
  85. }
  86. }
  87. /// <summary>
  88. /// 当发生二进制重载时,或用户点击刷新菜单
  89. /// </summary>
  90. public static void Reload()
  91. {
  92. if (Application.isPlaying)
  93. return;
  94. UIConfig.ClearResourceRefs();
  95. UIConfig[] configs = GameObject.FindObjectsOfType<UIConfig>();
  96. foreach (UIConfig config in configs)
  97. config.Load();
  98. packageListReady = true;
  99. int cnt = _targets.Count;
  100. for (int i = 0; i < cnt; i++)
  101. {
  102. EMRenderTarget panel = _targets[i];
  103. panel.EM_Reload();
  104. }
  105. }
  106. static int CompareDepth(EMRenderTarget c1, EMRenderTarget c2)
  107. {
  108. return c1.EM_sortingOrder - c2.EM_sortingOrder;
  109. }
  110. }
  111. }