Extension.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using Random = UnityEngine.Random;
  7. public static class Extension
  8. {
  9. #region List
  10. public static T Last<T>(this List<T> list, int index)
  11. {
  12. return list[list.Count - 1 - index];
  13. }
  14. public static T Random<T>(this List<T> list)
  15. {
  16. return list[UnityEngine.Random.Range(0, list.Count)];
  17. }
  18. public static bool Valid<T>(this List<T> list)
  19. {
  20. if (list == null || list.Count == 0)
  21. {
  22. return false;
  23. }
  24. else
  25. {
  26. return true;
  27. }
  28. }
  29. public static bool UniqueAdd<T>(this List<T> list, T obj)
  30. {
  31. if (list.Contains(obj) == false)
  32. {
  33. list.Add(obj);
  34. return true;
  35. }
  36. else
  37. {
  38. return false;
  39. }
  40. }
  41. #endregion
  42. #region Scale
  43. public static Vector3 GetScale(this Transform tra)
  44. {
  45. Vector3 scale = tra.localScale;
  46. while (tra.parent != null)
  47. {
  48. float x = scale.x*tra.parent.localScale.x;
  49. float y = scale.y*tra.parent.localScale.y;
  50. float z = scale.z*tra.parent.localScale.z;
  51. scale = new Vector3(x, y, z);
  52. tra = tra.parent;
  53. }
  54. return scale;
  55. }
  56. #endregion
  57. #region Move
  58. public static void MoveVec(this Component comp, Vector3 destination, float duration, Curve curve)
  59. {
  60. ManaAnim.MoveVec(comp.transform, destination, duration, curve);
  61. }
  62. public static MoveVec GetMoveVec(this Component comp)
  63. {
  64. return ManaAnim.GetMoveVec(comp.transform);
  65. }
  66. public static MoveVec CreateMoveVec(this Component comp)
  67. {
  68. return ManaAnim.CreateMoveVec(comp.transform);
  69. }
  70. #endregion
  71. #region Alpha
  72. public static void SetAlpha(this Graphic graphic, float alpha)
  73. {
  74. graphic.color = new Color(graphic.color.r, graphic.color.g, graphic.color.b, alpha);
  75. }
  76. public static void SetAlpha(this TextMesh textMesh, float alpha)
  77. {
  78. textMesh.color = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, alpha);
  79. }
  80. public static void SetAlpha(this SpriteRenderer sR, float alpha)
  81. {
  82. sR.color = new Color(sR.color.r, sR.color.g, sR.color.b, alpha);
  83. }
  84. #endregion
  85. #region Sprite
  86. public static Sprite GetSprite(this Component comp)
  87. {
  88. return comp.GetComponent<Image>().sprite;
  89. }
  90. #endregion
  91. #region Regist
  92. public static void AddScript<T>(this Component comp)
  93. {
  94. AddScript<T>(comp.gameObject);
  95. }
  96. public static void AddScript<T>(this GameObject go)
  97. {
  98. Component comp = go.AddComponent(typeof(T));
  99. if (comp is Regist)
  100. {
  101. ((Regist) comp).enabled = false;
  102. Initializer.RegistList.Add((Regist) comp);
  103. }
  104. }
  105. #endregion
  106. #region String
  107. public static string Replace(this string str, int startIndex, int endIndex, string newStr)
  108. {
  109. str = str.Remove(startIndex, endIndex - startIndex + 1);
  110. str = str.Insert(startIndex, newStr);
  111. return str;
  112. }
  113. public static string Between(this string str, int startIndex, int endIndex)
  114. {
  115. if (startIndex > endIndex)
  116. {
  117. return "";
  118. }
  119. else if (startIndex == endIndex)
  120. {
  121. return str[startIndex].ToString();
  122. }
  123. else
  124. {
  125. return str.Substring(startIndex, endIndex - startIndex + 1);
  126. }
  127. }
  128. #endregion
  129. #region Active
  130. public static void SetActive(this Component comp, bool active)
  131. {
  132. comp.gameObject.SetActive(active);
  133. }
  134. #endregion
  135. #region Tween
  136. public static void TweenForSr(this Component comp)
  137. {
  138. ManaAnim.TweenForSr(comp.transform);
  139. }
  140. public static void TweenForCG(this Component comp)
  141. {
  142. ManaAnim.TweenForCG(comp.transform);
  143. }
  144. public static void TweenForVec(this Component comp)
  145. {
  146. ManaAnim.TweenForVec(comp.transform);
  147. }
  148. public static void TweenForGra(this Component comp)
  149. {
  150. ManaAnim.TweenForGra(comp.transform);
  151. }
  152. public static void TweenForRect(this Component comp)
  153. {
  154. ManaAnim.TweenForRect(comp.transform);
  155. }
  156. public static void TweenForScale(this Component comp)
  157. {
  158. ManaAnim.TweenForScale(comp.transform);
  159. }
  160. public static void TweenForAudio(this Component comp)
  161. {
  162. ManaAnim.TweenForAudio(comp.transform);
  163. }
  164. public static void TweenBacSr(this Component comp)
  165. {
  166. ManaAnim.TweenBacSr(comp.transform);
  167. }
  168. public static void TweenBacCG(this Component comp)
  169. {
  170. ManaAnim.TweenBacCG(comp.transform);
  171. }
  172. public static void TweenBacVec(this Component comp)
  173. {
  174. ManaAnim.TweenBacVec(comp.transform);
  175. }
  176. public static void TweenBacGra(this Component comp)
  177. {
  178. ManaAnim.TweenBacGra(comp.transform);
  179. }
  180. public static void TweenBacRect(this Component comp)
  181. {
  182. ManaAnim.TweenBacRect(comp.transform);
  183. }
  184. public static void TweenBacScale(this Component comp)
  185. {
  186. ManaAnim.TweenBacScale(comp.transform);
  187. }
  188. public static void TweenBacAudio(this Component comp)
  189. {
  190. ManaAnim.TweenBacAudio(comp.transform);
  191. }
  192. public static void TweenConForSr(this Component comp)
  193. {
  194. ManaAnim.TweenConForSr(comp.transform);
  195. }
  196. public static void TweenConForCG(this Component comp)
  197. {
  198. ManaAnim.TweenConForCG(comp.transform);
  199. }
  200. public static void TweenConForVec(this Component comp)
  201. {
  202. ManaAnim.TweenConForVec(comp.transform);
  203. }
  204. public static void TweenConForGra(this Component comp)
  205. {
  206. ManaAnim.TweenConForGra(comp.transform);
  207. }
  208. public static void TweenConForRect(this Component comp)
  209. {
  210. ManaAnim.TweenConForRect(comp.transform);
  211. }
  212. public static void TweenConForScale(this Component comp)
  213. {
  214. ManaAnim.TweenConForScale(comp.transform);
  215. }
  216. public static void TweenConForAudio(this Component comp)
  217. {
  218. ManaAnim.TweenConForAudio(comp.transform);
  219. }
  220. public static void TweenConBacSr(this Component comp)
  221. {
  222. ManaAnim.TweenConBacSr(comp.transform);
  223. }
  224. public static void TweenConBacCG(this Component comp)
  225. {
  226. ManaAnim.TweenConBacCG(comp.transform);
  227. }
  228. public static void TweenConBacVec(this Component comp)
  229. {
  230. ManaAnim.TweenConBacVec(comp.transform);
  231. }
  232. public static void TweenConBacGra(this Component comp)
  233. {
  234. ManaAnim.TweenConBacGra(comp.transform);
  235. }
  236. public static void TweenConBacRect(this Component comp)
  237. {
  238. ManaAnim.TweenConBacRect(comp.transform);
  239. }
  240. public static void TweenConBacScale(this Component comp)
  241. {
  242. ManaAnim.TweenConBacScale(comp.transform);
  243. }
  244. public static void TweenConBacAudio(this Component comp)
  245. {
  246. ManaAnim.TweenConBacAudio(comp.transform);
  247. }
  248. public static TweenSr GetTweenSr(this Component comp)
  249. {
  250. return ManaAnim.GetTweenSr(comp.transform);
  251. }
  252. public static TweenCG GetTweenCG(this Component comp)
  253. {
  254. return ManaAnim.GetTweenCG(comp.transform);
  255. }
  256. public static TweenVec GetTweenVec(this Component comp)
  257. {
  258. return ManaAnim.GetTweenVec(comp.transform);
  259. }
  260. public static TweenGra GetTweenGra(this Component comp)
  261. {
  262. return ManaAnim.GetTweenGra(comp.transform);
  263. }
  264. public static TweenRect GetTweenRect(this Component comp)
  265. {
  266. return ManaAnim.GetTweenRect(comp.transform);
  267. }
  268. public static TweenScale GetTweenScale(this Component comp)
  269. {
  270. return ManaAnim.GetTweenScale(comp.transform);
  271. }
  272. public static TweenAudio GetTweenAudio(this Component comp)
  273. {
  274. return ManaAnim.GetTweenAudio(comp.transform);
  275. }
  276. public static TweenSr CreateTweenSr(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  277. {
  278. return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, originActive, destActive, curve);
  279. }
  280. public static TweenSr CreateTweenSr(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  281. {
  282. return ManaAnim.CreateTweenSr(comp.transform, destination, duration, originActive, destActive, curve);
  283. }
  284. public static TweenCG CreateTweenCG(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  285. {
  286. return ManaAnim.CreateTweenCG(comp.transform, origin, destination, duration, originActive, destActive, curve);
  287. }
  288. public static TweenCG CreateTweenCG(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  289. {
  290. return ManaAnim.CreateTweenCG(comp.transform, destination, duration, originActive, destActive, curve);
  291. }
  292. public static TweenGra CreateTweenGra(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  293. {
  294. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve);
  295. }
  296. public static TweenGra CreateTweenGra(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  297. {
  298. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve);
  299. }
  300. public static TweenVec CreateTweenVec(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  301. {
  302. return ManaAnim.CreateTweenVec(comp.transform, origin, destination, duration, originActive, destActive, curve);
  303. }
  304. public static TweenVec CreateTweenVec(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  305. {
  306. return ManaAnim.CreateTweenVec(comp.transform, destination, duration, originActive, destActive, curve);
  307. }
  308. public static TweenRect CreateTweenRect(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  309. {
  310. return ManaAnim.CreateTweenRect(comp.transform, origin, destination, duration, originActive, destActive, curve);
  311. }
  312. public static TweenRect CreateTweenRect(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  313. {
  314. return ManaAnim.CreateTweenRect(comp.transform, destination, duration, originActive, destActive, curve);
  315. }
  316. public static TweenScale CreateTweenScale(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  317. {
  318. return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve);
  319. }
  320. public static TweenScale CreateTweenScale(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  321. {
  322. return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve);
  323. }
  324. public static TweenAudio CreateTweenAudio(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  325. {
  326. return ManaAnim.CreateTweenAudio(comp.transform, origin, destination, duration, originActive, destActive, curve);
  327. }
  328. public static TweenAudio CreateTweenAudio(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  329. {
  330. return ManaAnim.CreateTweenAudio(comp.transform, destination, duration, originActive, destActive, curve);
  331. }
  332. #endregion
  333. #region Vector3
  334. public static void SetX(this Transform tra, float x)
  335. {
  336. tra.position = new Vector3(x, tra.position.y, tra.position.z);
  337. }
  338. public static void SetY(this Transform tra, float y)
  339. {
  340. tra.position = new Vector3(tra.position.x, y, tra.position.z);
  341. }
  342. public static void SetZ(this Transform tra, float z)
  343. {
  344. tra.position = new Vector3(tra.position.x, tra.position.y, z);
  345. }
  346. #endregion
  347. #region Collider
  348. public static void SetCollider(this Component comp, bool active)
  349. {
  350. BoxCollider2D collider = comp.GetComponent<BoxCollider2D>();
  351. if (collider)
  352. {
  353. collider.enabled = active;
  354. }
  355. }
  356. public static void SetCollider(this GameObject gameObject, bool active)
  357. {
  358. gameObject.GetComponent<BoxCollider2D>().enabled = active;
  359. }
  360. #endregion
  361. #region GetChild
  362. public static Transform GetChild(this Component comp, int index)
  363. {
  364. return comp.transform.GetChild(index);
  365. }
  366. public static Transform GetChild(this GameObject gameObject, int index)
  367. {
  368. return gameObject.transform.GetChild(index);
  369. }
  370. #endregion
  371. #region SetParent
  372. public static void SetParent(this Component comp, Transform par)
  373. {
  374. comp.transform.SetParent(par);
  375. }
  376. public static void SetParent(this GameObject go, Transform par)
  377. {
  378. go.transform.SetParent(par);
  379. }
  380. #endregion
  381. #region AddComponent
  382. public static T AddComponent<T>(this Component comp) where T : Component
  383. {
  384. return comp.transform.gameObject.AddComponent<T>();
  385. }
  386. #endregion
  387. }