Extension.cs 11 KB

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