Extension.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. public static class Extension
  7. {
  8. #region List
  9. public static T Last<T>(this List<T> list, int index)
  10. {
  11. return list[list.Count - 1 - index];
  12. }
  13. public static bool UniqueAdd<T>(this List<T> list, T obj)
  14. {
  15. if (list.Contains(obj) == false)
  16. {
  17. list.Add(obj);
  18. return true;
  19. }
  20. else
  21. {
  22. return false;
  23. }
  24. }
  25. #endregion
  26. #region Scale
  27. public static Vector3 GetScale(this Transform tra)
  28. {
  29. Vector3 scale = tra.localScale;
  30. while (tra.parent != null)
  31. {
  32. float x = scale.x*tra.parent.localScale.x;
  33. float y = scale.y*tra.parent.localScale.y;
  34. float z = scale.z*tra.parent.localScale.z;
  35. scale = new Vector3(x, y, z);
  36. tra = tra.parent;
  37. }
  38. return scale;
  39. }
  40. #endregion
  41. #region Move
  42. public static void MoveVec(this Component comp, Vector3 destination, float duration, Curve curve)
  43. {
  44. ManaAnim.MoveVec(comp.transform, destination, duration, curve);
  45. }
  46. public static MoveVec GetMoveVec(this Component comp)
  47. {
  48. return ManaAnim.GetMoveVec(comp.transform);
  49. }
  50. public static MoveVec CreateMoveVec(this Component comp)
  51. {
  52. return ManaAnim.CreateMoveVec(comp.transform);
  53. }
  54. #endregion
  55. #region Alpha
  56. public static void SetAlpha(this Graphic graphic, float alpha)
  57. {
  58. graphic.color = new Color(graphic.color.r, graphic.color.g, graphic.color.b, alpha);
  59. }
  60. public static void SetAlpha(this TextMesh textMesh, float alpha)
  61. {
  62. textMesh.color = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, alpha);
  63. }
  64. public static void SetAlpha(this SpriteRenderer sR, float alpha)
  65. {
  66. sR.color = new Color(sR.color.r, sR.color.g, sR.color.b, alpha);
  67. }
  68. #endregion
  69. #region Sprite
  70. public static Sprite GetSprite(this Component comp)
  71. {
  72. return comp.GetComponent<Image>().sprite;
  73. }
  74. #endregion
  75. #region String
  76. public static string Replace(this string str, int startIndex, int endIndex, string newStr)
  77. {
  78. str = str.Remove(startIndex, endIndex - startIndex + 1);
  79. str = str.Insert(startIndex, newStr);
  80. return str;
  81. }
  82. public static string Between(this string str, int startIndex, int endIndex)
  83. {
  84. return str.Substring(startIndex, endIndex - startIndex + 1);
  85. }
  86. #endregion
  87. #region Active
  88. public static void SetActive(this Component comp, bool active)
  89. {
  90. comp.gameObject.SetActive(active);
  91. }
  92. #endregion
  93. #region Tween
  94. public static void TweenForCG(this Component comp)
  95. {
  96. ManaAnim.TweenForCG(comp.transform);
  97. }
  98. public static void TweenForVec(this Component comp)
  99. {
  100. ManaAnim.TweenForVec(comp.transform);
  101. }
  102. public static void TweenForGra(this Component comp)
  103. {
  104. ManaAnim.TweenForGra(comp.transform);
  105. }
  106. public static void TweenForRect(this Component comp)
  107. {
  108. ManaAnim.TweenForRect(comp.transform);
  109. }
  110. public static void TweenForScale(this Component comp)
  111. {
  112. ManaAnim.TweenForScale(comp.transform);
  113. }
  114. public static void TweenForAudio(this Component comp)
  115. {
  116. ManaAnim.TweenForAudio(comp.transform);
  117. }
  118. public static void TweenBacCG(this Component comp)
  119. {
  120. ManaAnim.TweenBacCG(comp.transform);
  121. }
  122. public static void TweenBacVec(this Component comp)
  123. {
  124. ManaAnim.TweenBacVec(comp.transform);
  125. }
  126. public static void TweenBacGra(this Component comp)
  127. {
  128. ManaAnim.TweenBacGra(comp.transform);
  129. }
  130. public static void TweenBacRect(this Component comp)
  131. {
  132. ManaAnim.TweenBacRect(comp.transform);
  133. }
  134. public static void TweenBacScale(this Component comp)
  135. {
  136. ManaAnim.TweenBacScale(comp.transform);
  137. }
  138. public static void TweenBacAudio(this Component comp)
  139. {
  140. ManaAnim.TweenBacAudio(comp.transform);
  141. }
  142. public static TweenCG GetTweenCG(this Component comp)
  143. {
  144. return ManaAnim.GetTweenCG(comp.transform);
  145. }
  146. public static TweenVec GetTweenVec(this Component comp)
  147. {
  148. return ManaAnim.GetTweenVec(comp.transform);
  149. }
  150. public static TweenGra GetTweenGra(this Component comp)
  151. {
  152. return ManaAnim.GetTweenGra(comp.transform);
  153. }
  154. public static TweenRect GetTweenRect(this Component comp)
  155. {
  156. return ManaAnim.GetTweenRect(comp.transform);
  157. }
  158. public static TweenScale GetTweenScale(this Component comp)
  159. {
  160. return ManaAnim.GetTweenScale(comp.transform);
  161. }
  162. public static TweenAudio GetTweenAudio(this Component comp)
  163. {
  164. return ManaAnim.GetTweenAudio(comp.transform);
  165. }
  166. public static TweenCG CreateTweenCG(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  167. {
  168. return ManaAnim.CreateTweenCG(comp.transform, origin, destination, duration, originActive, destActive, curve);
  169. }
  170. public static TweenCG CreateTweenCG(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  171. {
  172. return ManaAnim.CreateTweenCG(comp.transform, destination, duration, originActive, destActive, curve);
  173. }
  174. public static TweenGra CreateTweenGra(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  175. {
  176. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve);
  177. }
  178. public static TweenGra CreateTweenGra(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  179. {
  180. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve);
  181. }
  182. public static TweenVec CreateTweenVec(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  183. {
  184. return ManaAnim.CreateTweenVec(comp.transform, origin, destination, duration, originActive, destActive, curve);
  185. }
  186. public static TweenVec CreateTweenVec(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  187. {
  188. return ManaAnim.CreateTweenVec(comp.transform, destination, duration, originActive, destActive, curve);
  189. }
  190. public static TweenRect CreateTweenRect(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  191. {
  192. return ManaAnim.CreateTweenRect(comp.transform, origin, destination, duration, originActive, destActive, curve);
  193. }
  194. public static TweenRect CreateTweenRect(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  195. {
  196. return ManaAnim.CreateTweenRect(comp.transform, destination, duration, originActive, destActive, curve);
  197. }
  198. public static TweenScale CreateTweenScale(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  199. {
  200. return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve);
  201. }
  202. public static TweenScale CreateTweenScale(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  203. {
  204. return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve);
  205. }
  206. public static TweenAudio CreateTweenAudio(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  207. {
  208. return ManaAnim.CreateTweenAudio(comp.transform, origin, destination, duration, originActive, destActive, curve);
  209. }
  210. public static TweenAudio CreateTweenAudio(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  211. {
  212. return ManaAnim.CreateTweenAudio(comp.transform, destination, duration, originActive, destActive, curve);
  213. }
  214. #endregion
  215. #region Vector3
  216. public static void SetX(this Transform tra, float x)
  217. {
  218. tra.position = new Vector3(x, tra.position.y, tra.position.z);
  219. }
  220. public static void SetY(this Transform tra, float y)
  221. {
  222. tra.position = new Vector3(tra.position.x, y, tra.position.z);
  223. }
  224. public static void SetZ(this Transform tra, float z)
  225. {
  226. tra.position = new Vector3(tra.position.x, tra.position.y, z);
  227. }
  228. #endregion
  229. #region Collider
  230. public static void SetCollider(this GameObject gameObject, bool active)
  231. {
  232. gameObject.GetComponent<BoxCollider2D>().enabled = active;
  233. }
  234. public static void SetCollider(this Component comp, bool active)
  235. {
  236. BoxCollider2D collider = comp.GetComponent<BoxCollider2D>();
  237. if (collider)
  238. {
  239. collider.enabled = active;
  240. }
  241. }
  242. #endregion
  243. #region GetChild
  244. public static Transform GetChild(this GameObject gameObject, int index)
  245. {
  246. return gameObject.transform.GetChild(index);
  247. }
  248. public static Transform GetChild(this Component comp, int index)
  249. {
  250. return comp.transform.GetChild(index);
  251. }
  252. #endregion
  253. #region SetParent
  254. public static void SetParent(this Component comp, Transform par)
  255. {
  256. comp.transform.SetParent(par);
  257. }
  258. public static void SetParent(this GameObject go, Transform par)
  259. {
  260. go.transform.SetParent(par);
  261. }
  262. #endregion
  263. #region AddComponent
  264. public static T AddComponent<T>(this Component comp) where T : Component
  265. {
  266. return comp.transform.gameObject.AddComponent<T>();
  267. }
  268. #endregion
  269. }