Extension.cs 37 KB

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