Extension.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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 String
  251. public static T ToEnum<T>(this string str)
  252. {
  253. return (T)Enum.Parse(typeof(T), str);
  254. }
  255. #endregion
  256. #region Sprite
  257. public static Sprite GetSprite(this Component comp)
  258. {
  259. return comp.GetComponent<Image>().sprite;
  260. }
  261. #endregion
  262. #region String
  263. public static string Replace(this string str, int startIndex, int endIndex, string newStr)
  264. {
  265. str = str.Remove(startIndex, endIndex - startIndex + 1);
  266. str = str.Insert(startIndex, newStr);
  267. return str;
  268. }
  269. public static string Between(this string str, int startIndex, int endIndex)
  270. {
  271. if (startIndex > endIndex)
  272. {
  273. return "";
  274. }
  275. else if (startIndex == endIndex)
  276. {
  277. return str[startIndex].ToString();
  278. }
  279. else
  280. {
  281. return str.Substring(startIndex, endIndex - startIndex + 1);
  282. }
  283. }
  284. #endregion
  285. #region Regist
  286. public static T AddScript<T>(this Component comp) where T : Component
  287. {
  288. return AddScript<T>(comp.gameObject);
  289. }
  290. public static T AddScript<T>(this GameObject go) where T : Component
  291. {
  292. Component comp = go.AddComponent(typeof(T));
  293. if (comp is Regist)
  294. {
  295. Regist regist = (Regist) comp;
  296. regist.enabled = false;
  297. regist.RegistImmed();
  298. Initializer.RegistList.Add(regist);
  299. return (T) comp;
  300. }
  301. else
  302. {
  303. throw new Exception();
  304. }
  305. }
  306. #endregion
  307. #region Active
  308. public static void SetActive(this Component comp, bool active)
  309. {
  310. comp.gameObject.SetActive(active);
  311. }
  312. #endregion
  313. #region Tween
  314. public static TweenSr TweenForSr(this Component comp)
  315. {
  316. return ManaAnim.TweenForSr(comp.transform);
  317. }
  318. public static TweenCG TweenForCG(this Component comp)
  319. {
  320. return ManaAnim.TweenForCG(comp.transform);
  321. }
  322. public static TweenVec TweenForVec(this Component comp)
  323. {
  324. return ManaAnim.TweenForVec(comp.transform);
  325. }
  326. public static TweenGra TweenForGra(this Component comp)
  327. {
  328. return ManaAnim.TweenForGra(comp.transform);
  329. }
  330. public static TweenFont TweenForFont(this Component comp)
  331. {
  332. return ManaAnim.TweenForFont(comp.transform);
  333. }
  334. public static TweenRect TweenForRect(this Component comp)
  335. {
  336. return ManaAnim.TweenForRect(comp.transform);
  337. }
  338. public static TweenScale TweenForScale(this Component comp)
  339. {
  340. return ManaAnim.TweenForScale(comp.transform);
  341. }
  342. public static TweenAudio TweenForAudio(this Component comp)
  343. {
  344. return ManaAnim.TweenForAudio(comp.transform);
  345. }
  346. public static TweenAudio TweenForAudio(this AudioSource audioSource)
  347. {
  348. return ManaAnim.TweenForAudio(audioSource);
  349. }
  350. public static TweenNumber TweenForNumber(this Component comp)
  351. {
  352. return ManaAnim.TweenForNumber(comp.transform);
  353. }
  354. public static TweenSr TweenBacSr(this Component comp)
  355. {
  356. return ManaAnim.TweenBacSr(comp.transform);
  357. }
  358. public static TweenCG TweenBacCG(this Component comp)
  359. {
  360. return ManaAnim.TweenBacCG(comp.transform);
  361. }
  362. public static TweenVec TweenBacVec(this Component comp)
  363. {
  364. return ManaAnim.TweenBacVec(comp.transform);
  365. }
  366. public static TweenGra TweenBacGra(this Component comp)
  367. {
  368. return ManaAnim.TweenBacGra(comp.transform);
  369. }
  370. public static TweenFont TweenBacFont(this Component comp)
  371. {
  372. return ManaAnim.TweenBacFont(comp.transform);
  373. }
  374. public static TweenRect TweenBacRect(this Component comp)
  375. {
  376. return ManaAnim.TweenBacRect(comp.transform);
  377. }
  378. public static TweenScale TweenBacScale(this Component comp)
  379. {
  380. return ManaAnim.TweenBacScale(comp.transform);
  381. }
  382. public static TweenAudio TweenBacAudio(this Component comp)
  383. {
  384. return ManaAnim.TweenBacAudio(comp.transform);
  385. }
  386. public static TweenAudio TweenBacAudio(this AudioSource audioSource)
  387. {
  388. return ManaAnim.TweenBacAudio(audioSource);
  389. }
  390. public static TweenNumber TweenBacNumber(this Component comp)
  391. {
  392. return ManaAnim.TweenBacNumber(comp.transform);
  393. }
  394. public static TweenSr TweenReForSr(this Component comp)
  395. {
  396. return ManaAnim.TweenReForSr(comp.transform);
  397. }
  398. public static TweenCG TweenReForCG(this Component comp)
  399. {
  400. return ManaAnim.TweenReForCG(comp.transform);
  401. }
  402. public static TweenVec TweenReForVec(this Component comp)
  403. {
  404. return ManaAnim.TweenReForVec(comp.transform);
  405. }
  406. public static TweenGra TweenReForGra(this Component comp)
  407. {
  408. return ManaAnim.TweenReForGra(comp.transform);
  409. }
  410. public static TweenFont TweenReForFont(this Component comp)
  411. {
  412. return ManaAnim.TweenReForFont(comp.transform);
  413. }
  414. public static TweenRect TweenReForRect(this Component comp)
  415. {
  416. return ManaAnim.TweenReForRect(comp.transform);
  417. }
  418. public static TweenScale TweenReForScale(this Component comp)
  419. {
  420. return ManaAnim.TweenReForScale(comp.transform);
  421. }
  422. public static TweenAudio TweenReForAudio(this Component comp)
  423. {
  424. return ManaAnim.TweenReForAudio(comp.transform);
  425. }
  426. public static TweenAudio TweenReForAudio(this AudioSource audioSource)
  427. {
  428. return ManaAnim.TweenReForAudio(audioSource);
  429. }
  430. public static TweenNumber TweenReForNumber(this Component comp)
  431. {
  432. return ManaAnim.TweenReForNumber(comp.transform);
  433. }
  434. public static TweenSr TweenReBacSr(this Component comp)
  435. {
  436. return ManaAnim.TweenReBacSr(comp.transform);
  437. }
  438. public static TweenCG TweenReBacCG(this Component comp)
  439. {
  440. return ManaAnim.TweenReBacCG(comp.transform);
  441. }
  442. public static TweenVec TweenReBacVec(this Component comp)
  443. {
  444. return ManaAnim.TweenReBacVec(comp.transform);
  445. }
  446. public static TweenGra TweenReBacGra(this Component comp)
  447. {
  448. return ManaAnim.TweenReBacGra(comp.transform);
  449. }
  450. public static TweenFont TweenReBacFont(this Component comp)
  451. {
  452. return ManaAnim.TweenReBacFont(comp.transform);
  453. }
  454. public static TweenRect TweenReBacRect(this Component comp)
  455. {
  456. return ManaAnim.TweenReBacRect(comp.transform);
  457. }
  458. public static TweenScale TweenReBacScale(this Component comp)
  459. {
  460. return ManaAnim.TweenReBacScale(comp.transform);
  461. }
  462. public static TweenAudio TweenReBacAudio(this Component comp)
  463. {
  464. return ManaAnim.TweenReBacAudio(comp.transform);
  465. }
  466. public static TweenAudio TweenReBacAudio(this AudioSource audioSource)
  467. {
  468. return ManaAnim.TweenReBacAudio(audioSource);
  469. }
  470. public static TweenNumber TweenReBacNumber(this Component comp)
  471. {
  472. return ManaAnim.TweenReBacNumber(comp.transform);
  473. }
  474. public static TweenSr TweenConForSr(this Component comp)
  475. {
  476. return ManaAnim.TweenConForSr(comp.transform);
  477. }
  478. public static TweenCG TweenConForCG(this Component comp)
  479. {
  480. return ManaAnim.TweenConForCG(comp.transform);
  481. }
  482. public static TweenVec TweenConForVec(this Component comp)
  483. {
  484. return ManaAnim.TweenConForVec(comp.transform);
  485. }
  486. public static TweenGra TweenConForGra(this Component comp)
  487. {
  488. return ManaAnim.TweenConForGra(comp.transform);
  489. }
  490. public static TweenFont TweenConForFont(this Component comp)
  491. {
  492. return ManaAnim.TweenConForFont(comp.transform);
  493. }
  494. public static TweenRect TweenConForRect(this Component comp)
  495. {
  496. return ManaAnim.TweenConForRect(comp.transform);
  497. }
  498. public static TweenScale TweenConForScale(this Component comp)
  499. {
  500. return ManaAnim.TweenConForScale(comp.transform);
  501. }
  502. public static TweenAudio TweenConForAudio(this Component comp)
  503. {
  504. return ManaAnim.TweenConForAudio(comp.transform);
  505. }
  506. public static TweenAudio TweenConForAudio(this AudioSource audioSource)
  507. {
  508. return ManaAnim.TweenConForAudio(audioSource);
  509. }
  510. public static TweenNumber TweenConForNumber(this Component comp)
  511. {
  512. return ManaAnim.TweenConForNumber(comp.transform);
  513. }
  514. public static TweenSr TweenConBacSr(this Component comp)
  515. {
  516. return ManaAnim.TweenConBacSr(comp.transform);
  517. }
  518. public static TweenCG TweenConBacCG(this Component comp)
  519. {
  520. return ManaAnim.TweenConBacCG(comp.transform);
  521. }
  522. public static TweenVec TweenConBacVec(this Component comp)
  523. {
  524. return ManaAnim.TweenConBacVec(comp.transform);
  525. }
  526. public static TweenGra TweenConBacGra(this Component comp)
  527. {
  528. return ManaAnim.TweenConBacGra(comp.transform);
  529. }
  530. public static TweenFont TweenConBacFont(this Component comp)
  531. {
  532. return ManaAnim.TweenConBacFont(comp.transform);
  533. }
  534. public static TweenRect TweenConBacRect(this Component comp)
  535. {
  536. return ManaAnim.TweenConBacRect(comp.transform);
  537. }
  538. public static TweenScale TweenConBacScale(this Component comp)
  539. {
  540. return ManaAnim.TweenConBacScale(comp.transform);
  541. }
  542. public static TweenAudio TweenConBacAudio(this Component comp)
  543. {
  544. return ManaAnim.TweenConBacAudio(comp.transform);
  545. }
  546. public static TweenAudio TweenConBacAudio(this AudioSource audioSource)
  547. {
  548. return ManaAnim.TweenConBacAudio(audioSource);
  549. }
  550. public static TweenNumber TweenConBacNumber(this Component comp)
  551. {
  552. return ManaAnim.TweenConBacNumber(comp.transform);
  553. }
  554. public static TweenSr GetTweenSr(this Component comp)
  555. {
  556. return ManaAnim.GetTweenSr(comp.transform);
  557. }
  558. public static TweenCG GetTweenCG(this Component comp)
  559. {
  560. return ManaAnim.GetTweenCG(comp.transform);
  561. }
  562. public static TweenVec GetTweenVec(this Component comp)
  563. {
  564. return ManaAnim.GetTweenVec(comp.transform);
  565. }
  566. public static TweenGra GetTweenGra(this Component comp)
  567. {
  568. return ManaAnim.GetTweenGra(comp.transform);
  569. }
  570. public static TweenFont GetTweenFont(this Component comp)
  571. {
  572. return ManaAnim.GetTweenFont(comp.transform);
  573. }
  574. public static TweenRect GetTweenRect(this Component comp)
  575. {
  576. return ManaAnim.GetTweenRect(comp.transform);
  577. }
  578. public static TweenScale GetTweenScale(this Component comp)
  579. {
  580. return ManaAnim.GetTweenScale(comp.transform);
  581. }
  582. public static TweenAudio GetTweenAudio(this Component comp)
  583. {
  584. return ManaAnim.GetTweenAudio(comp.transform);
  585. }
  586. public static TweenAudio GetTweenAudio(this AudioSource audioSource)
  587. {
  588. return ManaAnim.GetTweenAudio(audioSource);
  589. }
  590. public static TweenNumber GetTweenNumber(this Component comp)
  591. {
  592. return ManaAnim.GetTweenNumber(comp.transform);
  593. }
  594. 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)
  595. {
  596. return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, originActive, destActive, curve, cg, group);
  597. }
  598. public static TweenSr CreateTweenSr(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false)
  599. {
  600. return ManaAnim.CreateTweenSr(comp.transform, destination, duration, originActive, destActive, curve, cg, group);
  601. }
  602. 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)
  603. {
  604. return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, originActive, destActive, curve, cg, group);
  605. }
  606. public static TweenSr CreateTweenSr(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false)
  607. {
  608. return ManaAnim.CreateTweenSr(comp.transform, destination, duration, originActive, destActive, curve, cg, group);
  609. }
  610. public static TweenCG CreateTweenCG(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  611. {
  612. return ManaAnim.CreateTweenCG(comp.transform, origin, destination, duration, originActive, destActive, curve);
  613. }
  614. public static TweenCG CreateTweenCG(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  615. {
  616. return ManaAnim.CreateTweenCG(comp.transform, destination, duration, originActive, destActive, curve);
  617. }
  618. public static TweenGra CreateTweenGra(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  619. {
  620. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  621. }
  622. public static TweenGra CreateTweenGra(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  623. {
  624. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve, cg);
  625. }
  626. public static TweenGra CreateTweenGra(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  627. {
  628. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  629. }
  630. public static TweenGra CreateTweenGra(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  631. {
  632. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve, cg);
  633. }
  634. public static TweenVec CreateTweenVec2D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  635. {
  636. return ManaAnim.CreateTweenVec2D(comp.transform, origin, destination, duration, local, originActive, destActive, curve, cg);
  637. }
  638. public static TweenVec CreateTweenVec2D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  639. {
  640. return ManaAnim.CreateTweenVec2D(comp.transform, destination, duration, local, originActive, destActive, curve, cg);
  641. }
  642. public static TweenVec CreateTweenVec3D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  643. {
  644. return ManaAnim.CreateTweenVec3D(comp.transform, origin, destination, duration, local, originActive, destActive, curve, cg);
  645. }
  646. public static TweenVec CreateTweenVec3D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  647. {
  648. return ManaAnim.CreateTweenVec3D(comp.transform, destination, duration, local, originActive, destActive, curve, cg);
  649. }
  650. public static TweenVec CreateTweenVecOffset2D(this Component comp, Vector3 offset, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  651. {
  652. return ManaAnim.CreateTweenVecOffset2D(comp.transform, offset, duration, local, originActive, destActive, curve, cg);
  653. }
  654. public static TweenVec CreateTweenVecOffset3D(this Component comp, Vector3 offset, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  655. {
  656. return ManaAnim.CreateTweenVecOffset3D(comp.transform, offset, duration, local, originActive, destActive, curve, cg);
  657. }
  658. public static TweenFont CreateTweenFont(this Component comp, int origin, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  659. {
  660. return ManaAnim.CreateTweenFont(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  661. }
  662. public static TweenFont CreateTweenFont(this Component comp, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  663. {
  664. return ManaAnim.CreateTweenFont(comp.transform, destination, duration, originActive, destActive, curve, cg);
  665. }
  666. public static TweenRect CreateTweenRect(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  667. {
  668. return ManaAnim.CreateTweenRect(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  669. }
  670. public static TweenRect CreateTweenRect(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  671. {
  672. return ManaAnim.CreateTweenRect(comp.transform, destination, duration, originActive, destActive, curve, cg);
  673. }
  674. public static TweenScale CreateTweenScale(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  675. {
  676. return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  677. }
  678. public static TweenScale CreateTweenScale(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  679. {
  680. return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve, cg);
  681. }
  682. public static TweenAudio CreateTweenAudio(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  683. {
  684. return ManaAnim.CreateTweenAudio(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  685. }
  686. public static TweenAudio CreateTweenAudio(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  687. {
  688. return ManaAnim.CreateTweenAudio(comp.transform, destination, duration, originActive, destActive, curve, cg);
  689. }
  690. public static TweenAudio CreateTweenAudio(this AudioSource audioSource, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  691. {
  692. return ManaAnim.CreateTweenAudio(audioSource, origin, destination, duration, originActive, destActive, curve, cg);
  693. }
  694. public static TweenAudio CreateTweenAudio(this AudioSource audioSource, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  695. {
  696. return ManaAnim.CreateTweenAudio(audioSource, destination, duration, originActive, destActive, curve, cg);
  697. }
  698. public static TweenNumber CreateTweenNumber(this Component comp, int origin, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  699. {
  700. return ManaAnim.CreateTweenNumber(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  701. }
  702. public static TweenNumber CreateTweenNumber(this Component comp, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  703. {
  704. return ManaAnim.CreateTweenNumber(comp.transform, destination, duration, originActive, destActive, curve, cg);
  705. }
  706. #endregion
  707. #region Stream
  708. public static StreamScale StreamForScale(this Component comp)
  709. {
  710. return ManaAnim.StreamForScale(comp.transform);
  711. }
  712. public static StreamScale GetStreamScale(this Component comp)
  713. {
  714. return ManaAnim.GetStreamScale(comp.transform);
  715. }
  716. 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)
  717. {
  718. return ManaAnim.CreateStreamScale(comp.transform, delayList, durationList, destKvList, originActive, destActive, curve, cg, actionList);
  719. }
  720. 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)
  721. {
  722. return ManaAnim.CreateStreamScale(comp.transform, delayList, durationList, destList, originActive, destActive, curve, cg, actionList);
  723. }
  724. #endregion
  725. #region Collider
  726. public static void SetCollider(this Component comp, bool active)
  727. {
  728. BoxCollider2D collider = comp.GetComponent<BoxCollider2D>();
  729. if (collider)
  730. {
  731. collider.enabled = active;
  732. }
  733. }
  734. public static void SetCollider(this GameObject gameObject, bool active)
  735. {
  736. gameObject.GetComponent<BoxCollider2D>().enabled = active;
  737. }
  738. #endregion
  739. #region GetChild
  740. public static Transform GetChild(this Component comp, int index)
  741. {
  742. return comp.transform.GetChild(index);
  743. }
  744. public static Transform GetChild(this GameObject gameObject, int index)
  745. {
  746. return gameObject.transform.GetChild(index);
  747. }
  748. #endregion
  749. #region SetParent
  750. public static void SetParent(this Component comp, Transform par)
  751. {
  752. comp.transform.SetParent(par);
  753. }
  754. public static void SetParent(this GameObject go, Transform par)
  755. {
  756. go.transform.SetParent(par);
  757. }
  758. #endregion
  759. #region ScrollRect
  760. public static Move Locate(this ScrollRect scrollRect, int index, float duration, Curve curve, LocatePos locatePos)
  761. {
  762. LayoutGroup layoutGroup = scrollRect.content.GetComponent<LayoutGroup>();
  763. RectTransform tra1 = scrollRect.GetComponent<RectTransform>();
  764. Rect rect1 = tra1.rect;
  765. RectTransform tra2 = scrollRect.content.GetChild(index).GetComponent<RectTransform>();
  766. Rect rect2 = tra2.rect;
  767. if (locatePos == LocatePos.Up)
  768. {
  769. Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMax, 0);
  770. Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMax - layoutGroup.padding.top, 0);
  771. Vector3 offset = targetPos - itemPos;
  772. offset.x = 0;
  773. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  774. }
  775. if (locatePos == LocatePos.Down)
  776. {
  777. Vector3 itemPos = tra2.position + new Vector3(0, rect2.yMin, 0);
  778. Vector3 targetPos = tra1.position + new Vector3(0, rect1.yMin + layoutGroup.padding.bottom, 0);
  779. Vector3 offset = targetPos - itemPos;
  780. offset.x = 0;
  781. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  782. }
  783. else if (locatePos == LocatePos.Middle)
  784. {
  785. Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0);
  786. Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0);
  787. Vector3 offset = targetPos - itemPos;
  788. offset.x = 0;
  789. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  790. }
  791. if (locatePos == LocatePos.Left)
  792. {
  793. Vector3 itemPos = tra2.position + new Vector3(rect2.xMin, 0, 0);
  794. Vector3 targetPos = tra1.position + new Vector3(rect1.xMin + layoutGroup.padding.left, 0, 0);
  795. Vector3 offset = targetPos - itemPos;
  796. offset.y = 0;
  797. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  798. }
  799. if (locatePos == LocatePos.Right)
  800. {
  801. Vector3 itemPos = tra2.position + new Vector3(rect2.xMax, 0, 0);
  802. Vector3 targetPos = tra1.position + new Vector3(rect1.xMax - layoutGroup.padding.right, 0, 0);
  803. Vector3 offset = targetPos - itemPos;
  804. offset.y = 0;
  805. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  806. }
  807. if (locatePos == LocatePos.Center)
  808. {
  809. Vector3 itemPos = tra2.position + new Vector3(rect2.center.x, rect2.center.y, 0);
  810. Vector3 targetPos = tra1.position + new Vector3(rect1.center.x, rect1.center.y, 0);
  811. Vector3 offset = targetPos - itemPos;
  812. offset.y = 0;
  813. return scrollRect.content.MoveOffset2D(offset, duration, false, curve);
  814. }
  815. throw new Exception();
  816. }
  817. public static Move Locate(this ScrollRect scrollRect, Transform item, float duration, Curve curve, LocatePos locatePos)
  818. {
  819. return scrollRect.Locate(item.GetSiblingIndex(), duration, curve, locatePos);
  820. }
  821. #endregion
  822. #region Transform
  823. public static void SetX(this Transform tra, float x)
  824. {
  825. tra.position = new Vector3(x, tra.position.y, tra.position.z);
  826. }
  827. public static void SetY(this Transform tra, float y)
  828. {
  829. tra.position = new Vector3(tra.position.x, y, tra.position.z);
  830. }
  831. public static void SetZ(this Transform tra, float z)
  832. {
  833. tra.position = new Vector3(tra.position.x, tra.position.y, z);
  834. }
  835. public static void SetLX(this Transform tra, float x)
  836. {
  837. tra.localPosition = new Vector3(x, tra.localPosition.y, tra.localPosition.z);
  838. }
  839. public static void SetLY(this Transform tra, float y)
  840. {
  841. tra.localPosition = new Vector3(tra.localPosition.x, y, tra.localPosition.z);
  842. }
  843. public static void SetLZ(this Transform tra, float z)
  844. {
  845. tra.localPosition = new Vector3(tra.localPosition.x, tra.localPosition.y, z);
  846. }
  847. public static void SetEX(this Transform tra, float x)
  848. {
  849. tra.eulerAngles = new Vector3(x, tra.eulerAngles.y, tra.eulerAngles.z);
  850. }
  851. public static void SetEY(this Transform tra, float y)
  852. {
  853. tra.eulerAngles = new Vector3(tra.eulerAngles.x, y, tra.eulerAngles.z);
  854. }
  855. public static void SetEZ(this Transform tra, float z)
  856. {
  857. tra.eulerAngles = new Vector3(tra.eulerAngles.x, tra.eulerAngles.y, z);
  858. }
  859. public static Vector3 GetScale(this Transform tra)
  860. {
  861. Vector3 scale = tra.localScale;
  862. while (tra.parent != null)
  863. {
  864. float x = scale.x * tra.parent.localScale.x;
  865. float y = scale.y * tra.parent.localScale.y;
  866. float z = scale.z * tra.parent.localScale.z;
  867. scale = new Vector3(x, y, z);
  868. tra = tra.parent;
  869. }
  870. return scale;
  871. }
  872. #endregion
  873. #region Dictionary
  874. public static T2 Random<T1, T2>(this Dictionary<T1, T2> dic)
  875. {
  876. return dic.Values.ToList().Random();
  877. }
  878. public static bool UniqueAdd<T1, T2>(this Dictionary<T1, T2> dic, T1 t1, T2 t2)
  879. {
  880. if (dic.ContainsKey(t1))
  881. {
  882. return false;
  883. }
  884. else
  885. {
  886. dic.Add(t1, t2);
  887. return true;
  888. }
  889. }
  890. #endregion
  891. #region UnityAction
  892. public static void SafeInvoke(this UnityAction action)
  893. {
  894. if (action != null)
  895. {
  896. action.Invoke();
  897. }
  898. }
  899. public static void SafeInvoke<T>(this UnityAction<T> action, T t)
  900. {
  901. if (action != null)
  902. {
  903. action.Invoke(t);
  904. }
  905. }
  906. #endregion
  907. #region AddComponent
  908. public static T AddComponent<T>(this Component comp) where T : Component
  909. {
  910. return comp.transform.gameObject.AddComponent<T>();
  911. }
  912. #endregion
  913. }