Sutil.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. using System.Text.RegularExpressions;
  5. public class Sutil
  6. {
  7. /// <summary>
  8. /// 比较两个字符串中,是否包含与对方一样的字符 .
  9. /// 包含就行 返回true
  10. /// </summary>
  11. /// <param name="strA"></param>
  12. /// <param name="strB"></param>
  13. /// <returns></returns>
  14. public static bool CompareStringByChar(string strA, string strB)
  15. {
  16. bool IsEqual = true;
  17. char[] arrA = strA.ToCharArray();
  18. char[] arrB = strB.ToCharArray();
  19. foreach (char chara in arrA)
  20. {
  21. if (strB.IndexOf(chara) < 0)
  22. {
  23. IsEqual = false;
  24. }
  25. else
  26. {
  27. IsEqual = true;
  28. break;
  29. }
  30. }
  31. return IsEqual;
  32. }
  33. /// <summary>
  34. /// 获取字符串中的数字
  35. /// </summary>
  36. /// <param name="str">字符串</param>
  37. /// <returns>数字</returns>
  38. public static string GetNumber(string str)
  39. {
  40. if (str != null && str != string.Empty)
  41. {
  42. if (Regex.IsMatch(str, @"\d"))
  43. {
  44. // 正则表达式剔除非数字字符(不包含小数点.)
  45. str = Regex.Replace(str, @"[^\d.\d]", "");
  46. return str;
  47. }
  48. else
  49. {
  50. Debug.LogError("string not Exist Number");
  51. }
  52. //// 如果是数字,则转换为decimal类型
  53. //if (Regex.IsMatch(str, @"^[+-]?\d*[.]?\d*$"))
  54. //{
  55. // return str.Substring(0, count - 1);
  56. //}
  57. }
  58. return "";
  59. }
  60. /**
  61. *获取事件串 时:分:秒 00:00:00
  62. * @param time
  63. * @return
  64. *
  65. */
  66. public static string turnTimeToStr(UInt32 time)
  67. {
  68. DateTime date = new DateTime().FromUnixStamp(time);
  69. string str = "";
  70. int hour = Mathf.FloorToInt(time / 60 / 60);
  71. int minute = Mathf.FloorToInt((time - hour * 60 * 60) / 60);
  72. int second = Mathf.FloorToInt(time - hour * 60 * 60 - minute * 60);
  73. if (hour < 10)
  74. {
  75. str += "0";
  76. }
  77. str += hour.ToString();
  78. str += ":";
  79. if (minute < 10)
  80. {
  81. str += "0";
  82. }
  83. str += minute.ToString();
  84. str += ":";
  85. if (second < 10)
  86. {
  87. str += "0";
  88. }
  89. str += second.ToString();
  90. return str;
  91. }
  92. /**
  93. * 获取时间串 年月日
  94. * @param time 秒
  95. * @return
  96. *
  97. */
  98. public static string turnTimeToChinaymd(int time)
  99. {
  100. string res;
  101. DateTime date = new DateTime(time * 1000);
  102. res = date.Year.ToString() + "年" + date.Month.ToString() + "月" + date.Day.ToString() + "日";
  103. return res;
  104. }
  105. /**
  106. * 获取时间串 返回 各式: X月X日
  107. * @param time 秒
  108. * @return
  109. *
  110. */
  111. public static string turnTimeToChinaymd2(int time)
  112. {
  113. string res;
  114. DateTime date = new DateTime(time * 1000);
  115. res = date.Month.ToString() + "月" + date.Day.ToString() + "日";
  116. return res;
  117. }
  118. /**
  119. * 将日期转换为指定格式和指定间隔符的字符串
  120. * @param sec_ 秒
  121. * @param format_ 格式
  122. * @param split_ 间隔符
  123. * @return 格式化字符串
  124. *
  125. */
  126. public static string formatTimeToString(int sec_, string format_ = "ymd", string split_ = "//")
  127. {
  128. string res = "";
  129. DateTime date = new DateTime(sec_ * 1000);
  130. for (int i = 0; i < format_.Length; ++i)
  131. {
  132. string formStr = format_.Substring(i, 1);
  133. string splitStr = split_.Substring(i, 1);
  134. switch (formStr)
  135. {
  136. case "y":
  137. res += date.Year.ToString() + splitStr;
  138. break;
  139. case "m":
  140. res += date.Month.ToString() + splitStr;
  141. break;
  142. case "d":
  143. res += date.Day.ToString() + splitStr;
  144. break;
  145. }
  146. }
  147. return res;
  148. }
  149. /**
  150. * 将日期转换为指定格式和指定间隔符的字符串
  151. * 年月日00:00:00
  152. * @param sec_ 秒
  153. * @param format_ 格式
  154. * @param split_ 间隔符
  155. * @return 格式化字符串
  156. *
  157. */
  158. public static string activeFormatTimeToString(int sec_, string format_ = "ymdhs", string split_ = "//")
  159. {
  160. string res = "";
  161. DateTime date = new DateTime(sec_ * 1000);
  162. for (int i = 0; i < format_.Length; ++i)
  163. {
  164. string formStr = format_.Substring(i, 1);
  165. switch (formStr)
  166. {
  167. case "y":
  168. res += date.Year.ToString() + "年";
  169. break;
  170. case "m":
  171. res += date.Month.ToString() + "月";
  172. break;
  173. case "d":
  174. res += date.Day.ToString() + "日";
  175. break;
  176. case "h":
  177. if (date.Hour < 10)
  178. res += "0" + date.Hour.ToString() + ":";
  179. else
  180. res += date.Hour.ToString() + ":";
  181. break;
  182. case "s":
  183. if (date.Minute < 10)
  184. res += "0" + date.Minute.ToString();
  185. else
  186. res += date.Minute.ToString();
  187. break;
  188. }
  189. }
  190. return res;
  191. }
  192. /// <summary>
  193. /// 获取事件串 x时x分x秒
  194. /// </summary>
  195. /// <returns></returns>
  196. public static string turnTimeToChinaStr(int time)
  197. {
  198. string str = "";
  199. int hour = Mathf.FloorToInt(1.0f *time / 60 / 60);
  200. int minute = Mathf.FloorToInt(((time - hour * 60 * 60) / 60));
  201. int second = Mathf.FloorToInt((time - hour * 60 * 60 - minute * 60));
  202. if (hour > 0)
  203. {
  204. str += hour.ToString() + "小时";
  205. }
  206. if (minute > 0)
  207. {
  208. str += minute.ToString() + "分";
  209. }
  210. if (second > 0)
  211. {
  212. str += second.ToString() + "秒";
  213. }
  214. return str;
  215. }
  216. ///// <summary>
  217. ///// 关于时间转换::最近日期的简易版(类似刚刚,几分钟前等)
  218. ///// </summary>
  219. ///// <param name="time"></param>
  220. ///// <returns></returns>
  221. //public static string turnTimeToDataStr(UInt32 time)
  222. //{
  223. // string str = "";
  224. // DateTime date = new DateTime().FromUnixStamp(time);
  225. // DateTime now = UserProxy.Instance.GetCurrentDateTime();
  226. // // 如果超过两年
  227. // if (now.Year - date.Year >= 2)
  228. // {
  229. // str = "N年前";
  230. // }
  231. // else
  232. // {
  233. // // 如果超过一年
  234. // long yearSecond = 3156000000;
  235. // if (UserProxy.Instance.GetCurrentUnixTimeStamp() - date.GetUnixTimeStamp() >= yearSecond)
  236. // {
  237. // str = "1年前";
  238. // }
  239. // else
  240. // {
  241. // // 如果不在同一年
  242. // if (now.Year != date.Year)
  243. // {
  244. // str = date.ToString("yyyy-MM-dd");// date.Year.ToString() + "." + date.Month.ToString() + "." + date.Day.ToString() + " " + date.Hour.ToString() + ":" + date.Minute.ToString();
  245. // }
  246. // // 如果在同一年
  247. // else
  248. // {
  249. // // 如果在同一天
  250. // if (now.Month == date.Month && now.Day == date.Day)
  251. // {
  252. // if (now.Hour == date.Hour)
  253. // {
  254. // int minutes = now.Minute - date.Minute;
  255. // if (minutes <= 5)
  256. // {
  257. // str = "刚刚";
  258. // }
  259. // else
  260. // {
  261. // str = minutes.ToString() + "分钟前";
  262. // }
  263. // }
  264. // else
  265. // {
  266. // int hours = now.Hour - date.Hour;
  267. // str = hours.ToString() + "小时前";
  268. // }
  269. // }
  270. // // 如果不在同一天
  271. // else
  272. // {
  273. // str = date.ToString("yyyy-MM-dd"); //date.Month.ToString() + "." + date.Day.ToString() + " " + date.Hour.ToString() + ":" + date.Minute.ToString();
  274. // }
  275. // }
  276. // }
  277. // }
  278. // return str;
  279. //}
  280. /// <summary>
  281. ///
  282. /// </summary>
  283. /// <param name="source"></param>
  284. /// <param name="curLeve"></param>
  285. /// <param name="AD">物理攻击</param>
  286. /// <param name="AP">法术强度</param>
  287. /// <returns></returns>
  288. public static string ResolutionSkillDesc(string source, int curLeve, int AD, int AP)
  289. {
  290. Regex reg = new Regex(@"(?is)(?<=\()[^\)]+(?=\))");
  291. string finish = source;
  292. MatchCollection mc = reg.Matches(source);
  293. foreach (Match m in mc)
  294. {
  295. string xx = m.Value;
  296. string[] arr = xx.Split('+');
  297. float result = 0;
  298. foreach (string temp in arr)
  299. {
  300. if (temp.Contains("/"))
  301. {
  302. string[] vvv = temp.Split('/');
  303. if (curLeve >= 1 && curLeve <= vvv.Length)
  304. {
  305. result += System.Convert.ToSingle(vvv[curLeve - 1]);
  306. }
  307. else
  308. {
  309. result += 0;
  310. }
  311. }
  312. else if (temp.Contains("AD"))
  313. {
  314. string a = temp.Substring(0, temp.Length - 2);
  315. result += (int)(AD * System.Convert.ToSingle(a));
  316. }
  317. else if (temp.Contains("AP"))
  318. {
  319. string a = temp.Substring(0, temp.Length - 2);
  320. result += (int)(AP * System.Convert.ToSingle(a));
  321. }
  322. else
  323. {
  324. }
  325. }
  326. //Debug.LogError("________________" + result);
  327. finish = finish.Replace(m.Value, result.ToString());
  328. }
  329. finish = finish.Replace("(", "");
  330. finish = finish.Replace(")", "");
  331. finish = finish.Trim();
  332. return finish;
  333. }
  334. }
  335. #region
  336. // AudioTo:改变声音的音量和音调到指定的数值。
  337. //AudioFrom:将声音的音量和音调从给的数值变化到原始的数值;
  338. //AudioUpdate:类似于AudioTo,在Update()方法或循环环境中调用。提供每帧改变属性值的环境。不依赖于EasrType
  339. //Stab:播放AudioClip一次,可以不用手动加载AudioSource组件
  340. //通过iTween实现声音音量和音调的渐变效果
  341. //二、基础属性
  342. //基础属性比较简单直接上代码
  343. //首先是AudioTo的
  344. //[csharp] view plain copy 在CODE上查看代码片派生到我的代码片
  345. //void Start () {
  346. // //播放的声音对象
  347. // AudioSource tempSource = gameObject.AddComponent<AudioSource>();
  348. // tempSource.loop = true;
  349. // tempSource.clip = soundEnd;
  350. // tempSource.volume = 1;
  351. // tempSource.Play();
  352. // //键值对儿的形式保存iTween所用到的参数
  353. // Hashtable args = new Hashtable();
  354. // //声音
  355. // args.Add("audiosource", tempSource);
  356. // //音量
  357. // args.Add("volume", 0);
  358. // //音调
  359. // args.Add("pitch", 0);
  360. // //变化的时间
  361. // args.Add("time", 10f);
  362. // //延迟执行时间
  363. // args.Add("delay", 0.1f);
  364. // //这里是设置类型,iTween的类型又很多种,在源码中的枚举EaseType中
  365. // args.Add("easeType", iTween.EaseType.easeInOutExpo);
  366. // //三个循环类型 none loop pingPong (一般 循环 来回)
  367. // //args.Add("loopType", "none");
  368. // //args.Add("loopType", "loop");
  369. // args.Add("loopType", iTween.LoopType.pingPong);
  370. // //处理播放过程中的事件。
  371. // //开始播放时调用AnimationStart方法,5.0表示它的参数
  372. // args.Add("onstart", "AnimationStart");
  373. // args.Add("onstartparams", 5.0f);
  374. // //设置接受方法的对象,默认是自身接受,这里也可以改成别的对象接受,
  375. // //那么就得在接收对象的脚本中实现AnimationStart方法。
  376. // args.Add("onstarttarget", gameObject);
  377. // //播放结束时调用,参数和上面类似
  378. // args.Add("oncomplete", "AnimationEnd");
  379. // args.Add("oncompleteparams", "end");
  380. // args.Add("oncompletetarget", gameObject);
  381. // //播放中调用,参数和上面类似
  382. // args.Add("onupdate", "AnimationUpdate");
  383. // args.Add("onupdatetarget", gameObject);
  384. // args.Add("onupdateparams", true);
  385. // iTween.AudioTo(btnBegin, args);
  386. //}
  387. // //动画开始时调用
  388. // void AnimationStart(float f)
  389. // {
  390. // Debug.Log("start :" + f);
  391. // }
  392. // //动画结束时调用
  393. // void AnimationEnd(string f)
  394. // {
  395. // Debug.Log("end : " + f);
  396. // }
  397. // //动画中调用
  398. // void AnimationUpdate(bool f)
  399. // {
  400. // Debug.Log("update :" + f);
  401. // }
  402. #endregion