Extension.cs 21 KB

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