Extension.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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 static class Extension
  10. {
  11. #region List
  12. public static T Last<T>(this List<T> list, int index)
  13. {
  14. return list[list.Count - 1 - index];
  15. }
  16. public static T Prev<T>(this List<T> list, int index)
  17. {
  18. return list[(index + list.Count - 1)%list.Count];
  19. }
  20. public static T Next<T>(this List<T> list, int index)
  21. {
  22. return list[(index + 1)%list.Count];
  23. }
  24. public static T Random<T>(this List<T> list)
  25. {
  26. if (list.Count == 0)
  27. {
  28. Debug.Log("Count is 0");
  29. }
  30. return list[UnityEngine.Random.Range(0, list.Count)];
  31. }
  32. public static bool Valid<T>(this List<T> list)
  33. {
  34. if (list == null || list.Count == 0)
  35. {
  36. return false;
  37. }
  38. else
  39. {
  40. return true;
  41. }
  42. }
  43. public static bool UniqueAdd<T>(this List<T> list, T obj)
  44. {
  45. if (list.Contains(obj) == false)
  46. {
  47. list.Add(obj);
  48. return true;
  49. }
  50. else
  51. {
  52. return false;
  53. }
  54. }
  55. #endregion
  56. #region Move
  57. public static Shake Shake(this Component comp, float duration, int repeat, Vector3 strength, Curve curve)
  58. {
  59. return ManaAnim.Shake(comp.transform, duration, repeat, strength, curve);
  60. }
  61. public static Move2D Move2D(this Component comp, Vector3 destination, float duration, bool local, Curve curve)
  62. {
  63. return ManaAnim.Move2D(comp.transform, destination, duration, local, curve);
  64. }
  65. public static Move3D Move3D(this Component comp, Vector3 destination, float duration, bool local, Curve curve)
  66. {
  67. return ManaAnim.Move3D(comp.transform, destination, duration, local, curve);
  68. }
  69. public static Zoom2D Zoom2D(this Transform target, float origin, float destination, float duration, float stay, Transform targetZoom, Curve curve)
  70. {
  71. return ManaAnim.Zoom2D(target, origin, destination, duration, stay, targetZoom, curve);
  72. }
  73. public static Zoom2D Zoom2D(this Transform target, float destination, float duration, float stay, Transform targetZoom, Curve curve)
  74. {
  75. return ManaAnim.Zoom2D(target, destination, duration, stay, targetZoom, curve);
  76. }
  77. public static Shake GetShake(this Component comp)
  78. {
  79. return ManaAnim.GetShake(comp.transform);
  80. }
  81. public static Move2D GetMove2D(this Component comp)
  82. {
  83. return ManaAnim.GetMove2D(comp.transform);
  84. }
  85. public static Move3D GetMove3D(this Component comp)
  86. {
  87. return ManaAnim.GetMove3D(comp.transform);
  88. }
  89. public static Zoom2D GetZoom2D(this Component comp)
  90. {
  91. return ManaAnim.GetZoom2D(comp.transform);
  92. }
  93. public static Shake CreateShake(this Component comp)
  94. {
  95. return ManaAnim.CreateShake(comp.transform);
  96. }
  97. public static Move2D CreateMove2D(this Component comp)
  98. {
  99. return ManaAnim.CreateMove2D(comp.transform);
  100. }
  101. public static Move3D CreateMove3D(this Component comp)
  102. {
  103. return ManaAnim.CreateMove3D(comp.transform);
  104. }
  105. public static Zoom2D CreateZoom2D(this Component comp)
  106. {
  107. return ManaAnim.CreateZoom2D(comp.transform);
  108. }
  109. #endregion
  110. #region Alpha
  111. public static void SetAlpha(this Material material, float alpha, string propertyName)
  112. {
  113. Color color = material.GetColor(propertyName);
  114. color.a = alpha;
  115. material.SetColor(propertyName, color);
  116. }
  117. public static void SetAlpha(this Graphic graphic, float alpha)
  118. {
  119. graphic.color = new Color(graphic.color.r, graphic.color.g, graphic.color.b, alpha);
  120. }
  121. public static void SetAlpha(this TextMesh textMesh, float alpha)
  122. {
  123. textMesh.color = new Color(textMesh.color.r, textMesh.color.g, textMesh.color.b, alpha);
  124. }
  125. public static void SetAlpha(this SpriteRenderer sR, float alpha)
  126. {
  127. sR.color = new Color(sR.color.r, sR.color.g, sR.color.b, alpha);
  128. }
  129. #endregion
  130. #region Sprite
  131. public static Sprite GetSprite(this Component comp)
  132. {
  133. return comp.GetComponent<Image>().sprite;
  134. }
  135. #endregion
  136. #region String
  137. public static string Replace(this string str, int startIndex, int endIndex, string newStr)
  138. {
  139. str = str.Remove(startIndex, endIndex - startIndex + 1);
  140. str = str.Insert(startIndex, newStr);
  141. return str;
  142. }
  143. public static string Between(this string str, int startIndex, int endIndex)
  144. {
  145. if (startIndex > endIndex)
  146. {
  147. return "";
  148. }
  149. else if (startIndex == endIndex)
  150. {
  151. return str[startIndex].ToString();
  152. }
  153. else
  154. {
  155. return str.Substring(startIndex, endIndex - startIndex + 1);
  156. }
  157. }
  158. #endregion
  159. #region Regist
  160. public static T AddScript<T>(this Component comp) where T : Component
  161. {
  162. return AddScript<T>(comp.gameObject);
  163. }
  164. public static T AddScript<T>(this GameObject go) where T : Component
  165. {
  166. Component comp = go.AddComponent(typeof(T));
  167. if (comp is Regist)
  168. {
  169. Regist regist = (Regist) comp;
  170. regist.enabled = false;
  171. regist.RegistImmed();
  172. Initializer.RegistList.Add(regist);
  173. return (T) comp;
  174. }
  175. else
  176. {
  177. throw new Exception();
  178. }
  179. }
  180. #endregion
  181. #region Active
  182. public static void SetActive(this Component comp, bool active)
  183. {
  184. comp.gameObject.SetActive(active);
  185. }
  186. #endregion
  187. #region Tween
  188. public static TweenSr TweenForSr(this Component comp)
  189. {
  190. return ManaAnim.TweenForSr(comp.transform);
  191. }
  192. public static TweenCG TweenForCG(this Component comp)
  193. {
  194. return ManaAnim.TweenForCG(comp.transform);
  195. }
  196. public static TweenVec TweenForVec(this Component comp)
  197. {
  198. return ManaAnim.TweenForVec(comp.transform);
  199. }
  200. public static TweenGra TweenForGra(this Component comp)
  201. {
  202. return ManaAnim.TweenForGra(comp.transform);
  203. }
  204. public static TweenText TweenForText(this Component comp)
  205. {
  206. return ManaAnim.TweenForText(comp.transform);
  207. }
  208. public static TweenRect TweenForRect(this Component comp)
  209. {
  210. return ManaAnim.TweenForRect(comp.transform);
  211. }
  212. public static TweenScale TweenForScale(this Component comp)
  213. {
  214. return ManaAnim.TweenForScale(comp.transform);
  215. }
  216. public static TweenAudio TweenForAudio(this Component comp)
  217. {
  218. return ManaAnim.TweenForAudio(comp.transform);
  219. }
  220. public static TweenNumber TweenForNumber(this Component comp)
  221. {
  222. return ManaAnim.TweenForNumber(comp.transform);
  223. }
  224. public static TweenSr TweenBacSr(this Component comp)
  225. {
  226. return ManaAnim.TweenBacSr(comp.transform);
  227. }
  228. public static TweenCG TweenBacCG(this Component comp)
  229. {
  230. return ManaAnim.TweenBacCG(comp.transform);
  231. }
  232. public static TweenVec TweenBacVec(this Component comp)
  233. {
  234. return ManaAnim.TweenBacVec(comp.transform);
  235. }
  236. public static TweenGra TweenBacGra(this Component comp)
  237. {
  238. return ManaAnim.TweenBacGra(comp.transform);
  239. }
  240. public static TweenText TweenBacText(this Component comp)
  241. {
  242. return ManaAnim.TweenBacText(comp.transform);
  243. }
  244. public static TweenRect TweenBacRect(this Component comp)
  245. {
  246. return ManaAnim.TweenBacRect(comp.transform);
  247. }
  248. public static TweenScale TweenBacScale(this Component comp)
  249. {
  250. return ManaAnim.TweenBacScale(comp.transform);
  251. }
  252. public static TweenAudio TweenBacAudio(this Component comp)
  253. {
  254. return ManaAnim.TweenBacAudio(comp.transform);
  255. }
  256. public static TweenNumber TweenBacNumber(this Component comp)
  257. {
  258. return ManaAnim.TweenBacNumber(comp.transform);
  259. }
  260. public static TweenSr TweenConForSr(this Component comp)
  261. {
  262. return ManaAnim.TweenConForSr(comp.transform);
  263. }
  264. public static TweenCG TweenConForCG(this Component comp)
  265. {
  266. return ManaAnim.TweenConForCG(comp.transform);
  267. }
  268. public static TweenVec TweenConForVec(this Component comp)
  269. {
  270. return ManaAnim.TweenConForVec(comp.transform);
  271. }
  272. public static TweenGra TweenConForGra(this Component comp)
  273. {
  274. return ManaAnim.TweenConForGra(comp.transform);
  275. }
  276. public static TweenText TweenConForText(this Component comp)
  277. {
  278. return ManaAnim.TweenConForText(comp.transform);
  279. }
  280. public static TweenRect TweenConForRect(this Component comp)
  281. {
  282. return ManaAnim.TweenConForRect(comp.transform);
  283. }
  284. public static TweenScale TweenConForScale(this Component comp)
  285. {
  286. return ManaAnim.TweenConForScale(comp.transform);
  287. }
  288. public static TweenAudio TweenConForAudio(this Component comp)
  289. {
  290. return ManaAnim.TweenConForAudio(comp.transform);
  291. }
  292. public static TweenNumber TweenConForNumber(this Component comp)
  293. {
  294. return ManaAnim.TweenConForNumber(comp.transform);
  295. }
  296. public static TweenSr TweenConBacSr(this Component comp)
  297. {
  298. return ManaAnim.TweenConBacSr(comp.transform);
  299. }
  300. public static TweenCG TweenConBacCG(this Component comp)
  301. {
  302. return ManaAnim.TweenConBacCG(comp.transform);
  303. }
  304. public static TweenVec TweenConBacVec(this Component comp)
  305. {
  306. return ManaAnim.TweenConBacVec(comp.transform);
  307. }
  308. public static TweenGra TweenConBacGra(this Component comp)
  309. {
  310. return ManaAnim.TweenConBacGra(comp.transform);
  311. }
  312. public static TweenText TweenConBacText(this Component comp)
  313. {
  314. return ManaAnim.TweenConBacText(comp.transform);
  315. }
  316. public static TweenRect TweenConBacRect(this Component comp)
  317. {
  318. return ManaAnim.TweenConBacRect(comp.transform);
  319. }
  320. public static TweenScale TweenConBacScale(this Component comp)
  321. {
  322. return ManaAnim.TweenConBacScale(comp.transform);
  323. }
  324. public static TweenAudio TweenConBacAudio(this Component comp)
  325. {
  326. return ManaAnim.TweenConBacAudio(comp.transform);
  327. }
  328. public static TweenNumber TweenConBacNumber(this Component comp)
  329. {
  330. return ManaAnim.TweenConBacNumber(comp.transform);
  331. }
  332. public static TweenSr GetTweenSr(this Component comp)
  333. {
  334. return ManaAnim.GetTweenSr(comp.transform);
  335. }
  336. public static TweenCG GetTweenCG(this Component comp)
  337. {
  338. return ManaAnim.GetTweenCG(comp.transform);
  339. }
  340. public static TweenVec GetTweenVec(this Component comp)
  341. {
  342. return ManaAnim.GetTweenVec(comp.transform);
  343. }
  344. public static TweenGra GetTweenGra(this Component comp)
  345. {
  346. return ManaAnim.GetTweenGra(comp.transform);
  347. }
  348. public static TweenText GetTweenText(this Component comp)
  349. {
  350. return ManaAnim.GetTweenText(comp.transform);
  351. }
  352. public static TweenRect GetTweenRect(this Component comp)
  353. {
  354. return ManaAnim.GetTweenRect(comp.transform);
  355. }
  356. public static TweenScale GetTweenScale(this Component comp)
  357. {
  358. return ManaAnim.GetTweenScale(comp.transform);
  359. }
  360. public static TweenAudio GetTweenAudio(this Component comp)
  361. {
  362. return ManaAnim.GetTweenAudio(comp.transform);
  363. }
  364. public static TweenNumber GetTweenNumber(this Component comp)
  365. {
  366. return ManaAnim.GetTweenNumber(comp.transform);
  367. }
  368. 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)
  369. {
  370. return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, originActive, destActive, curve, cg, group);
  371. }
  372. public static TweenSr CreateTweenSr(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false)
  373. {
  374. return ManaAnim.CreateTweenSr(comp.transform, destination, duration, originActive, destActive, curve, cg, group);
  375. }
  376. 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)
  377. {
  378. return ManaAnim.CreateTweenSr(comp.transform, origin, destination, duration, originActive, destActive, curve, cg, group);
  379. }
  380. public static TweenSr CreateTweenSr(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false, bool group = false)
  381. {
  382. return ManaAnim.CreateTweenSr(comp.transform, destination, duration, originActive, destActive, curve, cg, group);
  383. }
  384. public static TweenCG CreateTweenCG(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve)
  385. {
  386. return ManaAnim.CreateTweenCG(comp.transform, origin, destination, duration, originActive, destActive, curve);
  387. }
  388. public static TweenCG CreateTweenCG(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve)
  389. {
  390. return ManaAnim.CreateTweenCG(comp.transform, destination, duration, originActive, destActive, curve);
  391. }
  392. public static TweenGra CreateTweenGra(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  393. {
  394. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  395. }
  396. public static TweenGra CreateTweenGra(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  397. {
  398. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve, cg);
  399. }
  400. public static TweenGra CreateTweenGra(this Component comp, Color origin, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  401. {
  402. return ManaAnim.CreateTweenGra(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  403. }
  404. public static TweenGra CreateTweenGra(this Component comp, Color destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  405. {
  406. return ManaAnim.CreateTweenGra(comp.transform, destination, duration, originActive, destActive, curve, cg);
  407. }
  408. public static TweenVec CreateTweenVec2D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  409. {
  410. return ManaAnim.CreateTweenVec2D(comp.transform, origin, destination, duration, local, originActive, destActive, curve, cg);
  411. }
  412. public static TweenVec CreateTweenVec2D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  413. {
  414. return ManaAnim.CreateTweenVec2D(comp.transform, destination, duration, local, originActive, destActive, curve, cg);
  415. }
  416. public static TweenVec CreateTweenVec3D(this Component comp, Vector3 origin, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  417. {
  418. return ManaAnim.CreateTweenVec3D(comp.transform, origin, destination, duration, local, originActive, destActive, curve, cg);
  419. }
  420. public static TweenVec CreateTweenVec3D(this Component comp, Vector3 destination, float duration, bool local, bool originActive, bool destActive, Curve curve, bool cg = false)
  421. {
  422. return ManaAnim.CreateTweenVec3D(comp.transform, destination, duration, local, originActive, destActive, curve, cg);
  423. }
  424. public static TweenText CreateTweenText(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  425. {
  426. return ManaAnim.CreateTweenText(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  427. }
  428. public static TweenText CreateTweenText(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  429. {
  430. return ManaAnim.CreateTweenText(comp.transform, destination, duration, originActive, destActive, curve, cg);
  431. }
  432. public static TweenRect CreateTweenRect(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  433. {
  434. return ManaAnim.CreateTweenRect(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  435. }
  436. public static TweenRect CreateTweenRect(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  437. {
  438. return ManaAnim.CreateTweenRect(comp.transform, destination, duration, originActive, destActive, curve, cg);
  439. }
  440. public static TweenScale CreateTweenScale(this Component comp, Vector3 origin, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  441. {
  442. return ManaAnim.CreateTweenScale(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  443. }
  444. public static TweenScale CreateTweenScale(this Component comp, Vector3 destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  445. {
  446. return ManaAnim.CreateTweenScale(comp.transform, destination, duration, originActive, destActive, curve, cg);
  447. }
  448. public static TweenAudio CreateTweenAudio(this Component comp, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  449. {
  450. return ManaAnim.CreateTweenAudio(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  451. }
  452. public static TweenAudio CreateTweenAudio(this Component comp, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  453. {
  454. return ManaAnim.CreateTweenAudio(comp.transform, destination, duration, originActive, destActive, curve, cg);
  455. }
  456. public static TweenNumber CreateTweenNumber(this Component comp, int origin, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  457. {
  458. return ManaAnim.CreateTweenNumber(comp.transform, origin, destination, duration, originActive, destActive, curve, cg);
  459. }
  460. public static TweenNumber CreateTweenNumber(this Component comp, int destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false)
  461. {
  462. return ManaAnim.CreateTweenNumber(comp.transform, destination, duration, originActive, destActive, curve, cg);
  463. }
  464. #endregion
  465. #region Collider
  466. public static void SetCollider(this Component comp, bool active)
  467. {
  468. BoxCollider2D collider = comp.GetComponent<BoxCollider2D>();
  469. if (collider)
  470. {
  471. collider.enabled = active;
  472. }
  473. }
  474. public static void SetCollider(this GameObject gameObject, bool active)
  475. {
  476. gameObject.GetComponent<BoxCollider2D>().enabled = active;
  477. }
  478. #endregion
  479. #region GetChild
  480. public static Transform GetChild(this Component comp, int index)
  481. {
  482. return comp.transform.GetChild(index);
  483. }
  484. public static Transform GetChild(this GameObject gameObject, int index)
  485. {
  486. return gameObject.transform.GetChild(index);
  487. }
  488. #endregion
  489. #region SetParent
  490. public static void SetParent(this Component comp, Transform par)
  491. {
  492. comp.transform.SetParent(par);
  493. }
  494. public static void SetParent(this GameObject go, Transform par)
  495. {
  496. go.transform.SetParent(par);
  497. }
  498. #endregion
  499. #region Transform
  500. public static void SetX(this Transform tra, float x)
  501. {
  502. tra.position = new Vector3(x, tra.position.y, tra.position.z);
  503. }
  504. public static void SetY(this Transform tra, float y)
  505. {
  506. tra.position = new Vector3(tra.position.x, y, tra.position.z);
  507. }
  508. public static void SetZ(this Transform tra, float z)
  509. {
  510. tra.position = new Vector3(tra.position.x, tra.position.y, z);
  511. }
  512. public static void SetLX(this Transform tra, float x)
  513. {
  514. tra.localPosition = new Vector3(x, tra.localPosition.y, tra.localPosition.z);
  515. }
  516. public static void SetLY(this Transform tra, float y)
  517. {
  518. tra.localPosition = new Vector3(tra.localPosition.x, y, tra.localPosition.z);
  519. }
  520. public static void SetLZ(this Transform tra, float z)
  521. {
  522. tra.localPosition = new Vector3(tra.localPosition.x, tra.localPosition.y, z);
  523. }
  524. public static void SetEX(this Transform tra, float x)
  525. {
  526. tra.eulerAngles = new Vector3(x, tra.eulerAngles.y, tra.eulerAngles.z);
  527. }
  528. public static void SetEY(this Transform tra, float y)
  529. {
  530. tra.eulerAngles = new Vector3(tra.eulerAngles.x, y, tra.eulerAngles.z);
  531. }
  532. public static void SetEZ(this Transform tra, float z)
  533. {
  534. tra.eulerAngles = new Vector3(tra.eulerAngles.x, tra.eulerAngles.y, z);
  535. }
  536. public static Vector3 GetScale(this Transform tra)
  537. {
  538. Vector3 scale = tra.localScale;
  539. while (tra.parent != null)
  540. {
  541. float x = scale.x * tra.parent.localScale.x;
  542. float y = scale.y * tra.parent.localScale.y;
  543. float z = scale.z * tra.parent.localScale.z;
  544. scale = new Vector3(x, y, z);
  545. tra = tra.parent;
  546. }
  547. return scale;
  548. }
  549. #endregion
  550. #region Dictionary
  551. public static T2 Random<T1, T2>(this Dictionary<T1, T2> dic)
  552. {
  553. return dic.Values.ToList().Random();
  554. }
  555. #endregion
  556. #region UnityAction
  557. public static void SafeInvoke(this UnityAction action)
  558. {
  559. if (action != null)
  560. {
  561. action.Invoke();
  562. }
  563. }
  564. public static void SafeInvoke<T>(this UnityAction<T> action, T t)
  565. {
  566. if (action != null)
  567. {
  568. action.Invoke(t);
  569. }
  570. }
  571. #endregion
  572. #region AddComponent
  573. public static T AddComponent<T>(this Component comp) where T : Component
  574. {
  575. return comp.transform.gameObject.AddComponent<T>();
  576. }
  577. #endregion
  578. }