TweenSr.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using UnityEngine;
  2. using System.Linq;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class TweenSr : TweenRoot
  6. {
  7. #region 变量
  8. public override bool InOrigin
  9. {
  10. get
  11. {
  12. if (Target.color.Equal(Origin))
  13. {
  14. InOrigin_ = true;
  15. }
  16. else
  17. {
  18. InOrigin_ = false;
  19. }
  20. return InOrigin_;
  21. }
  22. set
  23. {
  24. InOrigin_ = value;
  25. InPause = false;
  26. InForward = false;
  27. InBackward = false;
  28. if (InOrigin_)
  29. {
  30. Target.color = Origin;
  31. UpdateChildSpriteRenderer();
  32. if (Group)
  33. {
  34. for (int i = 0; i < ChildList.Count; i++)
  35. {
  36. if (!ChildList[i].transform.IsChildOf(Target.transform))
  37. {
  38. ChildList.RemoveAt(i--);
  39. continue;
  40. }
  41. Color selfColor = ColorDic[ChildList[i]];
  42. if (AlphaOnly)
  43. {
  44. ChildList[i].color = new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * Target.color.a);
  45. }
  46. else
  47. {
  48. ChildList[i].color = selfColor * Target.color;
  49. }
  50. }
  51. }
  52. BackwardFinish();
  53. }
  54. }
  55. }
  56. public override bool InDestination
  57. {
  58. get
  59. {
  60. if (Target.color.Equal(Destination))
  61. {
  62. InDestination_ = true;
  63. }
  64. else
  65. {
  66. InDestination_ = false;
  67. }
  68. return InDestination_;
  69. }
  70. set
  71. {
  72. InDestination_ = value;
  73. InPause = false;
  74. InForward = false;
  75. InBackward = false;
  76. if (InDestination_)
  77. {
  78. Target.color = Destination;
  79. UpdateChildSpriteRenderer();
  80. if (Group)
  81. {
  82. for (int i = 0; i < ChildList.Count; i++)
  83. {
  84. if (!ChildList[i].transform.IsChildOf(Target.transform))
  85. {
  86. ChildList.RemoveAt(i--);
  87. continue;
  88. }
  89. Color selfColor = ColorDic[ChildList[i]];
  90. if (AlphaOnly)
  91. {
  92. ChildList[i].color = new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * Target.color.a);
  93. }
  94. else
  95. {
  96. ChildList[i].color = selfColor*Target.color;
  97. }
  98. }
  99. }
  100. ForwardFinish();
  101. }
  102. }
  103. }
  104. public bool Group;
  105. public bool AlphaOnly;
  106. public Color Delta;
  107. public Color Origin;
  108. public Color Destination;
  109. public SpriteRenderer Target;
  110. public CurveFunctionC Func;
  111. public List<SpriteRenderer> ChildList = new List<SpriteRenderer>();
  112. public Dictionary<SpriteRenderer, Color> ColorDic = new Dictionary<SpriteRenderer, Color>();
  113. #endregion
  114. public TweenSr(SpriteRenderer target, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve,bool cg = false,bool group=false) : base(cg, curve, target)
  115. {
  116. Func = ManaAnim.CurveFuncDicC[curve];
  117. Target = target;
  118. InForward = false;
  119. InBackward = false;
  120. Delta = destination - origin;
  121. Origin = origin;
  122. Group = group;
  123. Duration = duration;
  124. DestActive = destActive;
  125. Destination = destination;
  126. OriginActive = originActive;
  127. }
  128. public override bool StartForward()
  129. {
  130. if (base.StartForward())
  131. {
  132. return true;
  133. }
  134. UpdateChildSpriteRenderer();
  135. if (InBackward)
  136. {
  137. InBackward = false;
  138. Timer = ManaAnim.GetTimerColor(Target.color, Duration, Origin, Delta, Curve);
  139. }
  140. return false;
  141. }
  142. public override bool StartBackward()
  143. {
  144. if (base.StartBackward())
  145. {
  146. return true;
  147. }
  148. UpdateChildSpriteRenderer();
  149. if (InForward)
  150. {
  151. InForward = false;
  152. Timer = ManaAnim.GetTimerColor(Target.color, Duration, Destination, new Color(-Delta.r, -Delta.g, -Delta.b, -Delta.a), Curve);
  153. }
  154. return false;
  155. }
  156. public override bool DoForward()
  157. {
  158. Timer += Time.deltaTime;
  159. if (Timer > Duration)
  160. {
  161. InDestination = true;
  162. if (OnForwardFinish != null)
  163. {
  164. OnForwardFinish.Invoke();
  165. }
  166. SetLoop(EventType.ForwardFinish);
  167. return true;
  168. }
  169. else
  170. {
  171. Target.color = Func(Timer, Duration, Origin, Delta);
  172. if (Group)
  173. {
  174. for (int i = 0; i < ChildList.Count; i++)
  175. {
  176. if (!ChildList[i].transform.IsChildOf(Target.transform))
  177. {
  178. ChildList.RemoveAt(i--);
  179. continue;
  180. }
  181. Color selfColor = ColorDic[ChildList[i]];
  182. if (AlphaOnly)
  183. {
  184. ChildList[i].color = new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a * Target.color.a);
  185. }
  186. else
  187. {
  188. ChildList[i].color = selfColor * Target.color;
  189. }
  190. }
  191. }
  192. return false;
  193. }
  194. }
  195. public override bool DoBackward()
  196. {
  197. Timer += Time.deltaTime;
  198. if (Timer > Duration)
  199. {
  200. InOrigin = true;
  201. if (OnBackwardFinish != null)
  202. {
  203. OnBackwardFinish.Invoke();
  204. }
  205. SetLoop(EventType.BackwardFinish);
  206. return true;
  207. }
  208. else
  209. {
  210. Target.color = Func(Timer, Duration, Destination, new Color(-Delta.r, -Delta.g, -Delta.b, -Delta.a));
  211. if (Group)
  212. {
  213. for (int i = 0; i < ChildList.Count; i++)
  214. {
  215. if (!ChildList[i].transform.IsChildOf(Target.transform))
  216. {
  217. ChildList.RemoveAt(i--);
  218. continue;
  219. }
  220. Color selfColor = ColorDic[ChildList[i]];
  221. if (AlphaOnly)
  222. {
  223. ChildList[i].color = new Color(selfColor.r, selfColor.g, selfColor.b, selfColor.a*Target.color.a);
  224. }
  225. else
  226. {
  227. ChildList[i].color = selfColor * Target.color;
  228. }
  229. }
  230. }
  231. return false;
  232. }
  233. }
  234. public void UpdateChildSpriteRenderer()
  235. {
  236. if (Group)
  237. {
  238. SpriteRenderer[] renderers = Target.GetComponentsInChildren<SpriteRenderer>();
  239. ChildList = new List<SpriteRenderer>();
  240. for (int i = 1; i < renderers.Length; i++)
  241. {
  242. SpriteRenderer renderer = renderers[i];
  243. ChildList.Add(renderer);
  244. if (!ColorDic.ContainsKey(renderer))
  245. {
  246. ColorDic.Add(renderer, renderer.color);
  247. }
  248. }
  249. List<SpriteRenderer> keyList = ColorDic.Keys.ToList();
  250. for (int i = 0; i < keyList.Count; i++)
  251. {
  252. if (!ChildList.Contains(keyList[i]))
  253. {
  254. ColorDic.Remove(keyList[i]);
  255. }
  256. }
  257. }
  258. }
  259. }