GameEffect.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using DG.Tweening;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using XRTool.Util;
  8. public class GameEffect : MonoBehaviour
  9. {
  10. public void Update()
  11. {
  12. }
  13. public static void callBackEnd(Action<GameObject> callBack, GameObject obj, List<MeshRenderer> mrList, List<Image> imList)
  14. {
  15. if (callBack != null)
  16. {
  17. callBack.Invoke(obj);
  18. }
  19. for (int i = 0; i < imList.Count; i++)
  20. {
  21. string mName = imList[i].materialForRendering.name.Split('(')[0];
  22. string lenCt = mName.Substring(mName.Length - 1);
  23. if (lenCt == " ")
  24. {
  25. mName = mName.Substring(0, mName.Length - 1);
  26. }
  27. if (mats.ContainsKey(mName))
  28. {
  29. imList[i].material = mats[mName];
  30. }
  31. else
  32. {
  33. }
  34. }
  35. for (int i = 0; i < mrList.Count; i++)
  36. {
  37. string mName = mrList[i].sharedMaterial.name.Split('(')[0];
  38. string lenCt = mName.Substring(mName.Length - 1);
  39. if (lenCt == " ")
  40. {
  41. mName = mName.Substring(0, mName.Length - 1);
  42. }
  43. if (mats.ContainsKey(mName))
  44. {
  45. mrList[i].material = mats[mName];
  46. }
  47. else
  48. {
  49. }
  50. }
  51. }
  52. static Dictionary<string, Material> mats = new Dictionary<string, Material>();
  53. static Renderer[] mrs;
  54. static Transform[] gos;
  55. public static void setAlpha(GameObject obj, float f, float time, Action<GameObject> callBack = null)
  56. {
  57. if (f == 0)
  58. {
  59. f = 0.00001f;
  60. }
  61. int callBackCt = 0;
  62. int callBackEndCt = 0;
  63. gos = obj.GetComponentsInChildren<Transform>();
  64. List<MeshRenderer> mrList = new List<MeshRenderer>();
  65. List<Image> imList = new List<Image>();
  66. mrs = obj.GetComponentsInChildren<Renderer>();
  67. // return;
  68. for (int i = 0; i < gos.Length; i++)
  69. {
  70. if (!gos[i].name.Contains("UIRoot") && !gos[i].name.Contains("XRScrollRect") && !gos[i].name.Contains("Viewport")
  71. && !gos[i].name.Contains("BoxIcon3D") && !gos[i].name.Contains("MenuGroup") && !gos[i].name.Contains("Shortcutkey"))
  72. {
  73. MeshRenderer mr = gos[i].GetComponent<MeshRenderer>();
  74. if (mr != null && mr.sharedMaterial)
  75. {
  76. if (!mr.material.HasProperty("_Color"))
  77. {
  78. continue;
  79. }
  80. string mName = mr.sharedMaterial.name.Split('(')[0];
  81. string lenCt = mName.Substring(mName.Length - 1);
  82. if (!mats.ContainsKey(mName) && lenCt != " ")
  83. {
  84. mats.Add(mName, mr.sharedMaterial);
  85. }
  86. mrList.Add(mr);
  87. if ((mr.material.color.a <= 0))
  88. {
  89. continue;
  90. }
  91. if (f > 0.1f)
  92. {
  93. mr.material.color = new Color(mr.material.color.r, mr.material.color.g, mr.material.color.b, 0.001f);
  94. }
  95. callBackCt++;
  96. mr.DOKill();
  97. mr.material.DOFade(f, time).OnKill<Tween>(() =>
  98. {
  99. callBackEndCt++;
  100. if (callBackEndCt >= callBackCt)
  101. {
  102. callBackCt = 9999;
  103. callBackEndCt = 0;
  104. callBackEnd(callBack, obj, mrList, imList);
  105. }
  106. }).OnComplete<Tween>(() =>
  107. {
  108. callBackEndCt++;
  109. if (callBackEndCt >= callBackCt)
  110. {
  111. callBackCt = 9999;
  112. callBackEndCt = 0;
  113. callBackEnd(callBack, obj, mrList, imList);
  114. }
  115. });
  116. }
  117. Image im = gos[i].GetComponent<Image>();
  118. if (im != null)
  119. {
  120. if ((im.color.a <= 0))
  121. {
  122. continue;
  123. }
  124. if (f > 0.1f)
  125. im.color = new Color(im.color.r, im.color.g, im.color.b, 0.001f);
  126. callBackCt++;
  127. im.DOKill();
  128. im.DOFade(f, time).SetEase<Tween>(Ease.InCirc).OnComplete<Tween>(() =>
  129. {
  130. callBackEndCt++;
  131. if (callBackEndCt >= callBackCt)
  132. {
  133. callBackCt = 9999;
  134. callBackEndCt = 0;
  135. callBackEnd(callBack, obj, mrList, imList);
  136. }
  137. }).OnKill<Tween>(() =>
  138. {
  139. callBackEndCt++;
  140. if (callBackEndCt >= callBackCt)
  141. {
  142. callBackCt = 9999;
  143. callBackEndCt = 0;
  144. callBackEnd(callBack, obj, mrList, imList);
  145. }
  146. });
  147. if (!im.material.ToString().Contains("Default UI Material"))
  148. {
  149. string mName = im.materialForRendering.name.Split('(')[0];
  150. string lenCt = mName.Substring(mName.Length - 1);
  151. if (!mats.ContainsKey(mName) && lenCt != " ")
  152. {
  153. mats.Add(mName, im.materialForRendering);
  154. }
  155. imList.Add(im);
  156. if (f > 0.1f)
  157. {
  158. // im.material.color = new Color(im.material.color.r, im.material.color.g, im.material.color.b, 0.001f);
  159. }
  160. callBackCt++;
  161. im.material.DOFade(f, time).SetEase<Tween>(Ease.InCirc).OnComplete<Tween>(() =>
  162. {
  163. callBackEndCt++;
  164. if (callBackEndCt >= callBackCt)
  165. {
  166. callBackCt = 9999;
  167. callBackEndCt = 0;
  168. callBackEnd(callBack, obj, mrList, imList);
  169. }
  170. }).OnKill<Tween>(() =>
  171. {
  172. callBackEndCt++;
  173. if (callBackEndCt >= callBackCt)
  174. {
  175. callBackCt = 9999;
  176. callBackEndCt = 0;
  177. callBackEnd(callBack, obj, mrList, imList);
  178. }
  179. if (f < 1)
  180. {
  181. Color c = im.material.color;
  182. im.material.color = new Color(c.r, c.g, c.b, 1);
  183. }
  184. });
  185. }
  186. }
  187. SpriteRenderer sr = gos[i].GetComponent<SpriteRenderer>();
  188. if (sr != null)
  189. {
  190. if ((sr.color.a <= 0))
  191. {
  192. continue;
  193. }
  194. if (f > 0.1f)
  195. sr.color = new Color(sr.color.r, sr.color.g, sr.color.b, 0.001f);
  196. callBackCt++;
  197. sr.DOKill();
  198. sr.DOFade(f, time).SetEase<Tween>(Ease.InCirc).OnComplete<Tween>(() =>
  199. {
  200. callBackEndCt++;
  201. if (callBackEndCt >= callBackCt)
  202. {
  203. callBackCt = 9999;
  204. callBackEndCt = 0;
  205. callBackEnd(callBack, obj, mrList, imList);
  206. }
  207. }).OnKill<Tween>(() =>
  208. {
  209. callBackEndCt++;
  210. if (callBackEndCt >= callBackCt)
  211. {
  212. callBackCt = 9999;
  213. callBackEndCt = 0;
  214. callBackEnd(callBack, obj, mrList, imList);
  215. }
  216. });
  217. }
  218. }
  219. }
  220. obj.SetActive(true);
  221. }
  222. }