TweenRenderer.cs 9.1 KB

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