Extension.cs 37 KB

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