Extension.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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 enum LocatePos
  10. {
  11. Left,
  12. Right,
  13. Center,
  14. Up,
  15. Down,
  16. Middle,
  17. }
  18. public static class Extension
  19. {
  20. #region List
  21. public static T Last<T>(this List<T> list, int index)
  22. {
  23. return list[list.Count - 1 - index];
  24. }
  25. public static T Prev<T>(this List<T> list, int index)
  26. {
  27. return list[(index + list.Count - 1)%list.Count];
  28. }
  29. public static T Next<T>(this List<T> list, int index)
  30. {
  31. return list[(index + 1)%list.Count];
  32. }
  33. public static T Random<T>(this List<T> list, bool remove = false)
  34. {
  35. if (list.Count == 0)
  36. {
  37. Debug.Log("Count is 0");
  38. }
  39. int index = UnityEngine.Random.Range(0, list.Count);
  40. if (remove)
  41. {
  42. T result = list[index];
  43. list.RemoveAt(index);
  44. return result;
  45. }
  46. else
  47. {
  48. return list[index];
  49. }
  50. }
  51. public static bool Valid<T>(this List<T> list)
  52. {
  53. if (list == null || list.Count == 0)
  54. {
  55. return false;
  56. }
  57. else
  58. {
  59. return true;
  60. }
  61. }
  62. public static bool UniqueAdd<T>(this List<T> list, T obj)
  63. {
  64. if (list.Contains(obj) == false)
  65. {
  66. list.Add(obj);
  67. return true;
  68. }
  69. else
  70. {
  71. return false;
  72. }
  73. }
  74. #endregion
  75. #region UGUI
  76. public static void Resize(this Image image, float ratioX, float ratioY)
  77. {
  78. Vector2 newSize = image.sprite.rect.size;
  79. newSize.x *= ratioX;
  80. newSize.y *= ratioY;
  81. image.rectTransform.sizeDelta = newSize;
  82. }
  83. #endregion
  84. #region Event
  85. public static void SetButtonEvent(this Button button, UnityAction onClick)
  86. {
  87. button.onClick = new Button.ButtonClickedEvent();
  88. button.onClick.AddListener(onClick);
  89. }
  90. public static void AddButtonEvent(this Button button, UnityAction onClick)
  91. {
  92. button.onClick.AddListener(onClick);
  93. }
  94. public static void PushButtonEvent(this Button button, UnityAction onClick)
  95. {
  96. Button.ButtonClickedEvent click = button.onClick;
  97. button.onClick = new Button.ButtonClickedEvent();
  98. button.onClick.AddListener(onClick);
  99. button.onClick.AddListener(click.Invoke);
  100. }
  101. public static void AddButtonEventOnetime(this Button button, UnityAction onClick)
  102. {
  103. onClick += () =>
  104. {
  105. button.onClick.RemoveListener(onClick);
  106. };
  107. button.onClick.AddListener(onClick);
  108. }
  109. public static void PushButtonEventOnetime(this Button button, UnityAction onClick)
  110. {
  111. onClick += () =>
  112. {
  113. button.onClick.RemoveListener(onClick);
  114. };
  115. Button.ButtonClickedEvent click = button.onClick;
  116. button.onClick = new Button.ButtonClickedEvent();
  117. button.onClick.AddListener(onClick);
  118. button.onClick.AddListener(click.Invoke);
  119. }
  120. #endregion
  121. #region Equal
  122. public static bool Equal(this int i1, int i2, int accuracy = 0)
  123. {
  124. if (Math.Abs(i1 - i2) < accuracy)
  125. {
  126. return true;
  127. }
  128. else
  129. {
  130. return false;
  131. }
  132. }
  133. public static bool Equal(this float f1, float f2, float accuracy = 0.0005f)
  134. {
  135. if (Math.Abs(f1 - f2) < accuracy)
  136. {
  137. return true;
  138. }
  139. else
  140. {
  141. return false;
  142. }
  143. }
  144. public static bool Equal(this Color c1, Color c2, float accuracy = 0.0005f)
  145. {
  146. if (Math.Abs(c1.r - c2.r) < accuracy && Math.Abs(c1.g - c2.g) < accuracy && Math.Abs(c1.b - c2.b) < accuracy && Math.Abs(c1.a - c2.a) < accuracy)
  147. {
  148. return true;
  149. }
  150. else
  151. {
  152. return false;
  153. }
  154. }
  155. public static bool Equal(this Vector2 v1, Vector2 v2, float accuracy = 0.0005f)
  156. {
  157. if (Math.Abs(v1.x - v2.x) < accuracy && Math.Abs(v1.y - v2.y) < accuracy)
  158. {
  159. return true;
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165. }
  166. public static bool Equal(this Vector3 v1, Vector3 v2, float accuracy = 0.0005f)
  167. {
  168. if (Math.Abs(v1.x - v2.x) < accuracy && Math.Abs(v1.y - v2.y) < accuracy && Math.Abs(v1.z - v2.z) < accuracy)
  169. {
  170. return true;
  171. }
  172. else
  173. {
  174. return false;
  175. }
  176. }
  177. #endregion
  178. #region Move
  179. public static Shake Shake(this Component comp, float duration, int repeat, Vector3 strength, Curve curve)
  180. {
  181. return ManaAnim.Shake(comp.transform, duration, repeat, strength, curve);
  182. }
  183. public static Move2D Move2D(this Component comp, Vector3 destination, float duration, bool local, Curve curve)
  184. {
  185. return ManaAnim.Move2D(comp.transform, destination, duration, local, curve);
  186. }
  187. public static Move3D Move3D(this Component comp, Vector3 destination, float duration, bool local, Curve curve)
  188. {
  189. return ManaAnim.Move3D(comp.transform, destination, duration, local, curve);
  190. }
  191. public static Move2D MoveOffset2D(this Component comp, Vector3 offset, float duration, bool local, Curve curve)
  192. {
  193. return ManaAnim.MoveOffset2D(comp.transform, offset, duration, local, curve);
  194. }
  195. public static Move3D MoveOffset3D(this Component comp, Vector3 offset, float duration, bool local, Curve curve)
  196. {
  197. return ManaAnim.MoveOffset3D(comp.transform, offset, duration, local, curve);
  198. }
  199. public static Zoom2D Zoom2D(this Transform target, float origin, float destination, float duration, float stay, Transform targetZoom, Curve curve)
  200. {
  201. return ManaAnim.Zoom2D(target, origin, destination, duration, stay, targetZoom, curve);
  202. }
  203. public static Zoom2D Zoom2D(this Transform target, float destination, float duration, float stay, Transform targetZoom, Curve curve)
  204. {
  205. return ManaAnim.Zoom2D(target, destination, duration, stay, targetZoom, curve);
  206. }
  207. public static Shake GetShake(this Component comp)
  208. {
  209. return ManaAnim.GetShake(comp.transform);
  210. }
  211. public static Move2D GetMove2D(this Component comp)
  212. {
  213. return ManaAnim.GetMove2D(comp.transform);
  214. }
  215. public static Move3D GetMove3D(this Component comp)
  216. {
  217. return ManaAnim.GetMove3D(comp.transform);
  218. }
  219. public static Zoom2D GetZoom2D(this Component comp)
  220. {
  221. return ManaAnim.GetZoom2D(comp.transform);
  222. }
  223. public static Shake CreateShake(this Component comp)
  224. {
  225. return ManaAnim.CreateShake(comp.transform);
  226. }
  227. public static Move2D CreateMove2D(this Component comp)
  228. {
  229. return ManaAnim.CreateMove2D(comp.transform);
  230. }
  231. public static Move3D CreateMove3D(this Component comp)
  232. {
  233. return ManaAnim.CreateMove3D(comp.transform);
  234. }
  235. public static Zoom2D CreateZoom2D(this Component comp)
  236. {
  237. return ManaAnim.CreateZoom2D(comp.transform);
  238. }
  239. #endregion
  240. #region Alpha
  241. public static void SetAlpha(this Graphic graphic, float alpha)
  242. {
  243. graphic.color = new Color(graphic.color.r, graphic.color.g, graphic.color.b, alpha);
  244. }
  245. public static void SetAlpha(this TextMesh textMesh, float alpha)
  246. {
  247. textMesh.color = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, alpha);
  248. }
  249. public static void SetAlpha(this SpriteRenderer sR, float alpha)
  250. {
  251. sR.color = new Color(sR.color.r, sR.color.g, sR.color.b, alpha);
  252. }
  253. #endregion
  254. #region String
  255. public static T ToEnum<T>(this string str)
  256. {
  257. return (T)Enum.Parse(typeof(T), str);
  258. }
  259. #endregion
  260. #region Sprite
  261. public static Sprite GetSprite(this Component comp)
  262. {
  263. return comp.GetComponent<Image>().sprite;
  264. }
  265. #endregion
  266. #region String
  267. public static string Remove(this string str, int startIndex, int endIndex, bool empty)
  268. {
  269. if (startIndex > endIndex)
  270. {
  271. throw new Exception();
  272. }
  273. return str.Remove(startIndex, endIndex - startIndex + 1);
  274. }
  275. public static string Replace(this string str, int startIndex, int endIndex, string newStr)
  276. {
  277. str = str.Remove(startIndex, endIndex - startIndex + 1);
  278. str = str.Insert(startIndex, newStr);
  279. return str;
  280. }
  281. public static string Between(this string str, int startIndex, int endIndex)
  282. {
  283. if (startIndex > endIndex)
  284. {
  285. return "";
  286. }
  287. else if (startIndex == endIndex)
  288. {
  289. return str[startIndex].ToString();
  290. }
  291. else
  292. {
  293. return str.Substring(startIndex, endIndex - startIndex + 1);
  294. }
  295. }
  296. #endregion
  297. #region Regist
  298. public static T AddScript<T>(this Component comp) where T : Component
  299. {
  300. return AddScript<T>(comp.gameObject);
  301. }
  302. public static T AddScript<T>(this GameObject go) where T : Component
  303. {
  304. Component comp = go.AddComponent(typeof(T));
  305. if (comp is Regist)
  306. {
  307. Regist regist = (Regist) comp;
  308. regist.enabled = false;
  309. regist.RegistImmed();
  310. Initializer.RegistList.Add(regist);
  311. return (T) comp;
  312. }
  313. else
  314. {
  315. throw new Exception();
  316. }
  317. }
  318. #endregion
  319. #region Active
  320. public static void SetActive(this Component comp, bool active)
  321. {
  322. comp.gameObject.SetActive(active);
  323. }
  324. #endregion
  325. #region Tween
  326. public static TweenSr TweenForSr(this Component comp)
  327. {
  328. return ManaAnim.TweenForSr(comp.transform);
  329. }
  330. public static TweenCG TweenForCG(this Component comp)
  331. {
  332. return ManaAnim.TweenForCG(comp.transform);
  333. }
  334. public static TweenVec TweenForVec(this Component comp)
  335. {
  336. return ManaAnim.TweenForVec(comp.transform);
  337. }
  338. public static TweenGra TweenForGra(this Component comp)
  339. {
  340. return ManaAnim.TweenForGra(comp.transform);
  341. }
  342. public static TweenFont TweenForFont(this Component comp)
  343. {
  344. return ManaAnim.TweenForFont(comp.transform);
  345. }
  346. public static TweenRect TweenForRect(this Component comp)
  347. {
  348. return ManaAnim.TweenForRect(comp.transform);
  349. }
  350. public static TweenScale TweenForScale(this Component comp)
  351. {
  352. return ManaAnim.TweenForScale(comp.transform);
  353. }
  354. public static TweenAudio TweenForAudio(this Component comp)
  355. {
  356. return ManaAnim.TweenForAudio(comp.transform);
  357. }
  358. public static TweenAudio TweenForAudio(this AudioSource audioSource)
  359. {
  360. return ManaAnim.TweenForAudio(audioSource);
  361. }
  362. public static TweenOutline TweenForOutline(this Component comp)
  363. {
  364. return ManaAnim.TweenForOutline(comp.transform);
  365. }
  366. public static TweenNumber TweenForNumber(this Component comp)
  367. {
  368. return ManaAnim.TweenForNumber(comp.transform);
  369. }
  370. public static TweenSr TweenBacSr(this Component comp)
  371. {
  372. return ManaAnim.TweenBacSr(comp.transform);
  373. }
  374. public static TweenCG TweenBacCG(this Component comp)
  375. {
  376. return ManaAnim.TweenBacCG(comp.transform);
  377. }
  378. public static TweenVec TweenBacVec(this Component comp)
  379. {
  380. return ManaAnim.TweenBacVec(comp.transform);
  381. }
  382. public static TweenGra TweenBacGra(this Component comp)
  383. {
  384. return ManaAnim.TweenBacGra(comp.transform);
  385. }
  386. public static TweenFont TweenBacFont(this Component comp)
  387. {
  388. return ManaAnim.TweenBacFont(comp.transform);
  389. }
  390. public static TweenRect TweenBacRect(this Component comp)
  391. {
  392. return ManaAnim.TweenBacRect(comp.transform);
  393. }
  394. public static TweenScale TweenBacScale(this Component comp)
  395. {
  396. return ManaAnim.TweenBacScale(comp.transform);
  397. }
  398. public static TweenAudio TweenBacAudio(this Component comp)
  399. {
  400. return ManaAnim.TweenBacAudio(comp.transform);
  401. }
  402. public static TweenAudio TweenBacAudio(this AudioSource audioSource)
  403. {
  404. return ManaAnim.TweenBacAudio(audioSource);
  405. }
  406. public static TweenOutline TweenBacOutline(this Component comp)
  407. {
  408. return ManaAnim.TweenBacOutline(comp.transform);
  409. }
  410. public static TweenNumber TweenBacNumber(this Component comp)
  411. {
  412. return ManaAnim.TweenBacNumber(comp.transform);
  413. }
  414. public static TweenSr TweenReForSr(this Component comp)
  415. {
  416. return ManaAnim.TweenReForSr(comp.transform);
  417. }
  418. public static TweenCG TweenReForCG(this Component comp)
  419. {
  420. return ManaAnim.TweenReForCG(comp.transform);
  421. }
  422. public static TweenVec TweenReForVec(this Component comp)
  423. {
  424. return ManaAnim.TweenReForVec(comp.transform);
  425. }
  426. public static TweenGra TweenReForGra(this Component comp)
  427. {
  428. return ManaAnim.TweenReForGra(comp.transform);
  429. }
  430. public static TweenFont TweenReForFont(this Component comp)
  431. {
  432. return ManaAnim.TweenReForFont(comp.transform);
  433. }
  434. public static TweenRect TweenReForRect(this Component comp)
  435. {
  436. return ManaAnim.TweenReForRect(comp.transform);
  437. }
  438. public static TweenScale TweenReForScale(this Component comp)
  439. {
  440. return ManaAnim.TweenReForScale(comp.transform);
  441. }
  442. public static TweenAudio TweenReForAudio(this Component comp)
  443. {
  444. return ManaAnim.TweenReForAudio(comp.transform);
  445. }
  446. public static TweenAudio TweenReForAudio(this AudioSource audioSource)
  447. {
  448. return ManaAnim.TweenReForAudio(audioSource);
  449. }
  450. public static TweenOutline TweenReForOutline(this Component comp)
  451. {
  452. return ManaAnim.TweenReForOutline(comp.transform);
  453. }
  454. public static TweenNumber TweenReForNumber(this Component comp)
  455. {
  456. return ManaAnim.TweenReForNumber(comp.transform);
  457. }
  458. public static TweenSr TweenReBacSr(this Component comp)
  459. {
  460. return ManaAnim.TweenReBacSr(comp.transform);
  461. }
  462. public static TweenCG TweenReBacCG(this Component comp)
  463. {
  464. return ManaAnim.TweenReBacCG(comp.transform);
  465. }
  466. public static TweenVec TweenReBacVec(this Component comp)
  467. {
  468. return ManaAnim.TweenReBacVec(comp.transform);
  469. }
  470. public static TweenGra TweenReBacGra(this Component comp)
  471. {
  472. return ManaAnim.TweenReBacGra(comp.transform);
  473. }
  474. public static TweenFont TweenReBacFont(this Component comp)
  475. {
  476. return ManaAnim.TweenReBacFont(comp.transform);
  477. }
  478. public static TweenRect TweenReBacRect(this Component comp)
  479. {
  480. return ManaAnim.TweenReBacRect(comp.transform);
  481. }
  482. public static TweenScale TweenReBacScale(this Component comp)
  483. {
  484. return ManaAnim.TweenReBacScale(comp.transform);
  485. }
  486. public static TweenAudio TweenReBacAudio(this Component comp)
  487. {
  488. return ManaAnim.TweenReBacAudio(comp.transform);
  489. }
  490. public static TweenAudio TweenReBacAudio(this AudioSource audioSource)
  491. {
  492. return ManaAnim.TweenReBacAudio(audioSource);
  493. }
  494. public static TweenOutline TweenReBacOutline(this Component comp)
  495. {
  496. return ManaAnim.TweenReBacOutline(comp.transform);
  497. }
  498. public static TweenNumber TweenReBacNumber(this Component comp)
  499. {
  500. return ManaAnim.TweenReBacNumber(comp.transform);
  501. }
  502. public static TweenSr GetTweenSr(this Component comp)
  503. {
  504. return ManaAnim.GetTweenSr(comp.transform);
  505. }
  506. public static TweenCG GetTweenCG(this Component comp)
  507. {
  508. return ManaAnim.GetTweenCG(comp.transform);
  509. }
  510. public static TweenVec GetTweenVec(this Component comp)
  511. {
  512. return ManaAnim.GetTweenVec(comp.transform);
  513. }
  514. public static TweenGra GetTweenGra(this Component comp)
  515. {
  516. return ManaAnim.GetTweenGra(comp.transform);
  517. }
  518. public static TweenFont GetTweenFont(this Component comp)
  519. {
  520. return ManaAnim.GetTweenFont(comp.transform);
  521. }
  522. public static TweenRect GetTweenRect(this Component comp)
  523. {
  524. return ManaAnim.GetTweenRect(comp.transform);
  525. }
  526. public static TweenScale GetTweenScale(this Component comp)
  527. {
  528. return ManaAnim.GetTweenScale(comp.transform);
  529. }
  530. public static TweenAudio GetTweenAudio(this Component comp)
  531. {
  532. return ManaAnim.GetTweenAudio(comp.transform);
  533. }
  534. public static TweenAudio GetTweenAudio(this AudioSource audioSource)
  535. {
  536. return ManaAnim.GetTweenAudio(audioSource);
  537. }
  538. public static TweenOutline GetTweenOutline(this Component comp)
  539. {
  540. return ManaAnim.GetTweenOutline(comp.transform);
  541. }
  542. public static TweenNumber GetTweenNumber(this Component comp)
  543. {
  544. return ManaAnim.GetTweenNumber(comp.transform);
  545. }
  546. 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)
  547. {
  548. return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, originActive, destActive, curve, cg, group);
  549. }
  550. public static TweenSr CreateTweenSr(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false)
  551. {
  552. return ManaAnim.CreateTweenSr(comp.transform, destination, duration, originActive, destActive, curve, cg, group);
  553. }
  554. 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)
  555. {
  556. return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, originActive, destActive, curve, cg, group);
  557. }
  558. public static TweenSr CreateTweenSr(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false)
  559. {
  560. return ManaAnim.CreateTweenSr(comp.transform, destination, duration, originActive, destActive, curve, cg, group);
  561. }
  562. public static TweenCG CreateTweenCG(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  563. {
  564. return ManaAnim.CreateTweenCG(comp.transform, origin, destination, duration, originActive, destActive, curve);
  565. }
  566. public static TweenCG CreateTweenCG(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  567. {
  568. return ManaAnim.CreateTweenCG(comp.transform, destination, duration, originActive, destActive, curve);
  569. }
  570. public static TweenGra CreateTweenGra(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  571. {
  572. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  573. }
  574. public static TweenGra CreateTweenGra(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  575. {
  576. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve, cg);
  577. }
  578. public static TweenGra CreateTweenGra(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  579. {
  580. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  581. }
  582. public static TweenGra CreateTweenGra(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  583. {
  584. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve, cg);
  585. }
  586. public static TweenVec CreateTweenVec2D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  587. {
  588. return ManaAnim.CreateTweenVec2D(comp.transform, origin, destination, duration, local, originActive, destActive, curve, cg);
  589. }
  590. public static TweenVec CreateTweenVec2D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  591. {
  592. return ManaAnim.CreateTweenVec2D(comp.transform, destination, duration, local, originActive, destActive, curve, cg);
  593. }
  594. public static TweenVec CreateTweenVec3D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  595. {
  596. return ManaAnim.CreateTweenVec3D(comp.transform, origin, destination, duration, local, originActive, destActive, curve, cg);
  597. }
  598. public static TweenVec CreateTweenVec3D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  599. {
  600. return ManaAnim.CreateTweenVec3D(comp.transform, destination, duration, local, originActive, destActive, curve, cg);
  601. }
  602. public static TweenVec CreateTweenVecOffset2D(this Component comp, Vector3 offset, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  603. {
  604. return ManaAnim.CreateTweenVecOffset2D(comp.transform, offset, duration, local, originActive, destActive, curve, cg);
  605. }
  606. public static TweenVec CreateTweenVecOffset3D(this Component comp, Vector3 offset, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  607. {
  608. return ManaAnim.CreateTweenVecOffset3D(comp.transform, offset, duration, local, originActive, destActive, curve, cg);
  609. }
  610. public static TweenFont CreateTweenFont(this Component comp, int origin, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  611. {
  612. return ManaAnim.CreateTweenFont(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  613. }
  614. public static TweenFont CreateTweenFont(this Component comp, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  615. {
  616. return ManaAnim.CreateTweenFont(comp.transform, destination, duration, originActive, destActive, curve, cg);
  617. }
  618. public static TweenRect CreateTweenRect(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  619. {
  620. return ManaAnim.CreateTweenRect(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  621. }
  622. public static TweenRect CreateTweenRect(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  623. {
  624. return ManaAnim.CreateTweenRect(comp.transform, destination, duration, originActive, destActive, curve, cg);
  625. }
  626. public static TweenScale CreateTweenScale(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  627. {
  628. return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  629. }
  630. public static TweenScale CreateTweenScale(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  631. {
  632. return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve, cg);
  633. }
  634. public static TweenScale CreateTweenScale(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  635. {
  636. return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  637. }
  638. public static TweenScale CreateTweenScale(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  639. {
  640. return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve, cg);
  641. }
  642. public static TweenAudio CreateTweenAudio(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  643. {
  644. return ManaAnim.CreateTweenAudio(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  645. }
  646. public static TweenAudio CreateTweenAudio(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  647. {
  648. return ManaAnim.CreateTweenAudio(comp.transform, destination, duration, originActive, destActive, curve, cg);
  649. }
  650. public static TweenAudio CreateTweenAudio(this AudioSource audioSource, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  651. {
  652. return ManaAnim.CreateTweenAudio(audioSource, origin, destination, duration, originActive, destActive, curve, cg);
  653. }
  654. public static TweenAudio CreateTweenAudio(this AudioSource audioSource, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  655. {
  656. return ManaAnim.CreateTweenAudio(audioSource, destination, duration, originActive, destActive, curve, cg);
  657. }
  658. public static TweenOutline CreateTweenOutline(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  659. {
  660. return ManaAnim.CreateTweenOutline(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  661. }
  662. public static TweenOutline CreateTweenOutline(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  663. {
  664. return ManaAnim.CreateTweenOutline(comp.transform, destination, duration, originActive, destActive, curve, cg);
  665. }
  666. public static TweenOutline CreateTweenOutline(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  667. {
  668. return ManaAnim.CreateTweenOutline(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  669. }
  670. public static TweenOutline CreateTweenOutline(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  671. {
  672. return ManaAnim.CreateTweenOutline(comp.transform, destination, duration, originActive, destActive, curve, cg);
  673. }
  674. public static TweenNumber CreateTweenNumber(this Component comp, int origin, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  675. {
  676. return ManaAnim.CreateTweenNumber(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  677. }
  678. public static TweenNumber CreateTweenNumber(this Component comp, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  679. {
  680. return ManaAnim.CreateTweenNumber(comp.transform, destination, duration, originActive, destActive, curve, cg);
  681. }
  682. #endregion
  683. #region Stream
  684. public static StreamScale StreamForScale(this Component comp)
  685. {
  686. return ManaAnim.StreamForScale(comp.transform);
  687. }
  688. public static StreamScale GetStreamScale(this Component comp)
  689. {
  690. return ManaAnim.GetStreamScale(comp.transform);
  691. }
  692. public static StreamScale CreateStreamScale(this Component comp, List<float> delayList, List<float> durationList, List<VecPair> destKvList, bool originActive, bool destActive, Curve curve, bool cg = false, List<UnityAction> actionList = null)
  693. {
  694. return ManaAnim.CreateStreamScale(comp.transform, delayList, durationList, destKvList, originActive, destActive, curve, cg, actionList);
  695. }
  696. public static StreamScale CreateStreamScale(this Component comp, List<float> delayList, List<float> durationList, List<Vector3> destList, bool originActive, bool destActive, Curve curve, bool cg = false, List<UnityAction> actionList = null)
  697. {
  698. return ManaAnim.CreateStreamScale(comp.transform, delayList, durationList, destList, originActive, destActive, curve, cg, actionList);
  699. }
  700. #endregion
  701. #region Collider
  702. public static void SetCollider(this Component comp, bool active)
  703. {
  704. BoxCollider2D collider = comp.GetComponent<BoxCollider2D>();
  705. if (collider)
  706. {
  707. collider.enabled = active;
  708. }
  709. }
  710. public static void SetCollider(this GameObject gameObject, bool active)
  711. {
  712. gameObject.GetComponent<BoxCollider2D>().enabled = active;
  713. }
  714. #endregion
  715. #region Material
  716. public static void SetAlpha(this Material material, float alpha, string propertyName)
  717. {
  718. Color color = material.GetColor(propertyName);
  719. color.a = alpha;
  720. material.SetColor(propertyName, color);
  721. }
  722. public static float GetAlpha(this Material material, string propertyName)
  723. {
  724. Color color = material.GetColor(propertyName);
  725. return color.a;
  726. }
  727. #endregion
  728. #region GetChild
  729. public static Transform GetChild(this Component comp, int index)
  730. {
  731. return comp.transform.GetChild(index);
  732. }
  733. public static Transform GetChild(this GameObject gameObject, int index)
  734. {
  735. return gameObject.transform.GetChild(index);
  736. }
  737. #endregion
  738. #region SetParent
  739. public static void SetParent(this Component comp, Transform par)
  740. {
  741. comp.transform.SetParent(par);
  742. }
  743. public static void SetParent(this GameObject go, Transform par)
  744. {
  745. go.transform.SetParent(par);
  746. }
  747. #endregion
  748. #region ScrollRect
  749. public static Move Locate(this ScrollRect scrollRect, int index, float duration, Curve curve, LocatePos locatePos)
  750. {
  751. LayoutGroup layoutGroup = scrollRect.content.GetComponent<LayoutGroup>();
  752. RectTransform tra1 = scrollRect.GetComponent<RectTransform>();
  753. Rect rect1 = tra1.rect;
  754. RectTransform tra2 = scrollRect.content.GetChild(index).GetComponent<RectTransform>();
  755. Rect rect2 = tra2.rect;
  756. if (locatePos == LocatePos.Up)
  757. {
  758. Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMax, 0);
  759. Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMax - layoutGroup.padding.top, 0);
  760. Vector3 offset = targetPos - itemPos;
  761. offset.x = 0;
  762. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  763. }
  764. if (locatePos == LocatePos.Down)
  765. {
  766. Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMin, 0);
  767. Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMin + layoutGroup.padding.bottom, 0);
  768. Vector3 offset = targetPos - itemPos;
  769. offset.x = 0;
  770. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  771. }
  772. else if (locatePos == LocatePos.Middle)
  773. {
  774. Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0);
  775. Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0);
  776. Vector3 offset = targetPos - itemPos;
  777. offset.x = 0;
  778. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  779. }
  780. if (locatePos == LocatePos.Left)
  781. {
  782. Vector3 itemPos = tra2.position + new Vector3(rect2.xMin, 0, 0);
  783. Vector3 targetPos = tra1.position + new Vector3(rect1.xMin + layoutGroup.padding.left, 0, 0);
  784. Vector3 offset = targetPos - itemPos;
  785. offset.y = 0;
  786. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  787. }
  788. if (locatePos == LocatePos.Right)
  789. {
  790. Vector3 itemPos = tra2.position + new Vector3(rect2.xMax, 0, 0);
  791. Vector3 targetPos = tra1.position + new Vector3(rect1.xMax - layoutGroup.padding.right, 0, 0);
  792. Vector3 offset = targetPos - itemPos;
  793. offset.y = 0;
  794. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  795. }
  796. if (locatePos == LocatePos.Center)
  797. {
  798. Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0);
  799. Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0);
  800. Vector3 offset = targetPos - itemPos;
  801. offset.y = 0;
  802. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  803. }
  804. throw new Exception();
  805. }
  806. public static Move Locate(this ScrollRect scrollRect, Transform item, float duration, Curve curve, LocatePos locatePos)
  807. {
  808. return scrollRect.Locate(item.GetSiblingIndex(), duration, curve, locatePos);
  809. }
  810. #endregion
  811. #region Transform
  812. public static void SetX(this Transform tra, float x)
  813. {
  814. tra.position = new Vector3(x, tra.position.y, tra.position.z);
  815. }
  816. public static void SetY(this Transform tra, float y)
  817. {
  818. tra.position = new Vector3(tra.position.x, y, tra.position.z);
  819. }
  820. public static void SetZ(this Transform tra, float z)
  821. {
  822. tra.position = new Vector3(tra.position.x, tra.position.y, z);
  823. }
  824. public static void SetLX(this Transform tra, float x)
  825. {
  826. tra.localPosition = new Vector3(x, tra.localPosition.y, tra.localPosition.z);
  827. }
  828. public static void SetLY(this Transform tra, float y)
  829. {
  830. tra.localPosition = new Vector3(tra.localPosition.x, y, tra.localPosition.z);
  831. }
  832. public static void SetLZ(this Transform tra, float z)
  833. {
  834. tra.localPosition = new Vector3(tra.localPosition.x, tra.localPosition.y, z);
  835. }
  836. public static void SetEX(this Transform tra, float x)
  837. {
  838. tra.eulerAngles = new Vector3(x, tra.eulerAngles.y, tra.eulerAngles.z);
  839. }
  840. public static void SetEY(this Transform tra, float y)
  841. {
  842. tra.eulerAngles = new Vector3(tra.eulerAngles.x, y, tra.eulerAngles.z);
  843. }
  844. public static void SetEZ(this Transform tra, float z)
  845. {
  846. tra.eulerAngles = new Vector3(tra.eulerAngles.x, tra.eulerAngles.y, z);
  847. }
  848. public static Vector3 GetScale(this Transform tra)
  849. {
  850. Vector3 scale = tra.localScale;
  851. while (tra.parent != null)
  852. {
  853. float x = scale.x * tra.parent.localScale.x;
  854. float y = scale.y * tra.parent.localScale.y;
  855. float z = scale.z * tra.parent.localScale.z;
  856. scale = new Vector3(x, y, z);
  857. tra = tra.parent;
  858. }
  859. return scale;
  860. }
  861. #endregion
  862. #region Dictionary
  863. public static T2 Random<T1, T2>(this Dictionary<T1, T2> dic)
  864. {
  865. return dic.Values.ToList().Random();
  866. }
  867. public static bool UniqueAdd<T1, T2>(this Dictionary<T1, T2> dic, T1 t1, T2 t2)
  868. {
  869. if (dic.ContainsKey(t1))
  870. {
  871. return false;
  872. }
  873. else
  874. {
  875. dic.Add(t1, t2);
  876. return true;
  877. }
  878. }
  879. #endregion
  880. #region UnityAction
  881. public static void SafeInvoke(this UnityAction action)
  882. {
  883. if (action != null)
  884. {
  885. action.Invoke();
  886. }
  887. }
  888. public static void SafeInvoke<T>(this UnityAction<T> action, T t)
  889. {
  890. if (action != null)
  891. {
  892. action.Invoke(t);
  893. }
  894. }
  895. #endregion
  896. #region AddComponent
  897. public static T AddComponent<T>(this Component comp) where T : Component
  898. {
  899. return comp.transform.gameObject.AddComponent<T>();
  900. }
  901. #endregion
  902. }