Extension.cs 16 KB

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