Extension.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 Regist
  76. public static void AddScript<T>(this Component comp)
  77. {
  78. AddScript<T>(comp.gameObject);
  79. }
  80. public static void AddScript<T>(this GameObject go)
  81. {
  82. Component comp = go.AddComponent(typeof(T));
  83. if (comp is Regist)
  84. {
  85. Initializer.RegistList.Add((Regist) comp);
  86. }
  87. }
  88. #endregion
  89. #region String
  90. public static string Replace(this string str, int startIndex, int endIndex, string newStr)
  91. {
  92. str = str.Remove(startIndex, endIndex - startIndex + 1);
  93. str = str.Insert(startIndex, newStr);
  94. return str;
  95. }
  96. public static string Between(this string str, int startIndex, int endIndex)
  97. {
  98. return str.Substring(startIndex, endIndex - startIndex + 1);
  99. }
  100. #endregion
  101. #region Active
  102. public static void SetActive(this Component comp, bool active)
  103. {
  104. comp.gameObject.SetActive(active);
  105. }
  106. #endregion
  107. #region Tween
  108. public static void TweenForCG(this Component comp)
  109. {
  110. ManaAnim.TweenForCG(comp.transform);
  111. }
  112. public static void TweenForVec(this Component comp)
  113. {
  114. ManaAnim.TweenForVec(comp.transform);
  115. }
  116. public static void TweenForGra(this Component comp)
  117. {
  118. ManaAnim.TweenForGra(comp.transform);
  119. }
  120. public static void TweenForRect(this Component comp)
  121. {
  122. ManaAnim.TweenForRect(comp.transform);
  123. }
  124. public static void TweenForScale(this Component comp)
  125. {
  126. ManaAnim.TweenForScale(comp.transform);
  127. }
  128. public static void TweenForAudio(this Component comp)
  129. {
  130. ManaAnim.TweenForAudio(comp.transform);
  131. }
  132. public static void TweenBacCG(this Component comp)
  133. {
  134. ManaAnim.TweenBacCG(comp.transform);
  135. }
  136. public static void TweenBacVec(this Component comp)
  137. {
  138. ManaAnim.TweenBacVec(comp.transform);
  139. }
  140. public static void TweenBacGra(this Component comp)
  141. {
  142. ManaAnim.TweenBacGra(comp.transform);
  143. }
  144. public static void TweenBacRect(this Component comp)
  145. {
  146. ManaAnim.TweenBacRect(comp.transform);
  147. }
  148. public static void TweenBacScale(this Component comp)
  149. {
  150. ManaAnim.TweenBacScale(comp.transform);
  151. }
  152. public static void TweenBacAudio(this Component comp)
  153. {
  154. ManaAnim.TweenBacAudio(comp.transform);
  155. }
  156. public static TweenCG GetTweenCG(this Component comp)
  157. {
  158. return ManaAnim.GetTweenCG(comp.transform);
  159. }
  160. public static TweenVec GetTweenVec(this Component comp)
  161. {
  162. return ManaAnim.GetTweenVec(comp.transform);
  163. }
  164. public static TweenGra GetTweenGra(this Component comp)
  165. {
  166. return ManaAnim.GetTweenGra(comp.transform);
  167. }
  168. public static TweenRect GetTweenRect(this Component comp)
  169. {
  170. return ManaAnim.GetTweenRect(comp.transform);
  171. }
  172. public static TweenScale GetTweenScale(this Component comp)
  173. {
  174. return ManaAnim.GetTweenScale(comp.transform);
  175. }
  176. public static TweenAudio GetTweenAudio(this Component comp)
  177. {
  178. return ManaAnim.GetTweenAudio(comp.transform);
  179. }
  180. public static TweenCG CreateTweenCG(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  181. {
  182. return ManaAnim.CreateTweenCG(comp.transform, origin, destination, duration, originActive, destActive, curve);
  183. }
  184. public static TweenCG CreateTweenCG(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  185. {
  186. return ManaAnim.CreateTweenCG(comp.transform, destination, duration, originActive, destActive, curve);
  187. }
  188. public static TweenGra CreateTweenGra(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  189. {
  190. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve);
  191. }
  192. public static TweenGra CreateTweenGra(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  193. {
  194. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve);
  195. }
  196. public static TweenVec CreateTweenVec(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  197. {
  198. return ManaAnim.CreateTweenVec(comp.transform, origin, destination, duration, originActive, destActive, curve);
  199. }
  200. public static TweenVec CreateTweenVec(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  201. {
  202. return ManaAnim.CreateTweenVec(comp.transform, destination, duration, originActive, destActive, curve);
  203. }
  204. public static TweenRect CreateTweenRect(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  205. {
  206. return ManaAnim.CreateTweenRect(comp.transform, origin, destination, duration, originActive, destActive, curve);
  207. }
  208. public static TweenRect CreateTweenRect(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  209. {
  210. return ManaAnim.CreateTweenRect(comp.transform, destination, duration, originActive, destActive, curve);
  211. }
  212. public static TweenScale CreateTweenScale(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  213. {
  214. return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve);
  215. }
  216. public static TweenScale CreateTweenScale(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  217. {
  218. return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve);
  219. }
  220. public static TweenAudio CreateTweenAudio(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  221. {
  222. return ManaAnim.CreateTweenAudio(comp.transform, origin, destination, duration, originActive, destActive, curve);
  223. }
  224. public static TweenAudio CreateTweenAudio(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  225. {
  226. return ManaAnim.CreateTweenAudio(comp.transform, destination, duration, originActive, destActive, curve);
  227. }
  228. #endregion
  229. #region Vector3
  230. public static void SetX(this Transform tra, float x)
  231. {
  232. tra.position = new Vector3(x, tra.position.y, tra.position.z);
  233. }
  234. public static void SetY(this Transform tra, float y)
  235. {
  236. tra.position = new Vector3(tra.position.x, y, tra.position.z);
  237. }
  238. public static void SetZ(this Transform tra, float z)
  239. {
  240. tra.position = new Vector3(tra.position.x, tra.position.y, z);
  241. }
  242. #endregion
  243. #region Collider
  244. public static void SetCollider(this GameObject gameObject, bool active)
  245. {
  246. gameObject.GetComponent<BoxCollider2D>().enabled = active;
  247. }
  248. public static void SetCollider(this Component comp, bool active)
  249. {
  250. BoxCollider2D collider = comp.GetComponent<BoxCollider2D>();
  251. if (collider)
  252. {
  253. collider.enabled = active;
  254. }
  255. }
  256. #endregion
  257. #region GetChild
  258. public static Transform GetChild(this GameObject gameObject, int index)
  259. {
  260. return gameObject.transform.GetChild(index);
  261. }
  262. public static Transform GetChild(this Component comp, int index)
  263. {
  264. return comp.transform.GetChild(index);
  265. }
  266. #endregion
  267. #region SetParent
  268. public static void SetParent(this Component comp, Transform par)
  269. {
  270. comp.transform.SetParent(par);
  271. }
  272. public static void SetParent(this GameObject go, Transform par)
  273. {
  274. go.transform.SetParent(par);
  275. }
  276. #endregion
  277. #region AddComponent
  278. public static T AddComponent<T>(this Component comp) where T : Component
  279. {
  280. return comp.transform.gameObject.AddComponent<T>();
  281. }
  282. #endregion
  283. }