Extension.cs 15 KB

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