Extension.cs 21 KB

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