Extension.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine.Events;
  7. using Random = UnityEngine.Random;
  8. public static class Extension
  9. {
  10. #region List
  11. public static T Last<T>(this List<T> list, int index)
  12. {
  13. return list[list.Count - 1 - index];
  14. }
  15. public static T Random<T>(this List<T> list)
  16. {
  17. if (list.Count == 0)
  18. {
  19. Debug.Log("Count is 0");
  20. }
  21. return list[UnityEngine.Random.Range(0, list.Count)];
  22. }
  23. public static bool Valid<T>(this List<T> list)
  24. {
  25. if (list == null || list.Count == 0)
  26. {
  27. return false;
  28. }
  29. else
  30. {
  31. return true;
  32. }
  33. }
  34. public static bool UniqueAdd<T>(this List<T> list, T obj)
  35. {
  36. if (list.Contains(obj) == false)
  37. {
  38. list.Add(obj);
  39. return true;
  40. }
  41. else
  42. {
  43. return false;
  44. }
  45. }
  46. #endregion
  47. #region Move
  48. public static Shake Shake(this Component comp, float duration, int repeat, Vector3 strength, Curve curve)
  49. {
  50. return ManaAnim.Shake(comp.transform, duration, repeat, strength, curve);
  51. }
  52. public static Move2D Move2D(this Component comp, Vector3 destination, float duration, bool local, Curve curve)
  53. {
  54. return ManaAnim.Move2D(comp.transform, destination, duration, local, curve);
  55. }
  56. public static Move3D Move3D(this Component comp, Vector3 destination, float duration, bool local, Curve curve)
  57. {
  58. return ManaAnim.Move3D(comp.transform, destination, duration, local, curve);
  59. }
  60. public static Shake GetShake(this Component comp)
  61. {
  62. return ManaAnim.GetShake(comp.transform);
  63. }
  64. public static Move2D GetMove2D(this Component comp)
  65. {
  66. return ManaAnim.GetMove2D(comp.transform);
  67. }
  68. public static Move3D GetMove3D(this Component comp)
  69. {
  70. return ManaAnim.GetMove3D(comp.transform);
  71. }
  72. public static Shake CreateShake(this Component comp)
  73. {
  74. return ManaAnim.CreateShake(comp.transform);
  75. }
  76. public static Move2D CreateMove2D(this Component comp)
  77. {
  78. return ManaAnim.CreateMove2D(comp.transform);
  79. }
  80. public static Move3D CreateMove3D(this Component comp)
  81. {
  82. return ManaAnim.CreateMove3D(comp.transform);
  83. }
  84. #endregion
  85. #region Alpha
  86. public static void SetAlpha(this Graphic graphic, float alpha)
  87. {
  88. graphic.color = new Color(graphic.color.r, graphic.color.g, graphic.color.b, alpha);
  89. }
  90. public static void SetAlpha(this TextMesh textMesh, float alpha)
  91. {
  92. textMesh.color = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, alpha);
  93. }
  94. public static void SetAlpha(this SpriteRenderer sR, float alpha)
  95. {
  96. sR.color = new Color(sR.color.r, sR.color.g, sR.color.b, alpha);
  97. }
  98. #endregion
  99. #region Sprite
  100. public static Sprite GetSprite(this Component comp)
  101. {
  102. return comp.GetComponent<Image>().sprite;
  103. }
  104. #endregion
  105. #region String
  106. public static string Replace(this string str, int startIndex, int endIndex, string newStr)
  107. {
  108. str = str.Remove(startIndex, endIndex - startIndex + 1);
  109. str = str.Insert(startIndex, newStr);
  110. return str;
  111. }
  112. public static string Between(this string str, int startIndex, int endIndex)
  113. {
  114. if (startIndex > endIndex)
  115. {
  116. return "";
  117. }
  118. else if (startIndex == endIndex)
  119. {
  120. return str[startIndex].ToString();
  121. }
  122. else
  123. {
  124. return str.Substring(startIndex, endIndex - startIndex + 1);
  125. }
  126. }
  127. #endregion
  128. #region Regist
  129. public static void AddScript<T>(this Component comp)
  130. {
  131. AddScript<T>(comp.gameObject);
  132. }
  133. public static void AddScript<T>(this GameObject go)
  134. {
  135. Component comp = go.AddComponent(typeof(T));
  136. if (comp is Regist)
  137. {
  138. ((Regist)comp).enabled = false;
  139. Initializer.RegistList.Add((Regist)comp);
  140. }
  141. }
  142. #endregion
  143. #region Active
  144. public static void SetActive(this Component comp, bool active)
  145. {
  146. comp.gameObject.SetActive(active);
  147. }
  148. #endregion
  149. #region Tween
  150. public static TweenSr TweenForSr(this Component comp)
  151. {
  152. return ManaAnim.TweenForSr(comp.transform);
  153. }
  154. public static TweenCG TweenForCG(this Component comp)
  155. {
  156. return ManaAnim.TweenForCG(comp.transform);
  157. }
  158. public static TweenVec TweenForVec(this Component comp)
  159. {
  160. return ManaAnim.TweenForVec(comp.transform);
  161. }
  162. public static TweenGra TweenForGra(this Component comp)
  163. {
  164. return ManaAnim.TweenForGra(comp.transform);
  165. }
  166. public static TweenText TweenForText(this Component comp)
  167. {
  168. return ManaAnim.TweenForText(comp.transform);
  169. }
  170. public static TweenRect TweenForRect(this Component comp)
  171. {
  172. return ManaAnim.TweenForRect(comp.transform);
  173. }
  174. public static TweenScale TweenForScale(this Component comp)
  175. {
  176. return ManaAnim.TweenForScale(comp.transform);
  177. }
  178. public static TweenAudio TweenForAudio(this Component comp)
  179. {
  180. return ManaAnim.TweenForAudio(comp.transform);
  181. }
  182. public static TweenNumber TweenForNumber(this Component comp)
  183. {
  184. return ManaAnim.TweenForNumber(comp.transform);
  185. }
  186. public static TweenSr TweenBacSr(this Component comp)
  187. {
  188. return ManaAnim.TweenBacSr(comp.transform);
  189. }
  190. public static TweenCG TweenBacCG(this Component comp)
  191. {
  192. return ManaAnim.TweenBacCG(comp.transform);
  193. }
  194. public static TweenVec TweenBacVec(this Component comp)
  195. {
  196. return ManaAnim.TweenBacVec(comp.transform);
  197. }
  198. public static TweenGra TweenBacGra(this Component comp)
  199. {
  200. return ManaAnim.TweenBacGra(comp.transform);
  201. }
  202. public static TweenText TweenBacText(this Component comp)
  203. {
  204. return ManaAnim.TweenBacText(comp.transform);
  205. }
  206. public static TweenRect TweenBacRect(this Component comp)
  207. {
  208. return ManaAnim.TweenBacRect(comp.transform);
  209. }
  210. public static TweenScale TweenBacScale(this Component comp)
  211. {
  212. return ManaAnim.TweenBacScale(comp.transform);
  213. }
  214. public static TweenAudio TweenBacAudio(this Component comp)
  215. {
  216. return ManaAnim.TweenBacAudio(comp.transform);
  217. }
  218. public static TweenNumber TweenBacNumber(this Component comp)
  219. {
  220. return ManaAnim.TweenBacNumber(comp.transform);
  221. }
  222. public static TweenSr TweenConForSr(this Component comp)
  223. {
  224. return ManaAnim.TweenConForSr(comp.transform);
  225. }
  226. public static TweenCG TweenConForCG(this Component comp)
  227. {
  228. return ManaAnim.TweenConForCG(comp.transform);
  229. }
  230. public static TweenVec TweenConForVec(this Component comp)
  231. {
  232. return ManaAnim.TweenConForVec(comp.transform);
  233. }
  234. public static TweenGra TweenConForGra(this Component comp)
  235. {
  236. return ManaAnim.TweenConForGra(comp.transform);
  237. }
  238. public static TweenText TweenConForText(this Component comp)
  239. {
  240. return ManaAnim.TweenConForText(comp.transform);
  241. }
  242. public static TweenRect TweenConForRect(this Component comp)
  243. {
  244. return ManaAnim.TweenConForRect(comp.transform);
  245. }
  246. public static TweenScale TweenConForScale(this Component comp)
  247. {
  248. return ManaAnim.TweenConForScale(comp.transform);
  249. }
  250. public static TweenAudio TweenConForAudio(this Component comp)
  251. {
  252. return ManaAnim.TweenConForAudio(comp.transform);
  253. }
  254. public static TweenNumber TweenConForNumber(this Component comp)
  255. {
  256. return ManaAnim.TweenConForNumber(comp.transform);
  257. }
  258. public static TweenSr TweenConBacSr(this Component comp)
  259. {
  260. return ManaAnim.TweenConBacSr(comp.transform);
  261. }
  262. public static TweenCG TweenConBacCG(this Component comp)
  263. {
  264. return ManaAnim.TweenConBacCG(comp.transform);
  265. }
  266. public static TweenVec TweenConBacVec(this Component comp)
  267. {
  268. return ManaAnim.TweenConBacVec(comp.transform);
  269. }
  270. public static TweenGra TweenConBacGra(this Component comp)
  271. {
  272. return ManaAnim.TweenConBacGra(comp.transform);
  273. }
  274. public static TweenText TweenConBacText(this Component comp)
  275. {
  276. return ManaAnim.TweenConBacText(comp.transform);
  277. }
  278. public static TweenRect TweenConBacRect(this Component comp)
  279. {
  280. return ManaAnim.TweenConBacRect(comp.transform);
  281. }
  282. public static TweenScale TweenConBacScale(this Component comp)
  283. {
  284. return ManaAnim.TweenConBacScale(comp.transform);
  285. }
  286. public static TweenAudio TweenConBacAudio(this Component comp)
  287. {
  288. return ManaAnim.TweenConBacAudio(comp.transform);
  289. }
  290. public static TweenNumber TweenConBacNumber(this Component comp)
  291. {
  292. return ManaAnim.TweenConBacNumber(comp.transform);
  293. }
  294. public static TweenSr GetTweenSr(this Component comp)
  295. {
  296. return ManaAnim.GetTweenSr(comp.transform);
  297. }
  298. public static TweenCG GetTweenCG(this Component comp)
  299. {
  300. return ManaAnim.GetTweenCG(comp.transform);
  301. }
  302. public static TweenVec GetTweenVec(this Component comp)
  303. {
  304. return ManaAnim.GetTweenVec(comp.transform);
  305. }
  306. public static TweenGra GetTweenGra(this Component comp)
  307. {
  308. return ManaAnim.GetTweenGra(comp.transform);
  309. }
  310. public static TweenText GetTweenText(this Component comp)
  311. {
  312. return ManaAnim.GetTweenText(comp.transform);
  313. }
  314. public static TweenRect GetTweenRect(this Component comp)
  315. {
  316. return ManaAnim.GetTweenRect(comp.transform);
  317. }
  318. public static TweenScale GetTweenScale(this Component comp)
  319. {
  320. return ManaAnim.GetTweenScale(comp.transform);
  321. }
  322. public static TweenAudio GetTweenAudio(this Component comp)
  323. {
  324. return ManaAnim.GetTweenAudio(comp.transform);
  325. }
  326. public static TweenNumber GetTweenNumber(this Component comp)
  327. {
  328. return ManaAnim.GetTweenNumber(comp.transform);
  329. }
  330. public static TweenSr CreateTweenSr(this Component comp, float origin, float destination, float duration, bool group, bool originActive, bool destActive, Curve curve)
  331. {
  332. return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, group, originActive, destActive, curve);
  333. }
  334. public static TweenSr CreateTweenSr(this Component comp, float destination, float duration, bool group, bool originActive, bool destActive, Curve curve)
  335. {
  336. return ManaAnim.CreateTweenSr(comp.transform, destination, duration, group, originActive, destActive, curve);
  337. }
  338. public static TweenSr CreateTweenSr(this Component comp, Color origin, Color destination, float duration, bool group, bool originActive, bool destActive, Curve curve)
  339. {
  340. return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, group, originActive, destActive, curve);
  341. }
  342. public static TweenSr CreateTweenSr(this Component comp, Color destination, float duration, bool group, bool originActive, bool destActive, Curve curve)
  343. {
  344. return ManaAnim.CreateTweenSr(comp.transform, destination, duration, group, originActive, destActive, curve);
  345. }
  346. public static TweenCG CreateTweenCG(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  347. {
  348. return ManaAnim.CreateTweenCG(comp.transform, origin, destination, duration, originActive, destActive, curve);
  349. }
  350. public static TweenCG CreateTweenCG(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  351. {
  352. return ManaAnim.CreateTweenCG(comp.transform, destination, duration, originActive, destActive, curve);
  353. }
  354. public static TweenGra CreateTweenGra(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  355. {
  356. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve);
  357. }
  358. public static TweenGra CreateTweenGra(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  359. {
  360. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve);
  361. }
  362. public static TweenGra CreateTweenGra(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  363. {
  364. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve);
  365. }
  366. public static TweenGra CreateTweenGra(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve)
  367. {
  368. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve);
  369. }
  370. public static TweenVec CreateTweenVec2D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve)
  371. {
  372. return ManaAnim.CreateTweenVec2D(comp.transform, origin, destination, duration, local, originActive, destActive, curve);
  373. }
  374. public static TweenVec CreateTweenVec2D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve)
  375. {
  376. return ManaAnim.CreateTweenVec2D(comp.transform, destination, duration, local, originActive, destActive, curve);
  377. }
  378. public static TweenVec CreateTweenVec3D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve)
  379. {
  380. return ManaAnim.CreateTweenVec3D(comp.transform, origin, destination, duration, local, originActive, destActive, curve);
  381. }
  382. public static TweenVec CreateTweenVec3D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve)
  383. {
  384. return ManaAnim.CreateTweenVec3D(comp.transform, destination, duration, local, originActive, destActive, curve);
  385. }
  386. public static TweenText CreateTweenText(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  387. {
  388. return ManaAnim.CreateTweenText(comp.transform, origin, destination, duration, originActive, destActive, curve);
  389. }
  390. public static TweenText CreateTweenText(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  391. {
  392. return ManaAnim.CreateTweenText(comp.transform, destination, duration, originActive, destActive, curve);
  393. }
  394. public static TweenRect CreateTweenRect(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  395. {
  396. return ManaAnim.CreateTweenRect(comp.transform, origin, destination, duration, originActive, destActive, curve);
  397. }
  398. public static TweenRect CreateTweenRect(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  399. {
  400. return ManaAnim.CreateTweenRect(comp.transform, destination, duration, originActive, destActive, curve);
  401. }
  402. public static TweenScale CreateTweenScale(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  403. {
  404. return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve);
  405. }
  406. public static TweenScale CreateTweenScale(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve)
  407. {
  408. return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve);
  409. }
  410. public static TweenAudio CreateTweenAudio(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  411. {
  412. return ManaAnim.CreateTweenAudio(comp.transform, origin, destination, duration, originActive, destActive, curve);
  413. }
  414. public static TweenAudio CreateTweenAudio(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  415. {
  416. return ManaAnim.CreateTweenAudio(comp.transform, destination, duration, originActive, destActive, curve);
  417. }
  418. public static TweenNumber CreateTweenNumber(this Component comp, int origin, int destination, float duration, bool originActive, bool destActive, Curve curve)
  419. {
  420. return ManaAnim.CreateTweenNumber(comp.transform, origin, destination, duration, originActive, destActive, curve);
  421. }
  422. public static TweenNumber CreateTweenNumber(this Component comp, int destination, float duration, bool originActive, bool destActive, Curve curve)
  423. {
  424. return ManaAnim.CreateTweenNumber(comp.transform, destination, duration, originActive, destActive, curve);
  425. }
  426. #endregion
  427. #region Collider
  428. public static void SetCollider(this Component comp, bool active)
  429. {
  430. BoxCollider2D collider = comp.GetComponent<BoxCollider2D>();
  431. if (collider)
  432. {
  433. collider.enabled = active;
  434. }
  435. }
  436. public static void SetCollider(this GameObject gameObject, bool active)
  437. {
  438. gameObject.GetComponent<BoxCollider2D>().enabled = active;
  439. }
  440. #endregion
  441. #region GetChild
  442. public static Transform GetChild(this Component comp, int index)
  443. {
  444. return comp.transform.GetChild(index);
  445. }
  446. public static Transform GetChild(this GameObject gameObject, int index)
  447. {
  448. return gameObject.transform.GetChild(index);
  449. }
  450. #endregion
  451. #region SetParent
  452. public static void SetParent(this Component comp, Transform par)
  453. {
  454. comp.transform.SetParent(par);
  455. }
  456. public static void SetParent(this GameObject go, Transform par)
  457. {
  458. go.transform.SetParent(par);
  459. }
  460. #endregion
  461. #region Transform
  462. public static void SetX(this Transform tra, float x)
  463. {
  464. tra.position = new Vector3(x, tra.position.y, tra.position.z);
  465. }
  466. public static void SetY(this Transform tra, float y)
  467. {
  468. tra.position = new Vector3(tra.position.x, y, tra.position.z);
  469. }
  470. public static void SetZ(this Transform tra, float z)
  471. {
  472. tra.position = new Vector3(tra.position.x, tra.position.y, z);
  473. }
  474. public static void SetEX(this Transform tra, float x)
  475. {
  476. tra.eulerAngles = new Vector3(x, tra.eulerAngles.y, tra.eulerAngles.z);
  477. }
  478. public static void SetEY(this Transform tra, float y)
  479. {
  480. tra.eulerAngles = new Vector3(tra.eulerAngles.x, y, tra.eulerAngles.z);
  481. }
  482. public static void SetEZ(this Transform tra, float z)
  483. {
  484. tra.eulerAngles = new Vector3(tra.eulerAngles.x, tra.eulerAngles.y, z);
  485. }
  486. public static Vector3 GetScale(this Transform tra)
  487. {
  488. Vector3 scale = tra.localScale;
  489. while (tra.parent != null)
  490. {
  491. float x = scale.x * tra.parent.localScale.x;
  492. float y = scale.y * tra.parent.localScale.y;
  493. float z = scale.z * tra.parent.localScale.z;
  494. scale = new Vector3(x, y, z);
  495. tra = tra.parent;
  496. }
  497. return scale;
  498. }
  499. #endregion
  500. #region UnityAction
  501. public static void SafeInvoke(this UnityAction action)
  502. {
  503. if (action != null)
  504. {
  505. action.Invoke();
  506. }
  507. }
  508. #endregion
  509. #region AddComponent
  510. public static T AddComponent<T>(this Component comp) where T : Component
  511. {
  512. return comp.transform.gameObject.AddComponent<T>();
  513. }
  514. #endregion
  515. }