Unity 框架

UIHighlight.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using DG.Tweening.Core;
  5. using DG.Tweening.Plugins.Options;
  6. using DG.Tweening;
  7. namespace TFramework
  8. {
  9. /// <summary>
  10. /// UI高亮
  11. /// </summary>
  12. public static class UIHighlight
  13. {
  14. private static Dictionary<Graphic, TweenerCore<Color, Color, ColorOptions>> Graphics = new Dictionary<Graphic, TweenerCore<Color, Color, ColorOptions>>();
  15. private static Dictionary<Selectable, TweenerCore<Color, Color, ColorOptions>> Selectables = new Dictionary<Selectable, TweenerCore<Color, Color, ColorOptions>>();
  16. /// <summary>
  17. /// 开启图像控件的闪烁
  18. /// </summary>
  19. /// <param name="graphic">图像控件</param>
  20. /// <param name="color">闪烁的目标颜色</param>
  21. /// <param name="time">闪烁一次的时间</param>
  22. public static void OpenFlash(this Graphic graphic, Color color, float time = 0.5f)
  23. {
  24. if (graphic == null)
  25. return;
  26. if (Graphics.ContainsKey(graphic))
  27. return;
  28. TweenerCore<Color, Color, ColorOptions> tweener = DOTween.To(
  29. () =>
  30. {
  31. return graphic.color;
  32. },
  33. (c) =>
  34. {
  35. graphic.color = c;
  36. }, color, time).SetLoops(-1, LoopType.Yoyo).SetEase(Ease.Linear);
  37. tweener.startValue = graphic.color;
  38. Graphics.Add(graphic, tweener);
  39. }
  40. /// <summary>
  41. /// 关闭图像控件的闪烁
  42. /// </summary>
  43. /// <param name="graphic">图像控件</param>
  44. public static void CloseFlash(this Graphic graphic)
  45. {
  46. if (graphic == null)
  47. return;
  48. if (!Graphics.ContainsKey(graphic))
  49. return;
  50. Color normalColor = Graphics[graphic].startValue;
  51. Graphics[graphic].Kill();
  52. Graphics.Remove(graphic);
  53. graphic.color = normalColor;
  54. }
  55. /// <summary>
  56. /// 开启可选控件的闪烁(只在Normal状态)
  57. /// </summary>
  58. /// <param name="selectable">可选控件</param>
  59. /// <param name="color">闪烁的目标颜色</param>
  60. /// <param name="time">闪烁一次的时间</param>
  61. public static void OpenFlash(this Selectable selectable, Color color, float time = 0.5f)
  62. {
  63. if (selectable == null)
  64. return;
  65. if (selectable.targetGraphic == null)
  66. return;
  67. if (Selectables.ContainsKey(selectable))
  68. return;
  69. if (selectable.transition == Selectable.Transition.ColorTint)
  70. {
  71. TweenerCore<Color, Color, ColorOptions> tweener = DOTween.To(
  72. () =>
  73. {
  74. return selectable.colors.normalColor;
  75. },
  76. (c) =>
  77. {
  78. ColorBlock block = selectable.colors;
  79. block.normalColor = c;
  80. selectable.colors = block;
  81. }, color, time).SetLoops(-1, LoopType.Yoyo).SetEase(Ease.Linear);
  82. tweener.startValue = selectable.colors.normalColor;
  83. Selectables.Add(selectable, tweener);
  84. }
  85. else
  86. {
  87. TweenerCore<Color, Color, ColorOptions> tweener = DOTween.To(
  88. () =>
  89. {
  90. return selectable.targetGraphic.color;
  91. },
  92. (c) =>
  93. {
  94. selectable.targetGraphic.color = c;
  95. }, color, time).SetLoops(-1, LoopType.Yoyo).SetEase(Ease.Linear);
  96. tweener.startValue = selectable.targetGraphic.color;
  97. Selectables.Add(selectable, tweener);
  98. }
  99. }
  100. /// <summary>
  101. /// 关闭可选控件的闪烁
  102. /// </summary>
  103. /// <param name="selectable">可选控件</param>
  104. public static void CloseFlash(this Selectable selectable)
  105. {
  106. if (selectable == null)
  107. return;
  108. if (!Selectables.ContainsKey(selectable))
  109. return;
  110. if (selectable.transition == Selectable.Transition.ColorTint)
  111. {
  112. ColorBlock block = selectable.colors;
  113. block.normalColor = Selectables[selectable].startValue;
  114. Selectables[selectable].Kill();
  115. Selectables.Remove(selectable);
  116. selectable.colors = block;
  117. }
  118. else
  119. {
  120. Color normalColor = Selectables[selectable].startValue;
  121. Selectables[selectable].Kill();
  122. Selectables.Remove(selectable);
  123. if (selectable.targetGraphic != null)
  124. selectable.targetGraphic.color = normalColor;
  125. }
  126. }
  127. /// <summary>
  128. /// 关闭所有控件的闪烁
  129. /// </summary>
  130. public static void CloseAllFlash()
  131. {
  132. foreach (var graphic in Graphics)
  133. {
  134. if (graphic.Key)
  135. {
  136. Color normalColor = graphic.Value.startValue;
  137. graphic.Value.Kill();
  138. graphic.Key.color = normalColor;
  139. }
  140. }
  141. foreach (var selectable in Selectables)
  142. {
  143. if (selectable.Key)
  144. {
  145. if (selectable.Key.transition == Selectable.Transition.ColorTint)
  146. {
  147. ColorBlock block = selectable.Key.colors;
  148. block.normalColor = selectable.Value.startValue;
  149. selectable.Value.Kill();
  150. selectable.Key.colors = block;
  151. }
  152. else
  153. {
  154. Color normalColor = selectable.Value.startValue;
  155. selectable.Value.Kill();
  156. if (selectable.Key.targetGraphic != null)
  157. selectable.Key.targetGraphic.color = normalColor;
  158. }
  159. }
  160. }
  161. Graphics.Clear();
  162. Selectables.Clear();
  163. }
  164. }
  165. }