uTweenColor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace uTools
  6. {
  7. [AddComponentMenu("uTools/Tween/Tween Color(uTools)")]
  8. public class uTweenColor : uTweener
  9. {
  10. Transform mTransform;
  11. public TweenTarget colorType = TweenTarget.UI;
  12. public Color from = Color.white;
  13. public Color to = Color.white;
  14. public bool includeChilds = false;
  15. List<SpriteRenderer> spList = new List<SpriteRenderer>();
  16. List<Graphic> gaList = new List<Graphic>();
  17. List<TextMesh> textMeshList = new List<TextMesh>();
  18. Color mColor = Color.white;
  19. public Transform cachedTransform { get { if (mTransform == null) mTransform = transform; return mTransform; } }
  20. protected override void Awake()
  21. {
  22. GetTweenList();
  23. base.Awake();
  24. }
  25. void GetTweenList()
  26. {
  27. spList.Clear();
  28. gaList.Clear();
  29. textMeshList.Clear();
  30. Queue<Transform> childList = new Queue<Transform>();
  31. childList.Enqueue(cachedTransform);
  32. while (childList.Count > 0)
  33. {
  34. Transform t = childList.Dequeue();
  35. if (colorType == TweenTarget.SpriteRender)
  36. {
  37. SpriteRenderer m_sp = t.GetComponent<SpriteRenderer>();
  38. if (null != m_sp) spList.Add(m_sp);
  39. }
  40. else if (colorType == TweenTarget.UI)
  41. {
  42. Graphic m_graphic = t.GetComponent<Graphic>();
  43. if (null != m_graphic) gaList.Add(m_graphic);
  44. }
  45. else if (colorType == TweenTarget.TextMesh)
  46. {
  47. TextMesh m_textMesh = t.GetComponent<TextMesh>();
  48. if (null != m_textMesh) textMeshList.Add(m_textMesh);
  49. }
  50. if (includeChilds)
  51. {
  52. for (int i = 0; i < t.childCount; i++)
  53. {
  54. childList.Enqueue(t.GetChild(i));
  55. }
  56. }
  57. }
  58. }
  59. public Color colorValue
  60. {
  61. get
  62. {
  63. return mColor;
  64. }
  65. set
  66. {
  67. SetColor(transform, value);
  68. mColor = value;
  69. }
  70. }
  71. protected override void OnUpdate(float factor, bool isFinished)
  72. {
  73. colorValue = Color.Lerp(from, to, factor);
  74. }
  75. public static uTweenColor Begin(GameObject go, float duration, float delay, Color from, Color to)
  76. {
  77. uTweenColor comp = uTweener.Begin<uTweenColor>(go, duration);
  78. comp.from = from;
  79. comp.to = to;
  80. comp.delay = delay;
  81. if (duration <= 0)
  82. {
  83. comp.Sample(1, true);
  84. comp.enabled = false;
  85. }
  86. return comp;
  87. }
  88. void SetColor(Transform _transform, Color _color)
  89. {
  90. if (colorType == TweenTarget.SpriteRender)
  91. {
  92. if (spList.Count == 0)
  93. {
  94. GetTweenList();
  95. }
  96. for (int i = 0; i < spList.Count; i++)
  97. {
  98. SpriteRenderer m_sp = spList[i];
  99. m_sp.color = _color;
  100. }
  101. }
  102. else if (colorType == TweenTarget.UI)
  103. {
  104. if (gaList.Count == 0)
  105. {
  106. GetTweenList();
  107. }
  108. for (int i = 0; i < gaList.Count; i++)
  109. {
  110. Graphic m_ga = gaList[i];
  111. m_ga.color = _color;
  112. }
  113. }
  114. else if (colorType == TweenTarget.TextMesh)
  115. {
  116. if (textMeshList.Count == 0)
  117. {
  118. GetTweenList();
  119. }
  120. for (int i = 0; i < textMeshList.Count; i++)
  121. {
  122. TextMesh m_textMesh = textMeshList[i];
  123. m_textMesh.color = _color;
  124. }
  125. }
  126. }
  127. }
  128. }