Tools.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1.  using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using SimpleJSON;
  5. using System.Text;
  6. using System;
  7. using UnityEngine.UI;
  8. using System.Text.RegularExpressions;
  9. public class Tools {
  10. //读取创建预制
  11. public static GameObject createGameObject(string path)
  12. {
  13. if (path == null || path == "") return null;
  14. GameObject obj = null;
  15. try
  16. {
  17. obj = GameObject.Instantiate(Resources.Load(path)) as GameObject;
  18. NameReset(obj);
  19. }
  20. catch (System.Exception ex)
  21. {
  22. Debug.LogError(ex.ToString() + "!!!! path = " + path);
  23. }
  24. return obj;
  25. }
  26. public static GameObject createGameObjectTr(GameObject preObj, GameObject go)
  27. {
  28. GameObject obj = null;
  29. try
  30. {
  31. obj = GameObject.Instantiate(preObj) as GameObject;
  32. NameReset(obj);
  33. }
  34. catch (System.Exception ex)
  35. {
  36. Debug.LogError("!!!! path = "+ex);
  37. }
  38. try
  39. {
  40. obj.transform.SetParent(go.transform );
  41. obj.transform.localPosition = Vector3.zero;
  42. obj.transform.localScale = new Vector3(1,1,1);
  43. obj.transform.localEulerAngles = Vector3.zero;
  44. }
  45. catch (System.Exception ex)
  46. {
  47. Debug.LogError("!!!! GO Tr = Null"+ex);
  48. }
  49. return obj;
  50. }
  51. //读取创建预制 并设置父类
  52. public static GameObject createGameObjectTr(string path, GameObject go)
  53. {
  54. if (path == null || path == "")
  55. return null;
  56. GameObject preObj = (GameObject)Resources.Load(path);
  57. if (preObj == null ){
  58. Debug.LogError("!!!! path = " + path );
  59. return null;
  60. }
  61. return createGameObjectTr(preObj, go );
  62. }
  63. //读取创建预制 并设置父类
  64. public static GameObject CreateUI(string path, GameObject go)
  65. {
  66. if (path == null || path == "") return null;
  67. try
  68. {
  69. return CreateUI((GameObject)Resources.Load(path), go );
  70. }
  71. catch (System.Exception )
  72. {
  73. Debug.LogError("!!!! path = " + path);
  74. }
  75. return null;
  76. }
  77. //读取 UI 预制 并设置父类
  78. public static GameObject createUITr(string path, Transform tr)
  79. {
  80. if (path == null || path == "") return null;
  81. GameObject obj = null;
  82. try
  83. {
  84. obj = GameObject.Instantiate(Resources.Load(path)) as GameObject;
  85. NameReset(obj);
  86. }
  87. catch (System.Exception)
  88. {
  89. Debug.LogError("!!!! path = " + path);
  90. }
  91. try
  92. {
  93. obj.transform.SetParent(tr);
  94. obj.transform.localPosition = Vector3.zero;
  95. obj.transform.localScale = Vector3.one;
  96. }
  97. catch (System.Exception)
  98. {
  99. Debug.LogError("!!!! GO Tr = Null");
  100. }
  101. return obj;
  102. }
  103. //读取创建预制 并设置父类
  104. public static GameObject CreateUI(GameObject preUI, GameObject go)
  105. {
  106. if (preUI == null) return null;
  107. GameObject obj = null;
  108. try
  109. {
  110. obj = GameObject.Instantiate(preUI) as GameObject;
  111. NameReset(obj);
  112. }
  113. catch (System.Exception ex)
  114. {
  115. Debug.LogError("!!!! preUI null = "+ex );
  116. }
  117. // try
  118. // {
  119. obj.transform.SetParent(go.transform );
  120. RectTransform rectTransform = obj.GetComponent<RectTransform>();
  121. if (rectTransform != null ){
  122. rectTransform.offsetMax = new Vector2(0,0);
  123. rectTransform.offsetMin = new Vector2(0,0);
  124. obj.transform.localPosition = Vector3.zero;
  125. obj.transform.localScale = new Vector3(1,1,1);
  126. // rectTransform.TransformPoint(0, 0, 0);
  127. // rectTransform.position = Vector3.zero;
  128. }else{
  129. obj.transform.localPosition = Vector3.zero;
  130. obj.transform.localScale = new Vector3(1,1,1);
  131. }
  132. // }
  133. // catch (System.Exception ex)
  134. // {
  135. // Debug.LogError("!!!! GO Tr = Null");
  136. // }
  137. return obj;
  138. }
  139. // 读取对象 Object
  140. public static UnityEngine.Object LoadResources(string path)
  141. {
  142. UnityEngine.Object obj = Resources.Load(path);
  143. if (obj == null) return null;
  144. UnityEngine.Object go = UnityEngine.Object.Instantiate(obj);
  145. return path != null ? go : null;
  146. }
  147. //创建物体并设置贴图及增加组件
  148. public static GameObject SetObjectProperty(GameObject obj, Texture2D tex,Transform tr,string path)
  149. {
  150. try
  151. {
  152. obj.transform.parent = tr.parent.Find(path);
  153. obj.transform.localScale = Vector3.one;
  154. obj.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
  155. obj.GetComponent<Image>().color = new Color(1, 1, 1, 1);
  156. obj.GetComponent<RectTransform>().sizeDelta = new Vector2(tex.width, tex.height);
  157. obj.GetComponent<BoxCollider2D>().size = new Vector2(tex.width, tex.height);
  158. obj.GetComponent<BoxCollider2D>().isTrigger = true;
  159. obj.GetComponent<Rigidbody2D>().gravityScale = 0;
  160. }
  161. catch
  162. {
  163. Debug.LogError("预制件组件缺失,请先增加");
  164. }
  165. return obj;
  166. }
  167. // 修改替换 图片
  168. public static void SetObjectImage(GameObject go, string path, string img_path)
  169. {
  170. try
  171. {
  172. Sprite temp = Resources.Load(img_path, typeof(Sprite)) as Sprite;
  173. go.transform.Find(path).GetComponent<Image>().overrideSprite = temp;
  174. }
  175. catch
  176. {
  177. Debug.LogError("!!!替换图片出错 == " + go.name + " || " + path + " ||" + img_path);
  178. }
  179. }
  180. // 修改替换 图片
  181. public static void SetImageToImage(Image img, string img_path)
  182. {
  183. try
  184. {
  185. Sprite temp = Resources.Load(img_path, typeof(Sprite)) as Sprite;
  186. img.sprite = temp;
  187. }
  188. catch
  189. {
  190. Debug.LogError("!!!替换图片出错 == " + img.name + " ||" + img_path);
  191. }
  192. }
  193. // 修改替换 图片
  194. public static void SetObjectImage(GameObject go, string img_path)
  195. {
  196. try
  197. {
  198. Sprite temp = Resources.Load(img_path, typeof(Sprite)) as Sprite;
  199. go.GetComponent<Image>().overrideSprite = temp;
  200. }
  201. catch
  202. {
  203. Debug.LogWarning("!!!替换图片出错 == " + go.name + " ||" + img_path);
  204. }
  205. }
  206. public static Sprite SetPathToSprite(string path)
  207. {
  208. Texture2D tex = new Texture2D(100, 100);
  209. try
  210. {
  211. tex = (Texture2D)Resources.Load(path, typeof(Texture2D));
  212. }
  213. catch (System.Exception)
  214. {
  215. Debug.LogError("!!!! tex = Null path =" + path);
  216. return null;
  217. }
  218. Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
  219. return sp;
  220. }
  221. public static Sprite SetTexToSprite(Texture2D tex)
  222. {
  223. Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
  224. return sp;
  225. }
  226. //读取 2D 纹理
  227. public static Texture2D LoadTexture2D(string path)
  228. {
  229. Texture2D tex = new Texture2D(100, 100);
  230. try
  231. {
  232. tex = (Texture2D)Resources.Load(path, typeof(Texture2D));
  233. }
  234. catch (System.Exception )
  235. {
  236. Debug.LogError("!!!! tex = Null path =" + path);
  237. return null;
  238. }
  239. return tex;
  240. }
  241. //返回 text
  242. public static Text GetUGUI_Text(GameObject go, string path = null)
  243. {
  244. if (go == null) { return null; }
  245. Text text;
  246. if (string.IsNullOrEmpty(path))
  247. {
  248. text = go.GetComponent<Text>();
  249. }
  250. else
  251. text = go.transform.Find(path).GetComponent<Text>();
  252. return text;
  253. }
  254. public static Text GetUGUI_Text(Transform go, string path = null)
  255. {
  256. if (go == null) { return null; }
  257. Text text;
  258. if (string.IsNullOrEmpty(path))
  259. {
  260. text = go.GetComponent<Text>();
  261. }
  262. else
  263. text = go.Find(path).GetComponent<Text>();
  264. return text;
  265. }
  266. //返回 Image
  267. public static Image GetUGUI_Image(GameObject go, string path = null)
  268. {
  269. if (go == null) { return null; }
  270. Image img;
  271. if (string.IsNullOrEmpty(path))
  272. {
  273. img = go.GetComponent<Image>();
  274. }
  275. else
  276. img = go.transform.Find(path).GetComponent<Image>();
  277. return img;
  278. }
  279. public static Image GetUGUI_Image(Transform go, string path = null)
  280. {
  281. if (go == null) { return null; }
  282. Image img;
  283. if (string.IsNullOrEmpty(path))
  284. {
  285. img = go.GetComponent<Image>();
  286. }
  287. else
  288. img = go.Find(path).GetComponent<Image>();
  289. return img;
  290. }
  291. public static Sprite TexToSprite(Texture2D tex)
  292. {
  293. if (tex == null)
  294. {
  295. return null;
  296. }
  297. Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
  298. return sp;
  299. }
  300. //两个Text 首尾相连
  301. public static void SetTextToTextPosition(Transform tr, Transform tr2)
  302. {
  303. float width = tr.GetComponent<RectTransform>().sizeDelta.x;
  304. float x = tr.localPosition.x;
  305. tr2.localPosition = new Vector3(x + width, tr2.localPosition.y, 0);
  306. }
  307. //重置 预制件名字
  308. public static void NameReset(GameObject go)
  309. {
  310. int fpos = go.name.IndexOf("(");
  311. if (fpos >= 0)
  312. {
  313. go.name = go.name.Substring(0, fpos);
  314. }
  315. }
  316. public static string NameReset(string name)
  317. {
  318. int fpos = name.IndexOf("(");
  319. if (fpos >= 0)
  320. {
  321. name = name.Substring(0, fpos);
  322. }
  323. return name;
  324. }
  325. public static Vector3 RandomPos(Vector2 x,Vector2 y,Vector2 z)
  326. {
  327. float X = UnityEngine.Random.Range(x.x, x.y);
  328. float Y = UnityEngine.Random.Range(y.x, y.y);
  329. float Z = UnityEngine.Random.Range(z.x, z.y);
  330. Vector3 pos = new Vector3(X, Y, Z);
  331. return pos;
  332. }
  333. //秒 转 小时 分 秒
  334. public static string FormatTime_H(int seconds)
  335. {
  336. int intH = seconds / 3600;
  337. string strH = intH < 10 ? "0" + intH.ToString() : intH.ToString();
  338. int intM = (seconds % 3600) / 60;
  339. string strM = intM < 10 ? "0" + intM.ToString() : intM.ToString();
  340. int intS = seconds % 3600 % 60;
  341. string strS = intS < 10 ? "0" + intS.ToString() : intS.ToString();
  342. return strH + ":" + strM + ":" + strS;
  343. }
  344. //秒 转 分 秒
  345. public static string FormatTime_M(int seconds)
  346. {
  347. int intM = seconds / 60;
  348. string strM = intM < 10 ? "0" + intM.ToString() : intM.ToString();
  349. int intS = seconds % 60;
  350. string strS = intS < 10 ? "0" + intS.ToString() : intS.ToString();
  351. return strM + ":" + strS;
  352. }
  353. //毫秒 转 分 秒 毫秒
  354. public static string FormatTime_MS(int seconds)
  355. {
  356. TimeSpan ss = new TimeSpan(0, 0, 0, 0, (int)seconds);
  357. string timeString = string.Format("{0}:{1}.{2}", ss.Minutes.ToString("00"), ss.Seconds.ToString("00"), ss.Milliseconds.ToString("00").Substring(0,2));
  358. return timeString;
  359. }
  360. // 颜色字符(0xffffffff)转换 color
  361. public static Color ColorFromString(string colorstring)
  362. {
  363. int r = VFromChar(colorstring[0]) * 16 + VFromChar(colorstring[1]);
  364. int g = VFromChar(colorstring[2]) * 16 + VFromChar(colorstring[3]);
  365. int b = VFromChar(colorstring[4]) * 16 + VFromChar(colorstring[5]);
  366. int a = VFromChar(colorstring[6]) * 16 + VFromChar(colorstring[7]);
  367. return new UnityEngine.Color(r * 1f / 255, g * 1f / 255, b * 1f / 255, a * 1f / 255);
  368. }
  369. static int VFromChar(int c)
  370. {
  371. if (c >= '0' && c <= '9')
  372. {
  373. return c - '0';
  374. }
  375. else if (c >= 'A' && c <= 'F')
  376. {
  377. return c - 'A' + 10;
  378. }
  379. else
  380. {
  381. return c - 'a' + 10;
  382. }
  383. }
  384. // 3D物体在2D屏幕上的位置
  385. public static Vector3 GetUIPosBy3DGameObj(GameObject gobj3d,
  386. Camera camer3d, Camera camera2d, float z, float y)
  387. {
  388. Vector3 v1 = camer3d.WorldToViewportPoint(new Vector3(gobj3d.transform.position.x, y, gobj3d.transform.position.z));
  389. Vector3 v2 = camera2d.ViewportToWorldPoint(v1);
  390. v2.z = z;
  391. return v2;
  392. }
  393. //设置2d物体 到 3D物体在屏幕上的位置
  394. public static void SetUIPosBy3DGameObj(GameObject gobj2d, GameObject gobj3d,
  395. Camera camer3d, Camera camera2d, float z, Vector3 offset)
  396. {
  397. Vector3 v1 = camer3d.WorldToViewportPoint(gobj3d.transform.position);
  398. Vector3 v2 = camera2d.ViewportToWorldPoint(v1);
  399. v2.z = z;
  400. gobj2d.transform.position = v2 + offset;
  401. }
  402. //返回物体内名字为 “” 的gameobject
  403. static GameObject findGo = null;
  404. public static GameObject GetNameFindGameObject(GameObject go, string name)
  405. {
  406. findGo = null;
  407. GetFindGameObjectName(go, name);
  408. if (findGo != null)
  409. {
  410. return findGo;
  411. }
  412. return findGo;
  413. }
  414. static void GetFindGameObjectName(GameObject go, string name)
  415. {
  416. bool find = false;
  417. for (int i = 0; i < go.transform.childCount; i++)
  418. {
  419. if (go.transform.GetChild(i).name == name)
  420. {
  421. find = true;
  422. findGo = go.transform.GetChild(i).gameObject;
  423. return;
  424. }
  425. }
  426. if (!find)
  427. {
  428. for (int i = 0; i < go.transform.childCount; i++)
  429. {
  430. if (go.transform.GetChild(i).childCount > 0)
  431. {
  432. GetFindGameObjectName(go.transform.GetChild(i).gameObject, name);
  433. }
  434. }
  435. }
  436. }
  437. //写 二进制文件
  438. public static void WriteBytes(string path, byte[] bytes){
  439. FileStream fs = new FileStream(path, FileMode.Create);
  440. fs.Write(bytes, 0, bytes.Length);
  441. fs.Flush();
  442. fs.Close();
  443. }
  444. //写 txt文件
  445. public static void WriteTxt(string path, string text)
  446. {
  447. FileStream fs = new FileStream(path, FileMode.Create);
  448. byte[] data = System.Text.Encoding.UTF8.GetBytes(text.ToString());
  449. fs.Write(data, 0, data.Length);
  450. fs.Flush();
  451. fs.Close();
  452. }
  453. //删除文件
  454. public static void RemoveTxt(string path)
  455. {
  456. File.Delete(path);
  457. }
  458. // 项目内部 读文件 String
  459. public static string LoadGameString(string path)
  460. {
  461. string txt = ((TextAsset)Resources.Load(path)).text;
  462. return txt;
  463. }
  464. // 项目内部 读文件 json
  465. public static JSONNode LoadGameJson(string path)
  466. {
  467. string txt = ((TextAsset)Resources.Load(path)).text;
  468. JSONNode json = JSONClass.Parse(txt);
  469. return json;
  470. }
  471. //项目内 读取二进制文件
  472. public static byte[] LoadBytes(string path)
  473. {
  474. FileStream file = new FileStream(path, FileMode.Open);
  475. int len = (int)file.Length;
  476. byte[] byData = new byte[len];
  477. file.Read(byData, 0, len);
  478. file.Close();
  479. return byData;
  480. }
  481. //外部 读txt文件
  482. public static string LoadString(string path)
  483. {
  484. FileStream file = new FileStream(path, FileMode.Open);
  485. int len = (int)file.Length;
  486. byte[] byData = new byte[len];
  487. file.Read(byData, 0, len);
  488. string text = Encoding.UTF8.GetString(byData);
  489. file.Close();
  490. return text;
  491. }
  492. //外部获取某个文件夹下某张图片并转换成sprite
  493. public static Sprite PathToSprite(string path,int i,Sprite src)
  494. {
  495. try
  496. {
  497. DirectoryInfo d = new DirectoryInfo(path);
  498. FileSystemInfo[] fsinfos = d.GetFileSystemInfos();
  499. byte[] bytes = Tools.LoadBytes(fsinfos[i].FullName);
  500. Texture2D tex = new Texture2D(100, 100);
  501. tex.LoadImage(bytes);
  502. Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
  503. src = sp;
  504. }
  505. catch (Exception)
  506. {
  507. Debug.Log("没有相对于的鱼");
  508. }
  509. return src;
  510. }
  511. //字符串是否 有中文字
  512. public static bool IsChinese(string text)
  513. {
  514. for (int i = 0; i < text.Length; i++ )
  515. {
  516. if (System.Text.RegularExpressions.Regex.IsMatch(text[i].ToString(), @"^[\u4e00-\u9fa5]+$"))
  517. {
  518. return true;
  519. }
  520. }
  521. return false;
  522. }
  523. //字符串是否 有 特殊符号
  524. public static bool IsSymbol(string text)
  525. {
  526. for (int i = 0; i < text.Length; i++)
  527. {
  528. if (!char.IsLetter(text[i]) && !char.IsNumber(text[i]))
  529. {
  530. return true;
  531. }
  532. }
  533. return false;
  534. }
  535. //字符串长度(中文字为2个字符)
  536. public static int GetStringLength(string text)
  537. {
  538. int num = 0;
  539. for (int i = 0; i < text.Length; i++)
  540. {
  541. if (System.Text.RegularExpressions.Regex.IsMatch(text[i].ToString(), @"^[\u4e00-\u9fa5]+$"))
  542. {
  543. num++;
  544. }
  545. }
  546. return text.Length + num;
  547. }
  548. // 中英字 是否超出长度
  549. public static bool IsStringLength(string text, int num)
  550. {
  551. if (text.Length > num) return true;
  552. int temp = 0;
  553. for (int i = 0; i < text.Length; i++)
  554. {
  555. if (System.Text.RegularExpressions.Regex.IsMatch(text[i].ToString(), @"^[\u4e00-\u9fa5]+$"))
  556. {
  557. temp++;
  558. }
  559. }
  560. if (text.Length + temp > num)
  561. {
  562. return true;
  563. }
  564. return false;
  565. }
  566. //字符串 是否 纯数字
  567. public static bool IsNumber(string str)
  568. {
  569. for (int i = 0; i < str.Length; i++ )
  570. {
  571. if (!Char.IsNumber(str, i))
  572. {
  573. return false;
  574. }
  575. }
  576. return true;
  577. }
  578. // 是否 是正确 的邮箱地址
  579. public static bool IsEmail(string str_email)
  580. {
  581. return System.Text.RegularExpressions.Regex.IsMatch(str_email, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
  582. }
  583. //获取时间戳
  584. public static string GetTimeStamp_MS()
  585. {
  586. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  587. return Convert.ToInt64(ts.TotalMilliseconds).ToString();
  588. }
  589. //解析时间戳
  590. public static string[] GetTimeStamp_ch(long timeStamp)
  591. {
  592. DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
  593. long lTime = ((long)timeStamp * 10000);
  594. TimeSpan toNow = new TimeSpan(lTime);
  595. DateTime dtResult = dtStart.Add(toNow);
  596. string date = dtResult.ToShortDateString().ToString();
  597. string time = dtResult.ToString("HH:mm:ss");
  598. string[] date_arr = date.Split('/');
  599. string[] time_arr = time.Split(':');
  600. string secondarr = time_arr[2];
  601. char[] second = secondarr.ToCharArray();
  602. string[] result = new string[]{ date_arr[2] + "年" + date_arr[0] + "月" + date_arr[1] + "日",
  603. time_arr[0] + ":" +time_arr[1] + ":" + second[0] + second[1]};
  604. return result;
  605. }
  606. public static string[] GetTimeCountDown(long overtime)
  607. {
  608. long nowTime = Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds);
  609. long timef = (overtime - nowTime) / 1000;
  610. string[] time = new string[4];
  611. time[0] = Math.Floor((float)(timef / 86400)).ToString();
  612. timef -= int.Parse(time[0]) * 86400;
  613. time[1] = Math.Floor((float)(timef / 3600)).ToString();
  614. timef -= int.Parse(time[1]) * 3600;
  615. time[2] = Math.Floor((float)(timef / 60)).ToString();
  616. timef -= int.Parse(time[2]) * 60;
  617. time[3] = timef.ToString();
  618. return time;
  619. }
  620. public static string GetTimeStamp(long _time)
  621. {
  622. long timeStamp = _time;
  623. System.DateTime dtStart = System.TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  624. Debug.LogWarning(timeStamp);
  625. long lTime =long.Parse(timeStamp+"0000000");
  626. System.TimeSpan toNow = new System.TimeSpan(lTime);
  627. System.DateTime dtResult = dtStart.Add(toNow);
  628. string date = dtResult.ToShortDateString().ToString();
  629. string[] date_arr = date.Split('/');
  630. string result = (date_arr[2] + "/" + date_arr[0] + "/" + date_arr[1]);
  631. return result;
  632. }
  633. public static string GetTimeFormat(int second)
  634. {
  635. TimeSpan ts = new TimeSpan(0, 0, second);
  636. return string.Format("{0:d2}:{1:d2}:{2:d2}",(int)ts.TotalHours, ts.Minutes, ts.Seconds);
  637. }
  638. public static string[] GetTimeStamp(string _time)
  639. {
  640. long timeStamp = long.Parse(_time);
  641. System.DateTime dtStart = System.TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  642. long lTime = timeStamp * 10000000;
  643. System.TimeSpan toNow = new System.TimeSpan(lTime);
  644. System.DateTime dtResult = dtStart.Add(toNow);
  645. string date = dtResult.ToShortDateString().ToString();
  646. string time = dtResult.ToString("HH:mm:ss");
  647. string[] date_arr = date.Split('/');
  648. string[] time_arr = time.Split(':');
  649. string secondarr = time_arr[2];
  650. char[] second = secondarr.ToCharArray();
  651. string[] result = new string[]{ date_arr[2] + "/" + date_arr[0] + "/" + date_arr[1],
  652. time_arr[0] + ":" +time_arr[1] + ":" + second[0] + second[1]};
  653. return result;
  654. }
  655. public static T GetOrCreateComponent<T>(GameObject go ) where T : Component{
  656. if (go.GetComponent<T>()==null){
  657. return go.AddComponent<T>();
  658. }
  659. return go.GetComponent<T>();
  660. }
  661. public static GameObject FindGoByID(GameObject parent, string childName)
  662. {
  663. if(parent.name == childName)
  664. {
  665. return parent;
  666. }
  667. if(parent.transform.childCount < 1)
  668. {
  669. return null;
  670. }
  671. GameObject obj = null;
  672. for(int i = 0;i<parent.transform.childCount;i++)
  673. {
  674. GameObject go = parent.transform.GetChild(i).gameObject;
  675. obj = FindGoByID(go, childName);
  676. if(obj != null)
  677. {
  678. break;
  679. }
  680. }
  681. return obj;
  682. }
  683. public static Com FindGoByID<Com>(GameObject parent, string childName) where Com : Component{
  684. if(parent.name == childName)
  685. {
  686. return parent.GetComponent<Com>();
  687. }
  688. Com[] childs = parent.GetComponentsInChildren<Com>(true );
  689. foreach(Com child in childs ){
  690. if (child.gameObject.name == childName ){
  691. return child;
  692. }
  693. }
  694. return null;
  695. }
  696. public static void ActiveAll(GameObject parent, bool isActive ){
  697. Transform[] allTrans = parent.GetComponentsInChildren<Transform>(true);
  698. foreach(Transform child in allTrans ){
  699. if (child.gameObject.activeSelf!=isActive ){
  700. child.gameObject.SetActive(isActive );
  701. }
  702. }
  703. }
  704. public static void ActiveChildren(GameObject parent, GameObject exceptGo, bool isActive ){
  705. for (int i = 0; i < parent.transform.childCount; i++ ){
  706. Transform child = parent.transform.GetChild(i);
  707. if (child.gameObject == exceptGo ){
  708. continue;
  709. }
  710. child.gameObject.SetActive(isActive );
  711. }
  712. }
  713. public static bool ObjectIsInArray<T>(T target, T[] targetArray)
  714. {
  715. for (int i = 0; i < targetArray.Length; i++)
  716. {
  717. if (target.Equals(targetArray[i]))
  718. return true;
  719. }
  720. return false;
  721. }
  722. public static void SetParent(GameObject target, GameObject parent ){
  723. Vector3 orginPosition = target.transform.localPosition;
  724. Vector3 orginAngles = target.transform.localEulerAngles;
  725. Vector3 orginScale = target.transform.localScale;
  726. target.transform.SetParent(parent.transform);
  727. target.transform.localEulerAngles = orginAngles;
  728. target.transform.localScale = orginScale;
  729. target.transform.localPosition = orginPosition;
  730. }
  731. public static bool IsAnimatorPlay(Animator animator ){
  732. return animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1;
  733. }
  734. public static IEnumerator DelayExecute(float time, Action del)
  735. {
  736. yield return new WaitForSeconds(time);
  737. if (del != null)
  738. del();
  739. }
  740. /**
  741. unit: ms
  742. */
  743. public static long GetRealTick(){
  744. long curUnixTime = DateTime.UtcNow.ToFileTimeUtc();
  745. DateTime date70 = new DateTime(1970, 1, 1, 0, 0, 0);
  746. long fileTime70 = curUnixTime - date70.ToFileTimeUtc();
  747. return fileTime70/10000;
  748. }
  749. }
  750. public class Gow{
  751. private GameObject mGo;
  752. public static Gow c(GameObject go ){
  753. Gow gow = new Gow();
  754. gow.mGo = go;
  755. return gow;
  756. }
  757. public GameObject Find(string childName)
  758. {
  759. Transform trans = Find<Transform>(childName);
  760. if (trans != null ){
  761. return trans.gameObject;
  762. }
  763. RectTransform rectTrans = Find<RectTransform>(childName);
  764. if (rectTrans != null ){
  765. return rectTrans.gameObject;
  766. }
  767. return null;
  768. }
  769. public Com Find<Com>(string childName) where Com : Component{
  770. if(mGo.name == childName)
  771. {
  772. return mGo.GetComponent<Com>();
  773. }
  774. Com[] childs = mGo.GetComponentsInChildren<Com>(true );
  775. foreach(Com child in childs ){
  776. //Debug.Log("name is " + child.gameObject.name );
  777. if (child.gameObject.name == childName ){
  778. return child;
  779. }
  780. }
  781. return null;
  782. }
  783. public void Normalize(GameObject parent ){
  784. mGo.transform.parent = parent.transform;
  785. mGo.transform.localPosition = Vector3.zero;
  786. mGo.transform.localEulerAngles = Vector3.zero;
  787. mGo.transform.localScale = new Vector3(1,1,1);
  788. }
  789. public void ActiveAll(bool isActive ){
  790. Transform[] allTrans = mGo.GetComponentsInChildren<Transform>(true );
  791. foreach(Transform trans in allTrans ){
  792. trans.gameObject.SetActive(isActive );
  793. }
  794. }
  795. public void PostPrefab(GameObject prefab ){
  796. Transform prefabsParent = mGo.transform.Find("ym_prefabs_parent");
  797. if (prefabsParent == null ){
  798. GameObject go = new GameObject("ym_prefabs_parent");
  799. Tools.SetParent(go, mGo );
  800. prefabsParent = go.transform;
  801. prefabsParent.SetAsLastSibling();
  802. }
  803. Tools.SetParent(prefab, prefabsParent.gameObject );
  804. prefab.SetActive(false );
  805. }
  806. public GameObject GetOrCreateGo(string name ){
  807. Transform childTrans = mGo.transform.Find(name);
  808. if (childTrans == null ){
  809. GameObject go = new GameObject(name );
  810. Tools.SetParent(go, mGo );
  811. childTrans = go.transform;
  812. }
  813. return childTrans.gameObject;
  814. }
  815. }