Extension.cs 18 KB

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