Extension.cs 11 KB

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