Extension.cs 32 KB

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