TweenRenderer.cs 10 KB

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