Extension.cs 37 KB

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