ExtensionKit.cs 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945
  1. /****************************************************************************
  2. * Copyright (c) 2017 ~ 2022 liangxiegame UNDER MIT LICENSE
  3. *
  4. * https://qframework.cn
  5. * https://github.com/liangxiegame/QFramework
  6. * https://gitee.com/liangxiegame/QFramework
  7. ****************************************************************************/
  8. namespace QFramework
  9. {
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.IO;
  14. using System.Text.RegularExpressions;
  15. using System.Reflection;
  16. using System.Text;
  17. using UnityEngine;
  18. using UnityEngine.Events;
  19. using UnityEngine.UI;
  20. public static class DelegateExtension
  21. {
  22. #region Func Extension
  23. /// <summary>
  24. /// 功能:不为空则调用 Func
  25. ///
  26. /// 示例:
  27. /// <code>
  28. /// Func<int> func = ()=> 1;
  29. /// var number = func.InvokeGracefully(); // 等价于 if (func != null) number = func();
  30. /// </code>
  31. /// </summary>
  32. /// <param name="selfFunc"></param>
  33. /// <typeparam name="T"></typeparam>
  34. /// <returns></returns>
  35. public static T InvokeGracefully<T>(this Func<T> selfFunc)
  36. {
  37. return null != selfFunc ? selfFunc() : default(T);
  38. }
  39. #endregion
  40. #region Action
  41. /// <summary>
  42. /// 功能:不为空则调用 Action
  43. ///
  44. /// 示例:
  45. /// <code>
  46. /// System.Action action = () => Log.I("action called");
  47. /// action.InvokeGracefully(); // if (action != null) action();
  48. /// </code>
  49. /// </summary>
  50. /// <param name="selfAction"> action 对象 </param>
  51. /// <returns> 是否调用成功 </returns>
  52. public static bool InvokeGracefully(this Action selfAction)
  53. {
  54. if (null != selfAction)
  55. {
  56. selfAction();
  57. return true;
  58. }
  59. return false;
  60. }
  61. /// <summary>
  62. /// 不为空则调用 Action<T>
  63. ///
  64. /// 示例:
  65. /// <code>
  66. /// System.Action<int> action = (number) => Log.I("action called" + number);
  67. /// action.InvokeGracefully(10); // if (action != null) action(10);
  68. /// </code>
  69. /// </summary>
  70. /// <param name="selfAction"> action 对象</param>
  71. /// <typeparam name="T">参数</typeparam>
  72. /// <returns> 是否调用成功</returns>
  73. public static bool InvokeGracefully<T>(this Action<T> selfAction, T t)
  74. {
  75. if (null != selfAction)
  76. {
  77. selfAction(t);
  78. return true;
  79. }
  80. return false;
  81. }
  82. /// <summary>
  83. /// 不为空则调用 Action<T,K>
  84. ///
  85. /// 示例
  86. /// <code>
  87. /// System.Action<int,string> action = (number,name) => Log.I("action called" + number + name);
  88. /// action.InvokeGracefully(10,"qframework"); // if (action != null) action(10,"qframework");
  89. /// </code>
  90. /// </summary>
  91. /// <param name="selfAction"></param>
  92. /// <returns> call succeed</returns>
  93. public static bool InvokeGracefully<T, K>(this Action<T, K> selfAction, T t, K k)
  94. {
  95. if (null != selfAction)
  96. {
  97. selfAction(t, k);
  98. return true;
  99. }
  100. return false;
  101. }
  102. /// <summary>
  103. /// 不为空则调用委托
  104. ///
  105. /// 示例:
  106. /// <code>
  107. /// // delegate
  108. /// TestDelegate testDelegate = () => { };
  109. /// testDelegate.InvokeGracefully();
  110. /// </code>
  111. /// </summary>
  112. /// <param name="selfAction"></param>
  113. /// <returns> call suceed </returns>
  114. public static bool InvokeGracefully(this Delegate selfAction, params object[] args)
  115. {
  116. if (null != selfAction)
  117. {
  118. selfAction.DynamicInvoke(args);
  119. return true;
  120. }
  121. return false;
  122. }
  123. #endregion
  124. }
  125. public static class CSharpObjectExtension
  126. {
  127. /// <summary>
  128. /// 是否相等
  129. ///
  130. /// 示例:
  131. /// <code>
  132. /// if (this.Is(player))
  133. /// {
  134. /// ...
  135. /// }
  136. /// </code>
  137. /// </summary>
  138. /// <param name="selfObj"></param>
  139. /// <param name="value"></param>
  140. /// <returns></returns>
  141. public static bool Is(this object selfObj, object value)
  142. {
  143. return selfObj == value;
  144. }
  145. public static bool Is<T>(this T selfObj, Func<T, bool> condition)
  146. {
  147. return condition(selfObj);
  148. }
  149. /// <summary>
  150. /// 表达式成立 则执行 Action
  151. ///
  152. /// 示例:
  153. /// <code>
  154. /// (1 == 1).Do(()=>Debug.Log("1 == 1");
  155. /// </code>
  156. /// </summary>
  157. /// <param name="selfCondition"></param>
  158. /// <param name="action"></param>
  159. /// <returns></returns>
  160. public static bool Do(this bool selfCondition, Action action)
  161. {
  162. if (selfCondition)
  163. {
  164. action();
  165. }
  166. return selfCondition;
  167. }
  168. /// <summary>
  169. /// 不管表达成不成立 都执行 Action,并把结果返回
  170. ///
  171. /// 示例:
  172. /// <code>
  173. /// (1 == 1).Do((result)=>Debug.Log("1 == 1:" + result);
  174. /// </code>
  175. /// </summary>
  176. /// <param name="selfCondition"></param>
  177. /// <param name="action"></param>
  178. /// <returns></returns>
  179. public static bool Do(this bool selfCondition, Action<bool> action)
  180. {
  181. action(selfCondition);
  182. return selfCondition;
  183. }
  184. /// <summary>
  185. /// 功能:判断是否为空
  186. ///
  187. /// 示例:
  188. /// <code>
  189. /// var simpleObject = new object();
  190. ///
  191. /// if (simpleObject.IsNull()) // 等价于 simpleObject == null
  192. /// {
  193. /// // do sth
  194. /// }
  195. /// </code>
  196. /// </summary>
  197. /// <param name="selfObj">判断对象(this)</param>
  198. /// <typeparam name="T">对象的类型(可不填)</typeparam>
  199. /// <returns>是否为空</returns>
  200. public static bool IsNull<T>(this T selfObj) where T : class
  201. {
  202. return null == selfObj;
  203. }
  204. /// <summary>
  205. /// 功能:判断不是为空
  206. /// 示例:
  207. /// <code>
  208. /// var simpleObject = new object();
  209. ///
  210. /// if (simpleObject.IsNotNull()) // 等价于 simpleObject != null
  211. /// {
  212. /// // do sth
  213. /// }
  214. /// </code>
  215. /// </summary>
  216. /// <param name="selfObj">判断对象(this)</param>
  217. /// <typeparam name="T">对象的类型(可不填)</typeparam>
  218. /// <returns>是否不为空</returns>
  219. public static bool IsNotNull<T>(this T selfObj) where T : class
  220. {
  221. return null != selfObj;
  222. }
  223. public static void DoIfNotNull<T>(this T selfObj, Action<T> action) where T : class
  224. {
  225. if (selfObj != null)
  226. {
  227. action(selfObj);
  228. }
  229. }
  230. }
  231. /// <summary>
  232. /// 泛型工具
  233. ///
  234. /// 实例:
  235. /// <code>
  236. /// 示例:
  237. /// var typeName = GenericExtention.GetTypeName<string>();
  238. /// typeName.LogInfo(); // string
  239. /// </code>
  240. /// </summary>
  241. public static class GenericUtil
  242. {
  243. /// <summary>
  244. /// 获取泛型名字
  245. /// <code>
  246. /// var typeName = GenericExtention.GetTypeName<string>();
  247. /// typeName.LogInfo(); // string
  248. /// </code>
  249. /// </summary>
  250. /// <typeparam name="T"></typeparam>
  251. /// <returns></returns>
  252. public static string GetTypeName<T>()
  253. {
  254. return typeof(T).ToString();
  255. }
  256. }
  257. /// <summary>
  258. /// 可枚举的集合扩展(Array、List<T>、Dictionary<K,V>)
  259. /// </summary>
  260. public static class IEnumerableExtension
  261. {
  262. #region Array Extension
  263. /// <summary>
  264. /// 遍历数组
  265. /// <code>
  266. /// var testArray = new[] { 1, 2, 3 };
  267. /// testArray.ForEach(number => number.LogInfo());
  268. /// </code>
  269. /// </summary>
  270. /// <returns>The each.</returns>
  271. /// <param name="selfArray">Self array.</param>
  272. /// <param name="action">Action.</param>
  273. /// <typeparam name="T">The 1st type parameter.</typeparam>
  274. /// <returns> 返回自己 </returns>
  275. public static T[] ForEach<T>(this T[] selfArray, Action<T> action)
  276. {
  277. Array.ForEach(selfArray, action);
  278. return selfArray;
  279. }
  280. /// <summary>
  281. /// 遍历 IEnumerable
  282. /// <code>
  283. /// // IEnumerable<T>
  284. /// IEnumerable<int> testIenumerable = new List<int> { 1, 2, 3 };
  285. /// testIenumerable.ForEach(number => number.LogInfo());
  286. /// // 支持字典的遍历
  287. /// new Dictionary<string, string>()
  288. /// .ForEach(keyValue => Log.I("key:{0},value:{1}", keyValue.Key, keyValue.Value));
  289. /// </code>
  290. /// </summary>
  291. /// <returns>The each.</returns>
  292. /// <param name="selfArray">Self array.</param>
  293. /// <param name="action">Action.</param>
  294. /// <typeparam name="T">The 1st type parameter.</typeparam>
  295. public static IEnumerable<T> ForEach<T>(this IEnumerable<T> selfArray, Action<T> action)
  296. {
  297. if (action == null) throw new ArgumentException();
  298. foreach (var item in selfArray)
  299. {
  300. action(item);
  301. }
  302. return selfArray;
  303. }
  304. #endregion
  305. #region List Extension
  306. /// <summary>
  307. /// 倒序遍历
  308. /// <code>
  309. /// var testList = new List<int> { 1, 2, 3 };
  310. /// testList.ForEachReverse(number => number.LogInfo()); // 3, 2, 1
  311. /// </code>
  312. /// </summary>
  313. /// <returns>返回自己</returns>
  314. /// <param name="selfList">Self list.</param>
  315. /// <param name="action">Action.</param>
  316. /// <typeparam name="T">The 1st type parameter.</typeparam>
  317. public static List<T> ForEachReverse<T>(this List<T> selfList, Action<T> action)
  318. {
  319. if (action == null) throw new ArgumentException();
  320. for (var i = selfList.Count - 1; i >= 0; --i)
  321. action(selfList[i]);
  322. return selfList;
  323. }
  324. /// <summary>
  325. /// 倒序遍历(可获得索引)
  326. /// <code>
  327. /// var testList = new List<int> { 1, 2, 3 };
  328. /// testList.ForEachReverse((number,index)=> number.LogInfo()); // 3, 2, 1
  329. /// </code>
  330. /// </summary>
  331. /// <returns>The each reverse.</returns>
  332. /// <param name="selfList">Self list.</param>
  333. /// <param name="action">Action.</param>
  334. /// <typeparam name="T">The 1st type parameter.</typeparam>
  335. public static List<T> ForEachReverse<T>(this List<T> selfList, Action<T, int> action)
  336. {
  337. if (action == null) throw new ArgumentException();
  338. for (var i = selfList.Count - 1; i >= 0; --i)
  339. action(selfList[i], i);
  340. return selfList;
  341. }
  342. /// <summary>
  343. /// 遍历列表(可获得索引)
  344. /// <code>
  345. /// var testList = new List<int> {1, 2, 3 };
  346. /// testList.Foreach((number,index)=>number.LogInfo()); // 1, 2, 3,
  347. /// </code>
  348. /// </summary>
  349. /// <typeparam name="T">列表类型</typeparam>
  350. /// <param name="list">目标表</param>
  351. /// <param name="action">行为</param>
  352. public static void ForEach<T>(this List<T> list, Action<int, T> action)
  353. {
  354. for (var i = 0; i < list.Count; i++)
  355. {
  356. action(i, list[i]);
  357. }
  358. }
  359. #endregion
  360. #region Dictionary Extension
  361. /// <summary>
  362. /// 合并字典
  363. /// <code>
  364. /// // 示例
  365. /// var dictionary1 = new Dictionary<string, string> { { "1", "2" } };
  366. /// var dictionary2 = new Dictionary<string, string> { { "3", "4" } };
  367. /// var dictionary3 = dictionary1.Merge(dictionary2);
  368. /// dictionary3.ForEach(pair => Log.I("{0}:{1}", pair.Key, pair.Value));
  369. /// </code>
  370. /// </summary>
  371. /// <returns>The merge.</returns>
  372. /// <param name="dictionary">Dictionary.</param>
  373. /// <param name="dictionaries">Dictionaries.</param>
  374. /// <typeparam name="TKey">The 1st type parameter.</typeparam>
  375. /// <typeparam name="TValue">The 2nd type parameter.</typeparam>
  376. public static Dictionary<TKey, TValue> Merge<TKey, TValue>(this Dictionary<TKey, TValue> dictionary,
  377. params Dictionary<TKey, TValue>[] dictionaries)
  378. {
  379. return dictionaries.Aggregate(dictionary,
  380. (current, dict) => current.Union(dict).ToDictionary(kv => kv.Key, kv => kv.Value));
  381. }
  382. /// <summary>
  383. /// 遍历字典
  384. /// <code>
  385. /// var dict = new Dictionary<string,string> {{"name","liangxie},{"age","18"}};
  386. /// dict.ForEach((key,value)=> Log.I("{0}:{1}",key,value);// name:liangxie age:18
  387. /// </code>
  388. /// </summary>
  389. /// <typeparam name="K"></typeparam>
  390. /// <typeparam name="V"></typeparam>
  391. /// <param name="dict"></param>
  392. /// <param name="action"></param>
  393. public static void ForEach<K, V>(this Dictionary<K, V> dict, Action<K, V> action)
  394. {
  395. var dictE = dict.GetEnumerator();
  396. while (dictE.MoveNext())
  397. {
  398. var current = dictE.Current;
  399. action(current.Key, current.Value);
  400. }
  401. dictE.Dispose();
  402. }
  403. /// <summary>
  404. /// 字典添加新的词典
  405. /// </summary>
  406. /// <typeparam name="K"></typeparam>
  407. /// <typeparam name="V"></typeparam>
  408. /// <param name="dict"></param>
  409. /// <param name="addInDict"></param>
  410. /// <param name="isOverride"></param>
  411. public static void AddRange<K, V>(this Dictionary<K, V> dict, Dictionary<K, V> addInDict,
  412. bool isOverride = false)
  413. {
  414. var dictE = addInDict.GetEnumerator();
  415. while (dictE.MoveNext())
  416. {
  417. var current = dictE.Current;
  418. if (dict.ContainsKey(current.Key))
  419. {
  420. if (isOverride)
  421. dict[current.Key] = current.Value;
  422. continue;
  423. }
  424. dict.Add(current.Key, current.Value);
  425. }
  426. dictE.Dispose();
  427. }
  428. #endregion
  429. }
  430. /// <summary>
  431. /// 对 System.IO 的一些扩展
  432. /// </summary>
  433. public static class IOExtension
  434. {
  435. /// <summary>
  436. /// 检测路径是否存在,如果不存在则创建
  437. /// </summary>
  438. /// <param name="path"></param>
  439. public static string CreateDirIfNotExists4FilePath(this string path)
  440. {
  441. var direct = Path.GetDirectoryName(path);
  442. if (!Directory.Exists(direct))
  443. {
  444. Directory.CreateDirectory(direct);
  445. }
  446. return path;
  447. }
  448. /// <summary>
  449. /// 创建新的文件夹,如果存在则不创建
  450. /// <code>
  451. /// var testDir = "Assets/TestFolder";
  452. /// testDir.CreateDirIfNotExists();
  453. /// // 结果为,在 Assets 目录下创建 TestFolder
  454. /// </code>
  455. /// </summary>
  456. public static string CreateDirIfNotExists(this string dirFullPath)
  457. {
  458. if (!Directory.Exists(dirFullPath))
  459. {
  460. Directory.CreateDirectory(dirFullPath);
  461. }
  462. return dirFullPath;
  463. }
  464. /// <summary>
  465. /// 删除文件夹,如果存在
  466. /// <code>
  467. /// var testDir = "Assets/TestFolder";
  468. /// testDir.DeleteDirIfExists();
  469. /// // 结果为,在 Assets 目录下删除了 TestFolder
  470. /// </code>
  471. /// </summary>
  472. public static void DeleteDirIfExists(this string dirFullPath)
  473. {
  474. if (Directory.Exists(dirFullPath))
  475. {
  476. Directory.Delete(dirFullPath, true);
  477. }
  478. }
  479. /// <summary>
  480. /// 清空 Dir(保留目录),如果存在。
  481. /// <code>
  482. /// var testDir = "Assets/TestFolder";
  483. /// testDir.EmptyDirIfExists();
  484. /// // 结果为,清空了 TestFolder 里的内容
  485. /// </code>
  486. /// </summary>
  487. public static void EmptyDirIfExists(this string dirFullPath)
  488. {
  489. if (Directory.Exists(dirFullPath))
  490. {
  491. Directory.Delete(dirFullPath, true);
  492. }
  493. Directory.CreateDirectory(dirFullPath);
  494. }
  495. /// <summary>
  496. /// 删除文件 如果存在
  497. /// <code>
  498. /// // 示例
  499. /// var filePath = "Assets/Test.txt";
  500. /// File.Create("Assets/Test);
  501. /// filePath.DeleteFileIfExists();
  502. /// // 结果为,删除了 Test.txt
  503. /// </code>
  504. /// </summary>
  505. /// <param name="fileFullPath"></param>
  506. /// <returns> 是否进行了删除操作 </returns>
  507. public static bool DeleteFileIfExists(this string fileFullPath)
  508. {
  509. if (File.Exists(fileFullPath))
  510. {
  511. File.Delete(fileFullPath);
  512. return true;
  513. }
  514. return false;
  515. }
  516. /// <summary>
  517. /// 合并路径
  518. /// <code>
  519. /// // 示例:
  520. /// Application.dataPath.CombinePath("Resources").LogInfo(); // /projectPath/Assets/Resources
  521. /// </code>
  522. /// </summary>
  523. /// <param name="selfPath"></param>
  524. /// <param name="toCombinePath"></param>
  525. /// <returns> 合并后的路径 </returns>
  526. public static string CombinePath(this string selfPath, string toCombinePath)
  527. {
  528. return Path.Combine(selfPath, toCombinePath);
  529. }
  530. #region 未经过测试
  531. /// <summary>
  532. /// 打开文件夹
  533. /// </summary>
  534. /// <param name="path"></param>
  535. public static void OpenFolder(string path)
  536. {
  537. #if UNITY_STANDALONE_OSX
  538. System.Diagnostics.Process.Start("open", path);
  539. #elif UNITY_STANDALONE_WIN
  540. System.Diagnostics.Process.Start("explorer.exe", path);
  541. #endif
  542. }
  543. /// <summary>
  544. /// 获取文件夹名
  545. /// </summary>
  546. /// <param name="fileName"></param>
  547. /// <returns></returns>
  548. public static string GetDirectoryName(string fileName)
  549. {
  550. fileName = MakePathStandard(fileName);
  551. return fileName.Substring(0, fileName.LastIndexOf('/'));
  552. }
  553. /// <summary>
  554. /// 获取文件名
  555. /// </summary>
  556. /// <param name="path"></param>
  557. /// <param name="separator"></param>
  558. /// <returns></returns>
  559. public static string GetFileName(string path, char separator = '/')
  560. {
  561. path = IOExtension.MakePathStandard(path);
  562. return path.Substring(path.LastIndexOf(separator) + 1);
  563. }
  564. /// <summary>
  565. /// 获取不带后缀的文件名
  566. /// </summary>
  567. /// <param name="fileName"></param>
  568. /// <param name="separator"></param>
  569. /// <returns></returns>
  570. public static string GetFileNameWithoutExtention(string fileName, char separator = '/')
  571. {
  572. return GetFilePathWithoutExtention(GetFileName(fileName, separator));
  573. }
  574. /// <summary>
  575. /// 获取不带后缀的文件路径
  576. /// </summary>
  577. /// <param name="fileName"></param>
  578. /// <returns></returns>
  579. public static string GetFilePathWithoutExtention(string fileName)
  580. {
  581. if (fileName.Contains("."))
  582. return fileName.Substring(0, fileName.LastIndexOf('.'));
  583. return fileName;
  584. }
  585. /// <summary>
  586. /// 使目录存在,Path可以是目录名必须是文件名
  587. /// </summary>
  588. /// <param name="path"></param>
  589. public static void MakeFileDirectoryExist(string path)
  590. {
  591. string root = Path.GetDirectoryName(path);
  592. if (!Directory.Exists(root))
  593. {
  594. Directory.CreateDirectory(root);
  595. }
  596. }
  597. /// <summary>
  598. /// 使目录存在
  599. /// </summary>
  600. /// <param name="path"></param>
  601. public static void MakeDirectoryExist(string path)
  602. {
  603. if (!Directory.Exists(path))
  604. {
  605. Directory.CreateDirectory(path);
  606. }
  607. }
  608. /// <summary>
  609. /// 获取父文件夹
  610. /// </summary>
  611. /// <param name="path"></param>
  612. /// <returns></returns>
  613. public static string GetPathParentFolder(this string path)
  614. {
  615. if (string.IsNullOrEmpty(path))
  616. {
  617. return string.Empty;
  618. }
  619. return Path.GetDirectoryName(path);
  620. }
  621. /// <summary>
  622. /// 使路径标准化,去除空格并将所有'\'转换为'/'
  623. /// </summary>
  624. /// <param name="path"></param>
  625. /// <returns></returns>
  626. public static string MakePathStandard(string path)
  627. {
  628. return path.Trim().Replace("\\", "/");
  629. }
  630. public static List<string> GetDirSubFilePathList(this string dirABSPath, bool isRecursive = true,
  631. string suffix = "")
  632. {
  633. var pathList = new List<string>();
  634. var di = new DirectoryInfo(dirABSPath);
  635. if (!di.Exists)
  636. {
  637. return pathList;
  638. }
  639. var files = di.GetFiles();
  640. foreach (var fi in files)
  641. {
  642. if (!string.IsNullOrEmpty(suffix))
  643. {
  644. if (!fi.FullName.EndsWith(suffix, System.StringComparison.CurrentCultureIgnoreCase))
  645. {
  646. continue;
  647. }
  648. }
  649. pathList.Add(fi.FullName);
  650. }
  651. if (isRecursive)
  652. {
  653. var dirs = di.GetDirectories();
  654. foreach (var d in dirs)
  655. {
  656. pathList.AddRange(GetDirSubFilePathList(d.FullName, isRecursive, suffix));
  657. }
  658. }
  659. return pathList;
  660. }
  661. public static List<string> GetDirSubDirNameList(this string dirABSPath)
  662. {
  663. var di = new DirectoryInfo(dirABSPath);
  664. var dirs = di.GetDirectories();
  665. return dirs.Select(d => d.Name).ToList();
  666. }
  667. public static string GetFileName(this string absOrAssetsPath)
  668. {
  669. var name = absOrAssetsPath.Replace("\\", "/");
  670. var lastIndex = name.LastIndexOf("/");
  671. return lastIndex >= 0 ? name.Substring(lastIndex + 1) : name;
  672. }
  673. public static string GetFileNameWithoutExtend(this string absOrAssetsPath)
  674. {
  675. var fileName = GetFileName(absOrAssetsPath);
  676. var lastIndex = fileName.LastIndexOf(".");
  677. return lastIndex >= 0 ? fileName.Substring(0, lastIndex) : fileName;
  678. }
  679. public static string GetFileExtendName(this string absOrAssetsPath)
  680. {
  681. var lastIndex = absOrAssetsPath.LastIndexOf(".");
  682. if (lastIndex >= 0)
  683. {
  684. return absOrAssetsPath.Substring(lastIndex);
  685. }
  686. return string.Empty;
  687. }
  688. public static string GetDirPath(this string absOrAssetsPath)
  689. {
  690. var name = absOrAssetsPath.Replace("\\", "/");
  691. var lastIndex = name.LastIndexOf("/");
  692. return name.Substring(0, lastIndex + 1);
  693. }
  694. public static string GetLastDirName(this string absOrAssetsPath)
  695. {
  696. var name = absOrAssetsPath.Replace("\\", "/");
  697. var dirs = name.Split('/');
  698. return absOrAssetsPath.EndsWith("/") ? dirs[dirs.Length - 2] : dirs[dirs.Length - 1];
  699. }
  700. #endregion
  701. }
  702. /// <summary>
  703. /// 简单的概率计算
  704. /// </summary>
  705. public static class ProbilityHelper
  706. {
  707. public static T RandomValueFrom<T>(params T[] values)
  708. {
  709. return values[UnityEngine.Random.Range(0, values.Length)];
  710. }
  711. /// <summary>
  712. /// percent probability
  713. /// </summary>
  714. /// <param name="percent"> 0 ~ 100 </param>
  715. /// <returns></returns>
  716. public static bool PercentProbability(int percent)
  717. {
  718. return UnityEngine.Random.Range(0, 1000) * 0.001f < 50 * 0.01f;
  719. }
  720. }
  721. /// <summary>
  722. /// 面向对象扩展(继承、封装、多态)
  723. /// </summary>
  724. public static class OOPExtension
  725. {
  726. interface ExampleInterface
  727. {
  728. }
  729. public static void Example()
  730. {
  731. if (typeof(OOPExtension).ImplementsInterface<ExampleInterface>())
  732. {
  733. }
  734. if (new object().ImplementsInterface<ExampleInterface>())
  735. {
  736. }
  737. }
  738. /// <summary>
  739. /// Determines whether the type implements the specified interface
  740. /// and is not an interface itself.
  741. /// </summary>
  742. /// <returns><c>true</c>, if interface was implementsed, <c>false</c> otherwise.</returns>
  743. /// <param name="type">Type.</param>
  744. /// <typeparam name="T">The 1st type parameter.</typeparam>
  745. public static bool ImplementsInterface<T>(this Type type)
  746. {
  747. return !type.IsInterface && type.GetInterfaces().Contains(typeof(T));
  748. }
  749. /// <summary>
  750. /// Determines whether the type implements the specified interface
  751. /// and is not an interface itself.
  752. /// </summary>
  753. /// <returns><c>true</c>, if interface was implementsed, <c>false</c> otherwise.</returns>
  754. /// <param name="type">Type.</param>
  755. /// <typeparam name="T">The 1st type parameter.</typeparam>
  756. public static bool ImplementsInterface<T>(this object obj)
  757. {
  758. var type = obj.GetType();
  759. return !type.IsInterface && type.GetInterfaces().Contains(typeof(T));
  760. }
  761. }
  762. /// <summary>
  763. /// 程序集工具
  764. /// </summary>
  765. public class AssemblyUtil
  766. {
  767. /// <summary>
  768. /// 获取 Assembly-CSharp 程序集
  769. /// </summary>
  770. public static Assembly DefaultCSharpAssembly
  771. {
  772. get
  773. {
  774. return AppDomain.CurrentDomain.GetAssemblies()
  775. .SingleOrDefault(a => a.GetName().Name == "Assembly-CSharp");
  776. }
  777. }
  778. /// <summary>
  779. /// 获取默认的程序集中的类型
  780. /// </summary>
  781. /// <param name="typeName"></param>
  782. /// <returns></returns>
  783. public static Type GetDefaultAssemblyType(string typeName)
  784. {
  785. return DefaultCSharpAssembly.GetType(typeName);
  786. }
  787. }
  788. /// <summary>
  789. /// 反射扩展
  790. /// </summary>
  791. public static class ReflectionExtension
  792. {
  793. public static void Example()
  794. {
  795. // var selfType = ReflectionExtension.GetAssemblyCSharp().GetType("QFramework.ReflectionExtension");
  796. // selfType.LogInfo();
  797. }
  798. public static Assembly GetAssemblyCSharp()
  799. {
  800. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  801. foreach (var a in assemblies)
  802. {
  803. if (a.FullName.StartsWith("Assembly-CSharp,"))
  804. {
  805. return a;
  806. }
  807. }
  808. // Log.E(">>>>>>>Error: Can\'t find Assembly-CSharp.dll");
  809. return null;
  810. }
  811. public static Assembly GetAssemblyCSharpEditor()
  812. {
  813. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  814. foreach (var a in assemblies)
  815. {
  816. if (a.FullName.StartsWith("Assembly-CSharp-Editor,"))
  817. {
  818. return a;
  819. }
  820. }
  821. // Log.E(">>>>>>>Error: Can\'t find Assembly-CSharp-Editor.dll");
  822. return null;
  823. }
  824. /// <summary>
  825. /// 通过反射方式调用函数
  826. /// </summary>
  827. /// <param name="obj"></param>
  828. /// <param name="methodName">方法名</param>
  829. /// <param name="args">参数</param>
  830. /// <returns></returns>
  831. public static object InvokeByReflect(this object obj, string methodName, params object[] args)
  832. {
  833. var methodInfo = obj.GetType().GetMethod(methodName);
  834. return methodInfo == null ? null : methodInfo.Invoke(obj, args);
  835. }
  836. /// <summary>
  837. /// 通过反射方式获取域值
  838. /// </summary>
  839. /// <param name="obj"></param>
  840. /// <param name="fieldName">域名</param>
  841. /// <returns></returns>
  842. public static object GetFieldByReflect(this object obj, string fieldName)
  843. {
  844. var fieldInfo = obj.GetType().GetField(fieldName);
  845. return fieldInfo == null ? null : fieldInfo.GetValue(obj);
  846. }
  847. /// <summary>
  848. /// 通过反射方式获取属性
  849. /// </summary>
  850. /// <param name="obj"></param>
  851. /// <param name="fieldName">属性名</param>
  852. /// <returns></returns>
  853. public static object GetPropertyByReflect(this object obj, string propertyName, object[] index = null)
  854. {
  855. var propertyInfo = obj.GetType().GetProperty(propertyName);
  856. return propertyInfo == null ? null : propertyInfo.GetValue(obj, index);
  857. }
  858. /// <summary>
  859. /// 拥有特性
  860. /// </summary>
  861. /// <returns></returns>
  862. public static bool HasAttribute(this PropertyInfo prop, Type attributeType, bool inherit)
  863. {
  864. return prop.GetCustomAttributes(attributeType, inherit).Length > 0;
  865. }
  866. /// <summary>
  867. /// 拥有特性
  868. /// </summary>
  869. /// <returns></returns>
  870. public static bool HasAttribute(this FieldInfo field, Type attributeType, bool inherit)
  871. {
  872. return field.GetCustomAttributes(attributeType, inherit).Length > 0;
  873. }
  874. /// <summary>
  875. /// 拥有特性
  876. /// </summary>
  877. /// <returns></returns>
  878. public static bool HasAttribute(this Type type, Type attributeType, bool inherit)
  879. {
  880. return type.GetCustomAttributes(attributeType, inherit).Length > 0;
  881. }
  882. /// <summary>
  883. /// 拥有特性
  884. /// </summary>
  885. /// <returns></returns>
  886. public static bool HasAttribute(this MethodInfo method, Type attributeType, bool inherit)
  887. {
  888. return method.GetCustomAttributes(attributeType, inherit).Length > 0;
  889. }
  890. /// <summary>
  891. /// 获取第一个特性
  892. /// </summary>
  893. public static T GetFirstAttribute<T>(this MethodInfo method, bool inherit) where T : Attribute
  894. {
  895. var attrs = (T[])method.GetCustomAttributes(typeof(T), inherit);
  896. if (attrs != null && attrs.Length > 0)
  897. return attrs[0];
  898. return null;
  899. }
  900. /// <summary>
  901. /// 获取第一个特性
  902. /// </summary>
  903. public static T GetFirstAttribute<T>(this FieldInfo field, bool inherit) where T : Attribute
  904. {
  905. var attrs = (T[])field.GetCustomAttributes(typeof(T), inherit);
  906. if (attrs != null && attrs.Length > 0)
  907. return attrs[0];
  908. return null;
  909. }
  910. /// <summary>
  911. /// 获取第一个特性
  912. /// </summary>
  913. public static T GetFirstAttribute<T>(this PropertyInfo prop, bool inherit) where T : Attribute
  914. {
  915. var attrs = (T[])prop.GetCustomAttributes(typeof(T), inherit);
  916. if (attrs != null && attrs.Length > 0)
  917. return attrs[0];
  918. return null;
  919. }
  920. /// <summary>
  921. /// 获取第一个特性
  922. /// </summary>
  923. public static T GetFirstAttribute<T>(this Type type, bool inherit) where T : Attribute
  924. {
  925. var attrs = (T[])type.GetCustomAttributes(typeof(T), inherit);
  926. if (attrs != null && attrs.Length > 0)
  927. return attrs[0];
  928. return null;
  929. }
  930. }
  931. /// <summary>
  932. /// 类型扩展
  933. /// </summary>
  934. public static class TypeEx
  935. {
  936. /// <summary>
  937. /// 获取默认值
  938. /// </summary>
  939. /// <param name="targetType"></param>
  940. /// <returns></returns>
  941. public static object DefaultForType(this Type targetType)
  942. {
  943. return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
  944. }
  945. }
  946. /// <summary>
  947. /// 字符串扩展
  948. /// </summary>
  949. public static class StringExtention
  950. {
  951. public static void Example()
  952. {
  953. var emptyStr = string.Empty;
  954. emptyStr.IsNotNullAndEmpty();
  955. emptyStr.IsNullOrEmpty();
  956. emptyStr = emptyStr.Append("appended").Append("1").ToString();
  957. emptyStr.IsNullOrEmpty();
  958. }
  959. /// <summary>
  960. /// Check Whether string is null or empty
  961. /// </summary>
  962. /// <param name="selfStr"></param>
  963. /// <returns></returns>
  964. public static bool IsNullOrEmpty(this string selfStr)
  965. {
  966. return string.IsNullOrEmpty(selfStr);
  967. }
  968. /// <summary>
  969. /// Check Whether string is null or empty
  970. /// </summary>
  971. /// <param name="selfStr"></param>
  972. /// <returns></returns>
  973. public static bool IsNotNullAndEmpty(this string selfStr)
  974. {
  975. return !string.IsNullOrEmpty(selfStr);
  976. }
  977. /// <summary>
  978. /// Check Whether string trim is null or empty
  979. /// </summary>
  980. /// <param name="selfStr"></param>
  981. /// <returns></returns>
  982. public static bool IsTrimNotNullAndEmpty(this string selfStr)
  983. {
  984. return selfStr != null && !string.IsNullOrEmpty(selfStr.Trim());
  985. }
  986. public static bool IsTrimNullOrEmpty(this string selfStr)
  987. {
  988. return selfStr == null || string.IsNullOrEmpty(selfStr.Trim());
  989. }
  990. /// <summary>
  991. /// 缓存
  992. /// </summary>
  993. private static readonly char[] mCachedSplitCharArray = { '.' };
  994. /// <summary>
  995. /// Split
  996. /// </summary>
  997. /// <param name="selfStr"></param>
  998. /// <param name="splitSymbol"></param>
  999. /// <returns></returns>
  1000. public static string[] Split(this string selfStr, char splitSymbol)
  1001. {
  1002. mCachedSplitCharArray[0] = splitSymbol;
  1003. return selfStr.Split(mCachedSplitCharArray);
  1004. }
  1005. /// <summary>
  1006. /// 首字母大写
  1007. /// </summary>
  1008. /// <param name="str"></param>
  1009. /// <returns></returns>
  1010. public static string UppercaseFirst(this string str)
  1011. {
  1012. return char.ToUpper(str[0]) + str.Substring(1);
  1013. }
  1014. /// <summary>
  1015. /// 首字母小写
  1016. /// </summary>
  1017. /// <param name="str"></param>
  1018. /// <returns></returns>
  1019. public static string LowercaseFirst(this string str)
  1020. {
  1021. return char.ToLower(str[0]) + str.Substring(1);
  1022. }
  1023. /// <summary>
  1024. ///
  1025. /// </summary>
  1026. /// <param name="str"></param>
  1027. /// <returns></returns>
  1028. public static string ToUnixLineEndings(this string str)
  1029. {
  1030. return str.Replace("\r\n", "\n").Replace("\r", "\n");
  1031. }
  1032. /// <summary>
  1033. /// 转换成 CSV
  1034. /// </summary>
  1035. /// <param name="values"></param>
  1036. /// <returns></returns>
  1037. public static string ToCSV(this string[] values)
  1038. {
  1039. return string.Join(", ", values
  1040. .Where(value => !string.IsNullOrEmpty(value))
  1041. .Select(value => value.Trim())
  1042. .ToArray()
  1043. );
  1044. }
  1045. public static string[] ArrayFromCSV(this string values)
  1046. {
  1047. return values
  1048. .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
  1049. .Select(value => value.Trim())
  1050. .ToArray();
  1051. }
  1052. public static string ToSpacedCamelCase(this string text)
  1053. {
  1054. var sb = new StringBuilder(text.Length * 2);
  1055. sb.Append(char.ToUpper(text[0]));
  1056. for (var i = 1; i < text.Length; i++)
  1057. {
  1058. if (char.IsUpper(text[i]) && text[i - 1] != ' ')
  1059. {
  1060. sb.Append(' ');
  1061. }
  1062. sb.Append(text[i]);
  1063. }
  1064. return sb.ToString();
  1065. }
  1066. /// <summary>
  1067. /// 有点不安全,编译器不会帮你排查错误。
  1068. /// </summary>
  1069. /// <param name="selfStr"></param>
  1070. /// <param name="args"></param>
  1071. /// <returns></returns>
  1072. public static string FillFormat(this string selfStr, params object[] args)
  1073. {
  1074. return string.Format(selfStr, args);
  1075. }
  1076. /// <summary>
  1077. /// 添加前缀
  1078. /// </summary>
  1079. /// <param name="selfStr"></param>
  1080. /// <param name="toAppend"></param>
  1081. /// <returns></returns>
  1082. public static StringBuilder Append(this string selfStr, string toAppend)
  1083. {
  1084. return new StringBuilder(selfStr).Append(toAppend);
  1085. }
  1086. /// <summary>
  1087. /// 添加后缀
  1088. /// </summary>
  1089. /// <param name="selfStr"></param>
  1090. /// <param name="toPrefix"></param>
  1091. /// <returns></returns>
  1092. public static string AddPrefix(this string selfStr, string toPrefix)
  1093. {
  1094. return new StringBuilder(toPrefix).Append(selfStr).ToString();
  1095. }
  1096. /// <summary>
  1097. /// 格式化
  1098. /// </summary>
  1099. /// <param name="selfStr"></param>
  1100. /// <param name="toAppend"></param>
  1101. /// <param name="args"></param>
  1102. /// <returns></returns>
  1103. public static StringBuilder AppendFormat(this string selfStr, string toAppend, params object[] args)
  1104. {
  1105. return new StringBuilder(selfStr).AppendFormat(toAppend, args);
  1106. }
  1107. /// <summary>
  1108. /// 最后一个单词
  1109. /// </summary>
  1110. /// <param name="selfUrl"></param>
  1111. /// <returns></returns>
  1112. public static string LastWord(this string selfUrl)
  1113. {
  1114. return selfUrl.Split('/').Last();
  1115. }
  1116. /// <summary>
  1117. /// 解析成数字类型
  1118. /// </summary>
  1119. /// <param name="selfStr"></param>
  1120. /// <param name="defaulValue"></param>
  1121. /// <returns></returns>
  1122. public static int ToInt(this string selfStr, int defaulValue = 0)
  1123. {
  1124. var retValue = defaulValue;
  1125. return int.TryParse(selfStr, out retValue) ? retValue : defaulValue;
  1126. }
  1127. /// <summary>
  1128. /// 解析到时间类型
  1129. /// </summary>
  1130. /// <param name="selfStr"></param>
  1131. /// <param name="defaultValue"></param>
  1132. /// <returns></returns>
  1133. public static DateTime ToDateTime(this string selfStr, DateTime defaultValue = default(DateTime))
  1134. {
  1135. var retValue = defaultValue;
  1136. return DateTime.TryParse(selfStr, out retValue) ? retValue : defaultValue;
  1137. }
  1138. /// <summary>
  1139. /// 解析 Float 类型
  1140. /// </summary>
  1141. /// <param name="selfStr"></param>
  1142. /// <param name="defaulValue"></param>
  1143. /// <returns></returns>
  1144. public static float ToFloat(this string selfStr, float defaulValue = 0)
  1145. {
  1146. var retValue = defaulValue;
  1147. return float.TryParse(selfStr, out retValue) ? retValue : defaulValue;
  1148. }
  1149. /// <summary>
  1150. /// 是否存在中文字符
  1151. /// </summary>
  1152. /// <param name="input"></param>
  1153. /// <returns></returns>
  1154. public static bool HasChinese(this string input)
  1155. {
  1156. return Regex.IsMatch(input, @"[\u4e00-\u9fa5]");
  1157. }
  1158. /// <summary>
  1159. /// 是否存在空格
  1160. /// </summary>
  1161. /// <param name="input"></param>
  1162. /// <returns></returns>
  1163. public static bool HasSpace(this string input)
  1164. {
  1165. return input.Contains(" ");
  1166. }
  1167. /// <summary>
  1168. /// 删除特定字符
  1169. /// </summary>
  1170. /// <param name="str"></param>
  1171. /// <param name="target"></param>
  1172. /// <returns></returns>
  1173. public static string RemoveString(this string str, params string[] targets)
  1174. {
  1175. return targets.Aggregate(str, (current, t) => current.Replace(t, string.Empty));
  1176. }
  1177. }
  1178. public static class BehaviourExtension
  1179. {
  1180. public static void Example()
  1181. {
  1182. var gameObject = new GameObject();
  1183. var component = gameObject.GetComponent<MonoBehaviour>();
  1184. component.Enable(); // component.enabled = true
  1185. component.Disable(); // component.enabled = false
  1186. }
  1187. public static T Enable<T>(this T selfBehaviour) where T : Behaviour
  1188. {
  1189. selfBehaviour.enabled = true;
  1190. return selfBehaviour;
  1191. }
  1192. public static T Disable<T>(this T selfBehaviour) where T : Behaviour
  1193. {
  1194. selfBehaviour.enabled = false;
  1195. return selfBehaviour;
  1196. }
  1197. }
  1198. public static class CameraExtension
  1199. {
  1200. public static void Example()
  1201. {
  1202. var screenshotTexture2D = Camera.main.CaptureCamera(new Rect(0, 0, Screen.width, Screen.height));
  1203. Debug.Log(screenshotTexture2D);
  1204. }
  1205. public static Texture2D CaptureCamera(this Camera camera, Rect rect)
  1206. {
  1207. var renderTexture = new RenderTexture(Screen.width, Screen.height, 0);
  1208. camera.targetTexture = renderTexture;
  1209. camera.Render();
  1210. RenderTexture.active = renderTexture;
  1211. var screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
  1212. screenShot.ReadPixels(rect, 0, 0);
  1213. screenShot.Apply();
  1214. camera.targetTexture = null;
  1215. RenderTexture.active = null;
  1216. UnityEngine.Object.Destroy(renderTexture);
  1217. return screenShot;
  1218. }
  1219. }
  1220. public static class ColorExtension
  1221. {
  1222. public static void Example()
  1223. {
  1224. var color = "#C5563CFF".HtmlStringToColor();
  1225. Log.I(color);
  1226. }
  1227. /// <summary>
  1228. /// #C5563CFF -> 197.0f / 255,86.0f / 255,60.0f / 255
  1229. /// </summary>
  1230. /// <param name="htmlString"></param>
  1231. /// <returns></returns>
  1232. public static Color HtmlStringToColor(this string htmlString)
  1233. {
  1234. Color retColor;
  1235. var parseSucceed = ColorUtility.TryParseHtmlString(htmlString, out retColor);
  1236. return parseSucceed ? retColor : Color.black;
  1237. }
  1238. /// <summary>
  1239. /// unity's color always new a color
  1240. /// </summary>
  1241. public static Color White = Color.white;
  1242. }
  1243. public static class GraphicExtension
  1244. {
  1245. public static void Example()
  1246. {
  1247. var gameObject = new GameObject();
  1248. var image = gameObject.AddComponent<Image>();
  1249. var rawImage = gameObject.AddComponent<RawImage>();
  1250. // image.color = new Color(image.color.r,image.color.g,image.color.b,1.0f);
  1251. image.ColorAlpha(1.0f);
  1252. rawImage.ColorAlpha(1.0f);
  1253. }
  1254. public static T ColorAlpha<T>(this T selfGraphic, float alpha) where T : Graphic
  1255. {
  1256. var color = selfGraphic.color;
  1257. color.a = alpha;
  1258. selfGraphic.color = color;
  1259. return selfGraphic;
  1260. }
  1261. }
  1262. public static class ImageExtension
  1263. {
  1264. public static void Example()
  1265. {
  1266. var gameObject = new GameObject();
  1267. var image1 = gameObject.AddComponent<Image>();
  1268. image1.FillAmount(0.0f); // image1.fillAmount = 0.0f;
  1269. }
  1270. public static Image FillAmount(this Image selfImage, float fillamount)
  1271. {
  1272. selfImage.fillAmount = fillamount;
  1273. return selfImage;
  1274. }
  1275. }
  1276. public static class LightmapExtension
  1277. {
  1278. public static void SetAmbientLightHTMLStringColor(string htmlStringColor)
  1279. {
  1280. RenderSettings.ambientLight = htmlStringColor.HtmlStringToColor();
  1281. }
  1282. }
  1283. public static class ObjectExtension
  1284. {
  1285. public static void Example()
  1286. {
  1287. var gameObject = new GameObject();
  1288. gameObject.Instantiate()
  1289. .Name("ExtensionExample")
  1290. .DestroySelf();
  1291. gameObject.Instantiate()
  1292. .DestroySelfGracefully();
  1293. gameObject.Instantiate()
  1294. .DestroySelfAfterDelay(1.0f);
  1295. gameObject.Instantiate()
  1296. .DestroySelfAfterDelayGracefully(1.0f);
  1297. gameObject
  1298. .ApplySelfTo(selfObj => Log.I(selfObj.name))
  1299. .Name("TestObj")
  1300. .ApplySelfTo(selfObj => Log.I(selfObj.name))
  1301. .Name("ExtensionExample")
  1302. .DontDestroyOnLoad();
  1303. }
  1304. #region CEUO001 Instantiate
  1305. public static T Instantiate<T>(this T selfObj) where T : UnityEngine.Object
  1306. {
  1307. return UnityEngine.Object.Instantiate(selfObj);
  1308. }
  1309. public static T Instantiate<T>(this T selfObj, Vector3 position, Quaternion rotation)
  1310. where T : UnityEngine.Object
  1311. {
  1312. return UnityEngine.Object.Instantiate(selfObj, position, rotation);
  1313. }
  1314. public static T Instantiate<T>(
  1315. this T selfObj,
  1316. Vector3 position,
  1317. Quaternion rotation,
  1318. Transform parent)
  1319. where T : UnityEngine.Object
  1320. {
  1321. return UnityEngine.Object.Instantiate(selfObj, position, rotation, parent);
  1322. }
  1323. public static T InstantiateWithParent<T>(this T selfObj, Transform parent, bool worldPositionStays)
  1324. where T : UnityEngine.Object
  1325. {
  1326. return (T)UnityEngine.Object.Instantiate((UnityEngine.Object)selfObj, parent, worldPositionStays);
  1327. }
  1328. public static T InstantiateWithParent<T>(this T selfObj, Transform parent) where T : UnityEngine.Object
  1329. {
  1330. return UnityEngine.Object.Instantiate(selfObj, parent, false);
  1331. }
  1332. #endregion
  1333. #region CEUO002 Name
  1334. public static T Name<T>(this T selfObj, string name) where T : UnityEngine.Object
  1335. {
  1336. selfObj.name = name;
  1337. return selfObj;
  1338. }
  1339. #endregion
  1340. #region CEUO003 Destroy Self
  1341. public static void DestroySelf<T>(this T selfObj) where T : UnityEngine.Object
  1342. {
  1343. UnityEngine.Object.Destroy(selfObj);
  1344. }
  1345. public static T DestroySelfGracefully<T>(this T selfObj) where T : UnityEngine.Object
  1346. {
  1347. if (selfObj)
  1348. {
  1349. UnityEngine.Object.Destroy(selfObj);
  1350. }
  1351. return selfObj;
  1352. }
  1353. #endregion
  1354. #region CEUO004 Destroy Self AfterDelay
  1355. public static T DestroySelfAfterDelay<T>(this T selfObj, float afterDelay) where T : UnityEngine.Object
  1356. {
  1357. UnityEngine.Object.Destroy(selfObj, afterDelay);
  1358. return selfObj;
  1359. }
  1360. public static T DestroySelfAfterDelayGracefully<T>(this T selfObj, float delay) where T : UnityEngine.Object
  1361. {
  1362. if (selfObj)
  1363. {
  1364. UnityEngine.Object.Destroy(selfObj, delay);
  1365. }
  1366. return selfObj;
  1367. }
  1368. #endregion
  1369. #region CEUO005 Apply Self To
  1370. public static T ApplySelfTo<T>(this T selfObj, System.Action<T> toFunction) where T : UnityEngine.Object
  1371. {
  1372. toFunction.InvokeGracefully(selfObj);
  1373. return selfObj;
  1374. }
  1375. #endregion
  1376. #region CEUO006 DontDestroyOnLoad
  1377. public static T DontDestroyOnLoad<T>(this T selfObj) where T : UnityEngine.Object
  1378. {
  1379. UnityEngine.Object.DontDestroyOnLoad(selfObj);
  1380. return selfObj;
  1381. }
  1382. #endregion
  1383. public static T As<T>(this object selfObj) where T : class
  1384. {
  1385. return selfObj as T;
  1386. }
  1387. }
  1388. public static class RectTransformExtension
  1389. {
  1390. public static Vector2 GetPosInRootTrans(this RectTransform selfRectTransform, Transform rootTrans)
  1391. {
  1392. return RectTransformUtility.CalculateRelativeRectTransformBounds(rootTrans, selfRectTransform).center;
  1393. }
  1394. public static RectTransform AnchorPosX(this RectTransform selfRectTrans, float anchorPosX)
  1395. {
  1396. var anchorPos = selfRectTrans.anchoredPosition;
  1397. anchorPos.x = anchorPosX;
  1398. selfRectTrans.anchoredPosition = anchorPos;
  1399. return selfRectTrans;
  1400. }
  1401. public static RectTransform AnchorPosY(this RectTransform selfRectTrans, float anchorPosY)
  1402. {
  1403. var anchorPos = selfRectTrans.anchoredPosition;
  1404. anchorPos.y = anchorPosY;
  1405. selfRectTrans.anchoredPosition = anchorPos;
  1406. return selfRectTrans;
  1407. }
  1408. public static RectTransform SetSizeWidth(this RectTransform selfRectTrans, float sizeWidth)
  1409. {
  1410. var sizeDelta = selfRectTrans.sizeDelta;
  1411. sizeDelta.x = sizeWidth;
  1412. selfRectTrans.sizeDelta = sizeDelta;
  1413. return selfRectTrans;
  1414. }
  1415. public static RectTransform SetSizeHeight(this RectTransform selfRectTrans, float sizeHeight)
  1416. {
  1417. var sizeDelta = selfRectTrans.sizeDelta;
  1418. sizeDelta.y = sizeHeight;
  1419. selfRectTrans.sizeDelta = sizeDelta;
  1420. return selfRectTrans;
  1421. }
  1422. public static Vector2 GetWorldSize(this RectTransform selfRectTrans)
  1423. {
  1424. return RectTransformUtility.CalculateRelativeRectTransformBounds(selfRectTrans).size;
  1425. }
  1426. }
  1427. public static class SelectableExtension
  1428. {
  1429. public static T EnableInteract<T>(this T selfSelectable) where T : Selectable
  1430. {
  1431. selfSelectable.interactable = true;
  1432. return selfSelectable;
  1433. }
  1434. public static T DisableInteract<T>(this T selfSelectable) where T : Selectable
  1435. {
  1436. selfSelectable.interactable = false;
  1437. return selfSelectable;
  1438. }
  1439. public static T CancalAllTransitions<T>(this T selfSelectable) where T : Selectable
  1440. {
  1441. selfSelectable.transition = Selectable.Transition.None;
  1442. return selfSelectable;
  1443. }
  1444. }
  1445. public static class ToggleExtension
  1446. {
  1447. public static void RegOnValueChangedEvent(this Toggle selfToggle, UnityAction<bool> onValueChangedEvent)
  1448. {
  1449. selfToggle.onValueChanged.AddListener(onValueChangedEvent);
  1450. }
  1451. }
  1452. /// <summary>
  1453. /// Transform's Extension
  1454. /// </summary>
  1455. public static class TransformExtension
  1456. {
  1457. public static void Example()
  1458. {
  1459. var selfScript = new GameObject().AddComponent<MonoBehaviour>();
  1460. var transform = selfScript.transform;
  1461. transform
  1462. // .Parent(null)
  1463. .LocalIdentity()
  1464. .LocalPositionIdentity()
  1465. .LocalRotationIdentity()
  1466. .LocalScaleIdentity()
  1467. .LocalPosition(Vector3.zero)
  1468. .LocalPosition(0, 0, 0)
  1469. .LocalPosition(0, 0)
  1470. .LocalPositionX(0)
  1471. .LocalPositionY(0)
  1472. .LocalPositionZ(0)
  1473. .LocalRotation(Quaternion.identity)
  1474. .LocalScale(Vector3.one)
  1475. .LocalScaleX(1.0f)
  1476. .LocalScaleY(1.0f)
  1477. .Identity()
  1478. .PositionIdentity()
  1479. .RotationIdentity()
  1480. .Position(Vector3.zero)
  1481. .PositionX(0)
  1482. .PositionY(0)
  1483. .PositionZ(0)
  1484. .Rotation(Quaternion.identity)
  1485. .DestroyChildren()
  1486. .AsLastSibling()
  1487. .AsFirstSibling()
  1488. .SiblingIndex(0);
  1489. selfScript
  1490. // .Parent(null)
  1491. .LocalIdentity()
  1492. .LocalPositionIdentity()
  1493. .LocalRotationIdentity()
  1494. .LocalScaleIdentity()
  1495. .LocalPosition(Vector3.zero)
  1496. .LocalPosition(0, 0, 0)
  1497. .LocalPosition(0, 0)
  1498. .LocalPositionX(0)
  1499. .LocalPositionY(0)
  1500. .LocalPositionZ(0)
  1501. .LocalRotation(Quaternion.identity)
  1502. .LocalScale(Vector3.one)
  1503. .LocalScaleX(1.0f)
  1504. .LocalScaleY(1.0f)
  1505. .Identity()
  1506. .PositionIdentity()
  1507. .RotationIdentity()
  1508. .Position(Vector3.zero)
  1509. .PositionX(0)
  1510. .PositionY(0)
  1511. .PositionZ(0)
  1512. .Rotation(Quaternion.identity)
  1513. .DestroyChildren()
  1514. .AsLastSibling()
  1515. .AsFirstSibling()
  1516. .SiblingIndex(0);
  1517. }
  1518. /// <summary>
  1519. /// 缓存的一些变量,免得每次声明
  1520. /// </summary>
  1521. private static Vector3 mLocalPos;
  1522. private static Vector3 mScale;
  1523. private static Vector3 mPos;
  1524. #region CETR001 Parent
  1525. public static T Parent<T>(this T selfComponent, Component parentComponent) where T : Component
  1526. {
  1527. selfComponent.transform.SetParent(parentComponent == null ? null : parentComponent.transform);
  1528. return selfComponent;
  1529. }
  1530. /// <summary>
  1531. /// 设置成为顶端 Transform
  1532. /// </summary>
  1533. /// <returns>The root transform.</returns>
  1534. /// <param name="selfComponent">Self component.</param>
  1535. /// <typeparam name="T">The 1st type parameter.</typeparam>
  1536. public static T AsRootTransform<T>(this T selfComponent) where T : Component
  1537. {
  1538. selfComponent.transform.SetParent(null);
  1539. return selfComponent;
  1540. }
  1541. #endregion
  1542. #region CETR002 LocalIdentity
  1543. public static T LocalIdentity<T>(this T selfComponent) where T : Component
  1544. {
  1545. selfComponent.transform.localPosition = Vector3.zero;
  1546. selfComponent.transform.localRotation = Quaternion.identity;
  1547. selfComponent.transform.localScale = Vector3.one;
  1548. return selfComponent;
  1549. }
  1550. #endregion
  1551. #region CETR003 LocalPosition
  1552. public static T LocalPosition<T>(this T selfComponent, Vector3 localPos) where T : Component
  1553. {
  1554. selfComponent.transform.localPosition = localPos;
  1555. return selfComponent;
  1556. }
  1557. public static Vector3 GetLocalPosition<T>(this T selfComponent) where T : Component
  1558. {
  1559. return selfComponent.transform.localPosition;
  1560. }
  1561. public static T LocalPosition<T>(this T selfComponent, float x, float y, float z) where T : Component
  1562. {
  1563. selfComponent.transform.localPosition = new Vector3(x, y, z);
  1564. return selfComponent;
  1565. }
  1566. public static T LocalPosition<T>(this T selfComponent, float x, float y) where T : Component
  1567. {
  1568. mLocalPos = selfComponent.transform.localPosition;
  1569. mLocalPos.x = x;
  1570. mLocalPos.y = y;
  1571. selfComponent.transform.localPosition = mLocalPos;
  1572. return selfComponent;
  1573. }
  1574. public static T LocalPositionX<T>(this T selfComponent, float x) where T : Component
  1575. {
  1576. mLocalPos = selfComponent.transform.localPosition;
  1577. mLocalPos.x = x;
  1578. selfComponent.transform.localPosition = mLocalPos;
  1579. return selfComponent;
  1580. }
  1581. public static T LocalPositionY<T>(this T selfComponent, float y) where T : Component
  1582. {
  1583. mLocalPos = selfComponent.transform.localPosition;
  1584. mLocalPos.y = y;
  1585. selfComponent.transform.localPosition = mLocalPos;
  1586. return selfComponent;
  1587. }
  1588. public static T LocalPositionZ<T>(this T selfComponent, float z) where T : Component
  1589. {
  1590. mLocalPos = selfComponent.transform.localPosition;
  1591. mLocalPos.z = z;
  1592. selfComponent.transform.localPosition = mLocalPos;
  1593. return selfComponent;
  1594. }
  1595. public static T LocalPositionIdentity<T>(this T selfComponent) where T : Component
  1596. {
  1597. selfComponent.transform.localPosition = Vector3.zero;
  1598. return selfComponent;
  1599. }
  1600. #endregion
  1601. #region CETR004 LocalRotation
  1602. public static Quaternion GetLocalRotation<T>(this T selfComponent) where T : Component
  1603. {
  1604. return selfComponent.transform.localRotation;
  1605. }
  1606. public static T LocalRotation<T>(this T selfComponent, Quaternion localRotation) where T : Component
  1607. {
  1608. selfComponent.transform.localRotation = localRotation;
  1609. return selfComponent;
  1610. }
  1611. public static T LocalRotationIdentity<T>(this T selfComponent) where T : Component
  1612. {
  1613. selfComponent.transform.localRotation = Quaternion.identity;
  1614. return selfComponent;
  1615. }
  1616. #endregion
  1617. #region CETR005 LocalScale
  1618. public static T LocalScale<T>(this T selfComponent, Vector3 scale) where T : Component
  1619. {
  1620. selfComponent.transform.localScale = scale;
  1621. return selfComponent;
  1622. }
  1623. public static Vector3 GetLocalScale<T>(this T selfComponent) where T : Component
  1624. {
  1625. return selfComponent.transform.localScale;
  1626. }
  1627. public static T LocalScale<T>(this T selfComponent, float xyz) where T : Component
  1628. {
  1629. selfComponent.transform.localScale = Vector3.one * xyz;
  1630. return selfComponent;
  1631. }
  1632. public static T LocalScale<T>(this T selfComponent, float x, float y, float z) where T : Component
  1633. {
  1634. mScale = selfComponent.transform.localScale;
  1635. mScale.x = x;
  1636. mScale.y = y;
  1637. mScale.z = z;
  1638. selfComponent.transform.localScale = mScale;
  1639. return selfComponent;
  1640. }
  1641. public static T LocalScale<T>(this T selfComponent, float x, float y) where T : Component
  1642. {
  1643. mScale = selfComponent.transform.localScale;
  1644. mScale.x = x;
  1645. mScale.y = y;
  1646. selfComponent.transform.localScale = mScale;
  1647. return selfComponent;
  1648. }
  1649. public static T LocalScaleX<T>(this T selfComponent, float x) where T : Component
  1650. {
  1651. mScale = selfComponent.transform.localScale;
  1652. mScale.x = x;
  1653. selfComponent.transform.localScale = mScale;
  1654. return selfComponent;
  1655. }
  1656. public static T LocalScaleY<T>(this T selfComponent, float y) where T : Component
  1657. {
  1658. mScale = selfComponent.transform.localScale;
  1659. mScale.y = y;
  1660. selfComponent.transform.localScale = mScale;
  1661. return selfComponent;
  1662. }
  1663. public static T LocalScaleZ<T>(this T selfComponent, float z) where T : Component
  1664. {
  1665. mScale = selfComponent.transform.localScale;
  1666. mScale.z = z;
  1667. selfComponent.transform.localScale = mScale;
  1668. return selfComponent;
  1669. }
  1670. public static T LocalScaleIdentity<T>(this T selfComponent) where T : Component
  1671. {
  1672. selfComponent.transform.localScale = Vector3.one;
  1673. return selfComponent;
  1674. }
  1675. #endregion
  1676. #region CETR006 Identity
  1677. public static T Identity<T>(this T selfComponent) where T : Component
  1678. {
  1679. selfComponent.transform.position = Vector3.zero;
  1680. selfComponent.transform.rotation = Quaternion.identity;
  1681. selfComponent.transform.localScale = Vector3.one;
  1682. return selfComponent;
  1683. }
  1684. #endregion
  1685. #region CETR007 Position
  1686. public static T Position<T>(this T selfComponent, Vector3 position) where T : Component
  1687. {
  1688. selfComponent.transform.position = position;
  1689. return selfComponent;
  1690. }
  1691. public static Vector3 GetPosition<T>(this T selfComponent) where T : Component
  1692. {
  1693. return selfComponent.transform.position;
  1694. }
  1695. public static T Position<T>(this T selfComponent, float x, float y, float z) where T : Component
  1696. {
  1697. selfComponent.transform.position = new Vector3(x, y, z);
  1698. return selfComponent;
  1699. }
  1700. public static T Position<T>(this T selfComponent, float x, float y) where T : Component
  1701. {
  1702. mPos = selfComponent.transform.position;
  1703. mPos.x = x;
  1704. mPos.y = y;
  1705. selfComponent.transform.position = mPos;
  1706. return selfComponent;
  1707. }
  1708. public static T PositionIdentity<T>(this T selfComponent) where T : Component
  1709. {
  1710. selfComponent.transform.position = Vector3.zero;
  1711. return selfComponent;
  1712. }
  1713. public static T PositionX<T>(this T selfComponent, float x) where T : Component
  1714. {
  1715. mPos = selfComponent.transform.position;
  1716. mPos.x = x;
  1717. selfComponent.transform.position = mPos;
  1718. return selfComponent;
  1719. }
  1720. public static T PositionX<T>(this T selfComponent, Func<float, float> xSetter) where T : Component
  1721. {
  1722. mPos = selfComponent.transform.position;
  1723. mPos.x = xSetter(mPos.x);
  1724. selfComponent.transform.position = mPos;
  1725. return selfComponent;
  1726. }
  1727. public static T PositionY<T>(this T selfComponent, float y) where T : Component
  1728. {
  1729. mPos = selfComponent.transform.position;
  1730. mPos.y = y;
  1731. selfComponent.transform.position = mPos;
  1732. return selfComponent;
  1733. }
  1734. public static T PositionY<T>(this T selfComponent, Func<float, float> ySetter) where T : Component
  1735. {
  1736. mPos = selfComponent.transform.position;
  1737. mPos.y = ySetter(mPos.y);
  1738. selfComponent.transform.position = mPos;
  1739. return selfComponent;
  1740. }
  1741. public static T PositionZ<T>(this T selfComponent, float z) where T : Component
  1742. {
  1743. mPos = selfComponent.transform.position;
  1744. mPos.z = z;
  1745. selfComponent.transform.position = mPos;
  1746. return selfComponent;
  1747. }
  1748. public static T PositionZ<T>(this T selfComponent, Func<float, float> zSetter) where T : Component
  1749. {
  1750. mPos = selfComponent.transform.position;
  1751. mPos.z = zSetter(mPos.z);
  1752. selfComponent.transform.position = mPos;
  1753. return selfComponent;
  1754. }
  1755. #endregion
  1756. #region CETR008 Rotation
  1757. public static T RotationIdentity<T>(this T selfComponent) where T : Component
  1758. {
  1759. selfComponent.transform.rotation = Quaternion.identity;
  1760. return selfComponent;
  1761. }
  1762. public static T Rotation<T>(this T selfComponent, Quaternion rotation) where T : Component
  1763. {
  1764. selfComponent.transform.rotation = rotation;
  1765. return selfComponent;
  1766. }
  1767. public static Quaternion GetRotation<T>(this T selfComponent) where T : Component
  1768. {
  1769. return selfComponent.transform.rotation;
  1770. }
  1771. #endregion
  1772. #region CETR009 WorldScale/LossyScale/GlobalScale/Scale
  1773. public static Vector3 GetGlobalScale<T>(this T selfComponent) where T : Component
  1774. {
  1775. return selfComponent.transform.lossyScale;
  1776. }
  1777. public static Vector3 GetScale<T>(this T selfComponent) where T : Component
  1778. {
  1779. return selfComponent.transform.lossyScale;
  1780. }
  1781. public static Vector3 GetWorldScale<T>(this T selfComponent) where T : Component
  1782. {
  1783. return selfComponent.transform.lossyScale;
  1784. }
  1785. public static Vector3 GetLossyScale<T>(this T selfComponent) where T : Component
  1786. {
  1787. return selfComponent.transform.lossyScale;
  1788. }
  1789. #endregion
  1790. #region CETR010 Destroy All Child
  1791. [Obsolete("弃用啦 请使用 DestroyChildren")]
  1792. public static T DestroyAllChild<T>(this T selfComponent) where T : Component
  1793. {
  1794. return selfComponent.DestroyChildren();
  1795. }
  1796. [Obsolete("弃用啦 请使用 DestroyChildren")]
  1797. public static GameObject DestroyAllChild(this GameObject selfGameObj)
  1798. {
  1799. return selfGameObj.DestroyChildren();
  1800. }
  1801. public static T DestroyChildren<T>(this T selfComponent) where T : Component
  1802. {
  1803. var childCount = selfComponent.transform.childCount;
  1804. for (var i = 0; i < childCount; i++)
  1805. {
  1806. selfComponent.transform.GetChild(i).DestroyGameObjGracefully();
  1807. }
  1808. return selfComponent;
  1809. }
  1810. public static GameObject DestroyChildren(this GameObject selfGameObj)
  1811. {
  1812. var childCount = selfGameObj.transform.childCount;
  1813. for (var i = 0; i < childCount; i++)
  1814. {
  1815. selfGameObj.transform.GetChild(i).DestroyGameObjGracefully();
  1816. }
  1817. return selfGameObj;
  1818. }
  1819. #endregion
  1820. #region CETR011 Sibling Index
  1821. public static T AsLastSibling<T>(this T selfComponent) where T : Component
  1822. {
  1823. selfComponent.transform.SetAsLastSibling();
  1824. return selfComponent;
  1825. }
  1826. public static T AsFirstSibling<T>(this T selfComponent) where T : Component
  1827. {
  1828. selfComponent.transform.SetAsFirstSibling();
  1829. return selfComponent;
  1830. }
  1831. public static T SiblingIndex<T>(this T selfComponent, int index) where T : Component
  1832. {
  1833. selfComponent.transform.SetSiblingIndex(index);
  1834. return selfComponent;
  1835. }
  1836. #endregion
  1837. public static Transform FindByPath(this Transform selfTrans, string path)
  1838. {
  1839. return selfTrans.Find(path.Replace(".", "/"));
  1840. }
  1841. public static Transform SeekTrans(this Transform selfTransform, string uniqueName)
  1842. {
  1843. var childTrans = selfTransform.Find(uniqueName);
  1844. if (null != childTrans)
  1845. return childTrans;
  1846. foreach (Transform trans in selfTransform)
  1847. {
  1848. childTrans = trans.SeekTrans(uniqueName);
  1849. if (null != childTrans)
  1850. return childTrans;
  1851. }
  1852. return null;
  1853. }
  1854. public static T ShowChildTransByPath<T>(this T selfComponent, string tranformPath) where T : Component
  1855. {
  1856. selfComponent.transform.Find(tranformPath).gameObject.Show();
  1857. return selfComponent;
  1858. }
  1859. public static T HideChildTransByPath<T>(this T selfComponent, string tranformPath) where T : Component
  1860. {
  1861. selfComponent.transform.Find(tranformPath).Hide();
  1862. return selfComponent;
  1863. }
  1864. public static void CopyDataFromTrans(this Transform selfTrans, Transform fromTrans)
  1865. {
  1866. selfTrans.SetParent(fromTrans.parent);
  1867. selfTrans.localPosition = fromTrans.localPosition;
  1868. selfTrans.localRotation = fromTrans.localRotation;
  1869. selfTrans.localScale = fromTrans.localScale;
  1870. }
  1871. /// <summary>
  1872. /// 递归遍历子物体,并调用函数
  1873. /// </summary>
  1874. /// <param name="tfParent"></param>
  1875. /// <param name="action"></param>
  1876. public static void ActionRecursion(this Transform tfParent, Action<Transform> action)
  1877. {
  1878. action(tfParent);
  1879. foreach (Transform tfChild in tfParent)
  1880. {
  1881. tfChild.ActionRecursion(action);
  1882. }
  1883. }
  1884. /// <summary>
  1885. /// 递归遍历查找指定的名字的子物体
  1886. /// </summary>
  1887. /// <param name="tfParent">当前Transform</param>
  1888. /// <param name="name">目标名</param>
  1889. /// <param name="stringComparison">字符串比较规则</param>
  1890. /// <returns></returns>
  1891. public static Transform FindChildRecursion(this Transform tfParent, string name,
  1892. StringComparison stringComparison = StringComparison.Ordinal)
  1893. {
  1894. if (tfParent.name.Equals(name, stringComparison))
  1895. {
  1896. //Debug.Log("Hit " + tfParent.name);
  1897. return tfParent;
  1898. }
  1899. foreach (Transform tfChild in tfParent)
  1900. {
  1901. Transform tfFinal = null;
  1902. tfFinal = tfChild.FindChildRecursion(name, stringComparison);
  1903. if (tfFinal)
  1904. {
  1905. return tfFinal;
  1906. }
  1907. }
  1908. return null;
  1909. }
  1910. /// <summary>
  1911. /// 递归遍历查找相应条件的子物体
  1912. /// </summary>
  1913. /// <param name="tfParent">当前Transform</param>
  1914. /// <param name="predicate">条件</param>
  1915. /// <returns></returns>
  1916. public static Transform FindChildRecursion(this Transform tfParent, Func<Transform, bool> predicate)
  1917. {
  1918. if (predicate(tfParent))
  1919. {
  1920. Log.I("Hit " + tfParent.name);
  1921. return tfParent;
  1922. }
  1923. foreach (Transform tfChild in tfParent)
  1924. {
  1925. Transform tfFinal = null;
  1926. tfFinal = tfChild.FindChildRecursion(predicate);
  1927. if (tfFinal)
  1928. {
  1929. return tfFinal;
  1930. }
  1931. }
  1932. return null;
  1933. }
  1934. public static string GetPath(this Transform transform)
  1935. {
  1936. var sb = new System.Text.StringBuilder();
  1937. var t = transform;
  1938. while (true)
  1939. {
  1940. sb.Insert(0, t.name);
  1941. t = t.parent;
  1942. if (t)
  1943. {
  1944. sb.Insert(0, "/");
  1945. }
  1946. else
  1947. {
  1948. return sb.ToString();
  1949. }
  1950. }
  1951. }
  1952. }
  1953. public static class UnityActionExtension
  1954. {
  1955. public static void Example()
  1956. {
  1957. UnityAction action = () => { };
  1958. UnityAction<int> actionWithInt = num => { };
  1959. UnityAction<int, string> actionWithIntString = (num, str) => { };
  1960. action.InvokeGracefully();
  1961. actionWithInt.InvokeGracefully(1);
  1962. actionWithIntString.InvokeGracefully(1, "str");
  1963. }
  1964. /// <summary>
  1965. /// Call action
  1966. /// </summary>
  1967. /// <param name="selfAction"></param>
  1968. /// <returns> call succeed</returns>
  1969. public static bool InvokeGracefully(this UnityAction selfAction)
  1970. {
  1971. if (null != selfAction)
  1972. {
  1973. selfAction();
  1974. return true;
  1975. }
  1976. return false;
  1977. }
  1978. /// <summary>
  1979. /// Call action
  1980. /// </summary>
  1981. /// <param name="selfAction"></param>
  1982. /// <typeparam name="T"></typeparam>
  1983. /// <returns></returns>
  1984. public static bool InvokeGracefully<T>(this UnityAction<T> selfAction, T t)
  1985. {
  1986. if (null != selfAction)
  1987. {
  1988. selfAction(t);
  1989. return true;
  1990. }
  1991. return false;
  1992. }
  1993. /// <summary>
  1994. /// Call action
  1995. /// </summary>
  1996. /// <param name="selfAction"></param>
  1997. /// <returns> call succeed</returns>
  1998. public static bool InvokeGracefully<T, K>(this UnityAction<T, K> selfAction, T t, K k)
  1999. {
  2000. if (null != selfAction)
  2001. {
  2002. selfAction(t, k);
  2003. return true;
  2004. }
  2005. return false;
  2006. }
  2007. /// <summary>
  2008. /// 获得随机列表中元素
  2009. /// </summary>
  2010. /// <typeparam name="T">元素类型</typeparam>
  2011. /// <param name="list">列表</param>
  2012. /// <returns></returns>
  2013. public static T GetRandomItem<T>(this List<T> list)
  2014. {
  2015. return list[UnityEngine.Random.Range(0, list.Count)];
  2016. }
  2017. /// <summary>
  2018. /// 根据权值来获取索引
  2019. /// </summary>
  2020. /// <param name="powers"></param>
  2021. /// <returns></returns>
  2022. public static int GetRandomWithPower(this List<int> powers)
  2023. {
  2024. var sum = 0;
  2025. foreach (var power in powers)
  2026. {
  2027. sum += power;
  2028. }
  2029. var randomNum = UnityEngine.Random.Range(0, sum);
  2030. var currentSum = 0;
  2031. for (var i = 0; i < powers.Count; i++)
  2032. {
  2033. var nextSum = currentSum + powers[i];
  2034. if (randomNum >= currentSum && randomNum <= nextSum)
  2035. {
  2036. return i;
  2037. }
  2038. currentSum = nextSum;
  2039. }
  2040. Log.E("权值范围计算错误!");
  2041. return -1;
  2042. }
  2043. /// <summary>
  2044. /// 根据权值获取值,Key为值,Value为权值
  2045. /// </summary>
  2046. /// <typeparam name="T"></typeparam>
  2047. /// <param name="powersDict"></param>
  2048. /// <returns></returns>
  2049. public static T GetRandomWithPower<T>(this Dictionary<T, int> powersDict)
  2050. {
  2051. var keys = new List<T>();
  2052. var values = new List<int>();
  2053. foreach (var key in powersDict.Keys)
  2054. {
  2055. keys.Add(key);
  2056. values.Add(powersDict[key]);
  2057. }
  2058. var finalKeyIndex = values.GetRandomWithPower();
  2059. return keys[finalKeyIndex];
  2060. }
  2061. }
  2062. public static class AnimatorExtension
  2063. {
  2064. public static void AddAnimatorParameterIfExists(this Animator animator, string parameterName,
  2065. AnimatorControllerParameterType type, List<string> parameterList)
  2066. {
  2067. if (animator.HasParameterOfType(parameterName, type))
  2068. {
  2069. parameterList.Add(parameterName);
  2070. }
  2071. }
  2072. // <summary>
  2073. /// Updates the animator bool.
  2074. /// </summary>
  2075. /// <param name="self">Animator.</param>
  2076. /// <param name="parameterName">Parameter name.</param>
  2077. /// <param name="value">If set to <c>true</c> value.</param>
  2078. public static void UpdateAnimatorBool(this Animator self, string parameterName, bool value,
  2079. List<string> parameterList)
  2080. {
  2081. if (parameterList.Contains(parameterName))
  2082. {
  2083. self.SetBool(parameterName, value);
  2084. }
  2085. }
  2086. public static void UpdateAnimatorTrigger(this Animator self, string parameterName, List<string> parameterList)
  2087. {
  2088. if (parameterList.Contains(parameterName))
  2089. {
  2090. self.SetTrigger(parameterName);
  2091. }
  2092. }
  2093. /// <summary>
  2094. /// Triggers an animator trigger.
  2095. /// </summary>
  2096. /// <param name="self">Animator.</param>
  2097. /// <param name="parameterName">Parameter name.</param>
  2098. /// <param name="value">If set to <c>true</c> value.</param>
  2099. public static void SetAnimatorTrigger(this Animator self, string parameterName, List<string> parameterList)
  2100. {
  2101. if (parameterList.Contains(parameterName))
  2102. {
  2103. self.SetTrigger(parameterName);
  2104. }
  2105. }
  2106. /// <summary>
  2107. /// Updates the animator float.
  2108. /// </summary>
  2109. /// <param name="self">Animator.</param>
  2110. /// <param name="parameterName">Parameter name.</param>
  2111. /// <param name="value">Value.</param>
  2112. public static void UpdateAnimatorFloat(this Animator self, string parameterName, float value,
  2113. List<string> parameterList)
  2114. {
  2115. if (parameterList.Contains(parameterName))
  2116. {
  2117. self.SetFloat(parameterName, value);
  2118. }
  2119. }
  2120. /// <summary>
  2121. /// Updates the animator integer.
  2122. /// </summary>
  2123. /// <param name="self">self.</param>
  2124. /// <param name="parameterName">Parameter name.</param>
  2125. /// <param name="value">Value.</param>
  2126. public static void UpdateAnimatorInteger(this Animator self, string parameterName, int value,
  2127. List<string> parameterList)
  2128. {
  2129. if (parameterList.Contains(parameterName))
  2130. {
  2131. self.SetInteger(parameterName, value);
  2132. }
  2133. }
  2134. // <summary>
  2135. /// Updates the animator bool without checking the parameter's existence.
  2136. /// </summary>
  2137. /// <param name="self">self.</param>
  2138. /// <param name="parameterName">Parameter name.</param>
  2139. /// <param name="value">If set to <c>true</c> value.</param>
  2140. public static void UpdateAnimatorBool(this Animator self, string parameterName, bool value)
  2141. {
  2142. self.SetBool(parameterName, value);
  2143. }
  2144. /// <summary>
  2145. /// Updates the animator trigger without checking the parameter's existence
  2146. /// </summary>
  2147. /// <param name="self">self.</param>
  2148. /// <param name="parameterName">Parameter name.</param>
  2149. public static void UpdateAnimatorTrigger(this Animator self, string parameterName)
  2150. {
  2151. self.SetTrigger(parameterName);
  2152. }
  2153. /// <summary>
  2154. /// Triggers an animator trigger without checking for the parameter's existence.
  2155. /// </summary>
  2156. /// <param name="self">self.</param>
  2157. /// <param name="parameterName">Parameter name.</param>
  2158. /// <param name="value">If set to <c>true</c> value.</param>
  2159. public static void SetAnimatorTrigger(this Animator self, string parameterName)
  2160. {
  2161. self.SetTrigger(parameterName);
  2162. }
  2163. /// <summary>
  2164. /// Updates the animator float without checking for the parameter's existence.
  2165. /// </summary>
  2166. /// <param name="self">self.</param>
  2167. /// <param name="parameterName">Parameter name.</param>
  2168. /// <param name="value">Value.</param>
  2169. public static void UpdateAnimatorFloat(this Animator self, string parameterName, float value)
  2170. {
  2171. self.SetFloat(parameterName, value);
  2172. }
  2173. /// <summary>
  2174. /// Updates the animator integer without checking for the parameter's existence.
  2175. /// </summary>
  2176. /// <param name="self">self.</param>
  2177. /// <param name="parameterName">Parameter name.</param>
  2178. /// <param name="value">Value.</param>
  2179. public static void UpdateAnimatorInteger(this Animator self, string parameterName, int value)
  2180. {
  2181. self.SetInteger(parameterName, value);
  2182. }
  2183. // <summary>
  2184. /// Updates the animator bool after checking the parameter's existence.
  2185. /// </summary>
  2186. /// <param name="self">Animator.</param>
  2187. /// <param name="parameterName">Parameter name.</param>
  2188. /// <param name="value">If set to <c>true</c> value.</param>
  2189. public static void UpdateAnimatorBoolIfExists(this Animator self, string parameterName, bool value)
  2190. {
  2191. if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Bool))
  2192. {
  2193. self.SetBool(parameterName, value);
  2194. }
  2195. }
  2196. public static void UpdateAnimatorTriggerIfExists(this Animator self, string parameterName)
  2197. {
  2198. if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Trigger))
  2199. {
  2200. self.SetTrigger(parameterName);
  2201. }
  2202. }
  2203. /// <summary>
  2204. /// Triggers an animator trigger after checking for the parameter's existence.
  2205. /// </summary>
  2206. /// <param name="self">Animator.</param>
  2207. /// <param name="parameterName">Parameter name.</param>
  2208. /// <param name="value">If set to <c>true</c> value.</param>
  2209. public static void SetAnimatorTriggerIfExists(this Animator self, string parameterName)
  2210. {
  2211. if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Trigger))
  2212. {
  2213. self.SetTrigger(parameterName);
  2214. }
  2215. }
  2216. /// <summary>
  2217. /// Updates the animator float after checking for the parameter's existence.
  2218. /// </summary>
  2219. /// <param name="self">Animator.</param>
  2220. /// <param name="parameterName">Parameter name.</param>
  2221. /// <param name="value">Value.</param>
  2222. public static void UpdateAnimatorFloatIfExists(this Animator self, string parameterName, float value)
  2223. {
  2224. if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Float))
  2225. {
  2226. self.SetFloat(parameterName, value);
  2227. }
  2228. }
  2229. /// <summary>
  2230. /// Updates the animator integer after checking for the parameter's existence.
  2231. /// </summary>
  2232. /// <param name="self">Animator.</param>
  2233. /// <param name="parameterName">Parameter name.</param>
  2234. /// <param name="value">Value.</param>
  2235. public static void UpdateAnimatorIntegerIfExists(this Animator self, string parameterName, int value)
  2236. {
  2237. if (self.HasParameterOfType(parameterName, AnimatorControllerParameterType.Int))
  2238. {
  2239. self.SetInteger(parameterName, value);
  2240. }
  2241. }
  2242. /// <summary>
  2243. /// Determines if an animator contains a certain parameter, based on a type and a name
  2244. /// </summary>
  2245. /// <returns><c>true</c> if has parameter of type the specified self name type; otherwise, <c>false</c>.</returns>
  2246. /// <param name="self">Self.</param>
  2247. /// <param name="name">Name.</param>
  2248. /// <param name="type">Type.</param>
  2249. public static bool HasParameterOfType(this Animator self, string name, AnimatorControllerParameterType type)
  2250. {
  2251. if (string.IsNullOrEmpty(name))
  2252. {
  2253. return false;
  2254. }
  2255. var parameters = self.parameters;
  2256. return parameters.Any(currParam => currParam.type == type && currParam.name == name);
  2257. }
  2258. }
  2259. /// <summary>
  2260. /// GameObject's Util/Static This Extension
  2261. /// </summary>
  2262. public static class GameObjectExtension
  2263. {
  2264. public static void Example()
  2265. {
  2266. var gameObject = new GameObject();
  2267. var transform = gameObject.transform;
  2268. var selfScript = gameObject.AddComponent<MonoBehaviour>();
  2269. var boxCollider = gameObject.AddComponent<BoxCollider>();
  2270. gameObject.Show(); // gameObject.SetActive(true)
  2271. selfScript.Show(); // this.gameObject.SetActive(true)
  2272. boxCollider.Show(); // boxCollider.gameObject.SetActive(true)
  2273. gameObject.transform.Show(); // transform.gameObject.SetActive(true)
  2274. gameObject.Hide(); // gameObject.SetActive(false)
  2275. selfScript.Hide(); // this.gameObject.SetActive(false)
  2276. boxCollider.Hide(); // boxCollider.gameObject.SetActive(false)
  2277. transform.Hide(); // transform.gameObject.SetActive(false)
  2278. selfScript.DestroyGameObj();
  2279. boxCollider.DestroyGameObj();
  2280. transform.DestroyGameObj();
  2281. selfScript.DestroyGameObjGracefully();
  2282. boxCollider.DestroyGameObjGracefully();
  2283. transform.DestroyGameObjGracefully();
  2284. selfScript.DestroyGameObjAfterDelay(1.0f);
  2285. boxCollider.DestroyGameObjAfterDelay(1.0f);
  2286. transform.DestroyGameObjAfterDelay(1.0f);
  2287. selfScript.DestroyGameObjAfterDelayGracefully(1.0f);
  2288. boxCollider.DestroyGameObjAfterDelayGracefully(1.0f);
  2289. transform.DestroyGameObjAfterDelayGracefully(1.0f);
  2290. gameObject.Layer(0);
  2291. selfScript.Layer(0);
  2292. boxCollider.Layer(0);
  2293. transform.Layer(0);
  2294. gameObject.Layer("Default");
  2295. selfScript.Layer("Default");
  2296. boxCollider.Layer("Default");
  2297. transform.Layer("Default");
  2298. }
  2299. #region CEGO001 Show
  2300. public static GameObject Show(this GameObject selfObj)
  2301. {
  2302. selfObj.SetActive(true);
  2303. return selfObj;
  2304. }
  2305. public static T Show<T>(this T selfComponent) where T : Component
  2306. {
  2307. selfComponent.gameObject.Show();
  2308. return selfComponent;
  2309. }
  2310. #endregion
  2311. #region CEGO002 Hide
  2312. public static GameObject Hide(this GameObject selfObj)
  2313. {
  2314. selfObj.SetActive(false);
  2315. return selfObj;
  2316. }
  2317. public static T Hide<T>(this T selfComponent) where T : Component
  2318. {
  2319. selfComponent.gameObject.Hide();
  2320. return selfComponent;
  2321. }
  2322. #endregion
  2323. #region CEGO003 DestroyGameObj
  2324. public static void DestroyGameObj<T>(this T selfBehaviour) where T : Component
  2325. {
  2326. selfBehaviour.gameObject.DestroySelf();
  2327. }
  2328. #endregion
  2329. #region CEGO004 DestroyGameObjGracefully
  2330. public static void DestroyGameObjGracefully<T>(this T selfBehaviour) where T : Component
  2331. {
  2332. if (selfBehaviour && selfBehaviour.gameObject)
  2333. {
  2334. selfBehaviour.gameObject.DestroySelfGracefully();
  2335. }
  2336. }
  2337. #endregion
  2338. #region CEGO005 DestroyGameObjGracefully
  2339. public static T DestroyGameObjAfterDelay<T>(this T selfBehaviour, float delay) where T : Component
  2340. {
  2341. selfBehaviour.gameObject.DestroySelfAfterDelay(delay);
  2342. return selfBehaviour;
  2343. }
  2344. public static T DestroyGameObjAfterDelayGracefully<T>(this T selfBehaviour, float delay) where T : Component
  2345. {
  2346. if (selfBehaviour && selfBehaviour.gameObject)
  2347. {
  2348. selfBehaviour.gameObject.DestroySelfAfterDelay(delay);
  2349. }
  2350. return selfBehaviour;
  2351. }
  2352. #endregion
  2353. #region CEGO006 Layer
  2354. public static GameObject Layer(this GameObject selfObj, int layer)
  2355. {
  2356. selfObj.layer = layer;
  2357. return selfObj;
  2358. }
  2359. public static T Layer<T>(this T selfComponent, int layer) where T : Component
  2360. {
  2361. selfComponent.gameObject.layer = layer;
  2362. return selfComponent;
  2363. }
  2364. public static GameObject Layer(this GameObject selfObj, string layerName)
  2365. {
  2366. selfObj.layer = LayerMask.NameToLayer(layerName);
  2367. return selfObj;
  2368. }
  2369. public static T Layer<T>(this T selfComponent, string layerName) where T : Component
  2370. {
  2371. selfComponent.gameObject.layer = LayerMask.NameToLayer(layerName);
  2372. return selfComponent;
  2373. }
  2374. public static bool IsInLayerMask(this GameObject selfObj, LayerMask layerMask)
  2375. {
  2376. return LayerMaskUtility.IsInLayerMask(selfObj, layerMask);
  2377. }
  2378. public static bool IsInLayerMask<T>(this T selfComponent, LayerMask layerMask) where T : Component
  2379. {
  2380. return LayerMaskUtility.IsInLayerMask(selfComponent.gameObject, layerMask);
  2381. }
  2382. #endregion
  2383. #region CEGO007 Component
  2384. public static T GetOrAddComponent<T>(this GameObject selfComponent) where T : Component
  2385. {
  2386. var comp = selfComponent.gameObject.GetComponent<T>();
  2387. return comp ? comp : selfComponent.gameObject.AddComponent<T>();
  2388. }
  2389. public static T GetOrAddComponent<T>(this Component component) where T : Component
  2390. {
  2391. return component.gameObject.GetOrAddComponent<T>();
  2392. }
  2393. public static Component GetOrAddComponent(this GameObject selfComponent, Type type)
  2394. {
  2395. var comp = selfComponent.gameObject.GetComponent(type);
  2396. return comp ? comp : selfComponent.gameObject.AddComponent(type);
  2397. }
  2398. #endregion
  2399. }
  2400. public static class LayerMaskExtension
  2401. {
  2402. public static bool ContainsGameObject(this LayerMask selfLayerMask, GameObject gameObject)
  2403. {
  2404. return LayerMaskUtility.IsInLayerMask(gameObject, selfLayerMask);
  2405. }
  2406. }
  2407. public static class LayerMaskUtility
  2408. {
  2409. public static bool IsInLayerMask(GameObject gameObj, LayerMask layerMask)
  2410. {
  2411. // 根据Layer数值进行移位获得用于运算的Mask值
  2412. var objLayerMask = 1 << gameObj.layer;
  2413. return (layerMask.value & objLayerMask) == objLayerMask;
  2414. }
  2415. }
  2416. public static class MaterialExtension
  2417. {
  2418. /// <summary>
  2419. /// 参考资料: https://blog.csdn.net/qiminixi/article/details/78402505
  2420. /// </summary>
  2421. /// <param name="self"></param>
  2422. public static void SetStandardMaterialToTransparentMode(this Material self)
  2423. {
  2424. self.SetFloat("_Mode", 3);
  2425. self.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  2426. self.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  2427. self.SetInt("_ZWrite", 0);
  2428. self.DisableKeyword("_ALPHATEST_ON");
  2429. self.EnableKeyword("_ALPHABLEND_ON");
  2430. self.DisableKeyword("_ALPHAPREMULTIPLY_ON");
  2431. self.renderQueue = 3000;
  2432. }
  2433. }
  2434. public static class TextureExtensions
  2435. {
  2436. public static Sprite CreateSprite(this Texture2D self)
  2437. {
  2438. return Sprite.Create(self, new Rect(0, 0, self.width, self.height), Vector2.one * 0.5f);
  2439. }
  2440. }
  2441. internal static class Log
  2442. {
  2443. public enum LogLevel
  2444. {
  2445. None = 0,
  2446. Exception = 1,
  2447. Error = 2,
  2448. Warning = 3,
  2449. Normal = 4,
  2450. Max = 5,
  2451. }
  2452. internal static void LogInfo(this object selfMsg)
  2453. {
  2454. I(selfMsg);
  2455. }
  2456. internal static void LogWarning(this object selfMsg)
  2457. {
  2458. W(selfMsg);
  2459. }
  2460. internal static void LogError(this object selfMsg)
  2461. {
  2462. E(selfMsg);
  2463. }
  2464. internal static void LogException(this Exception selfExp)
  2465. {
  2466. E(selfExp);
  2467. }
  2468. private static LogLevel mLogLevel = LogLevel.Normal;
  2469. public static LogLevel Level
  2470. {
  2471. get { return mLogLevel; }
  2472. set { mLogLevel = value; }
  2473. }
  2474. internal static void I(object msg, params object[] args)
  2475. {
  2476. if (mLogLevel < LogLevel.Normal)
  2477. {
  2478. return;
  2479. }
  2480. if (args == null || args.Length == 0)
  2481. {
  2482. Debug.Log(msg);
  2483. }
  2484. else
  2485. {
  2486. Debug.LogFormat(msg.ToString(), args);
  2487. }
  2488. }
  2489. internal static void E(Exception e)
  2490. {
  2491. if (mLogLevel < LogLevel.Exception)
  2492. {
  2493. return;
  2494. }
  2495. Debug.LogException(e);
  2496. }
  2497. internal static void E(object msg, params object[] args)
  2498. {
  2499. if (mLogLevel < LogLevel.Error)
  2500. {
  2501. return;
  2502. }
  2503. if (args == null || args.Length == 0)
  2504. {
  2505. Debug.LogError(msg);
  2506. }
  2507. else
  2508. {
  2509. Debug.LogError(string.Format(msg.ToString(), args));
  2510. }
  2511. }
  2512. internal static void W(object msg)
  2513. {
  2514. if (mLogLevel < LogLevel.Warning)
  2515. {
  2516. return;
  2517. }
  2518. Debug.LogWarning(msg);
  2519. }
  2520. internal static void W(string msg, params object[] args)
  2521. {
  2522. if (mLogLevel < LogLevel.Warning)
  2523. {
  2524. return;
  2525. }
  2526. Debug.LogWarning(string.Format(msg, args));
  2527. }
  2528. }
  2529. }