Utility.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using I2.Loc;
  5. using Ionic.Zlib;
  6. using UnityEngine;
  7. using ZXing;
  8. using ZXing.Common;
  9. using ZXing.QrCode;
  10. using ZXing.QrCode.Internal;
  11. namespace wf
  12. {
  13. public static class Utility
  14. {
  15. public static GameObject CreatePrefab(GameObject parent, string path, bool trans_reset = true)
  16. {
  17. GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load(path)) as GameObject;
  18. if (parent != null)
  19. {
  20. gameObject.transform.SetParent(parent.transform, false);
  21. }
  22. else
  23. {
  24. gameObject.transform.SetParent(null, false);
  25. }
  26. if (trans_reset)
  27. {
  28. gameObject.transform.localPosition = Vector3.zero;
  29. gameObject.transform.localScale = Vector3.one;
  30. }
  31. return gameObject;
  32. }
  33. public static string GetHierarchyPath(Transform parent, Transform target)
  34. {
  35. if (target == null)
  36. {
  37. return string.Empty;
  38. }
  39. if (parent == target)
  40. {
  41. return target.name;
  42. }
  43. string text = target.gameObject.name;
  44. Transform parent2 = target.parent;
  45. while (parent2 != null)
  46. {
  47. string name = parent2.name;
  48. if (parent2 == null && parent != null)
  49. {
  50. return string.Empty;
  51. }
  52. if (parent2 == parent)
  53. {
  54. break;
  55. }
  56. parent2 = parent2.parent;
  57. text = name + "/" + text;
  58. }
  59. return text;
  60. }
  61. public static Sprite CreateTextureSprite(string texturFileName)
  62. {
  63. if (!string.IsNullOrEmpty(texturFileName))
  64. {
  65. texturFileName = Path.ChangeExtension(texturFileName, ".tex");
  66. }
  67. if (!GameUty.FileSystem.IsExistentFile(texturFileName))
  68. {
  69. return null;
  70. }
  71. Texture2D texture2D = ImportCM.CreateTexture(texturFileName);
  72. Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
  73. sprite.name = texturFileName;
  74. return sprite;
  75. }
  76. public static Sprite CreateTextureSpriteFromImageFile(string pngFilePath)
  77. {
  78. if (string.IsNullOrEmpty(pngFilePath))
  79. {
  80. return null;
  81. }
  82. if (!File.Exists(pngFilePath))
  83. {
  84. return null;
  85. }
  86. Texture2D texture2D = null;
  87. try
  88. {
  89. byte[] array = File.ReadAllBytes(pngFilePath);
  90. if (array == null || array.Length == 0)
  91. {
  92. return null;
  93. }
  94. texture2D = new Texture2D(32, 32);
  95. texture2D.LoadImage(array);
  96. }
  97. catch (Exception ex)
  98. {
  99. Debug.LogError(ex.Message + "\n\n" + ex.StackTrace);
  100. return null;
  101. }
  102. if (texture2D == null || texture2D.width == 0 || texture2D.height == 0)
  103. {
  104. return null;
  105. }
  106. Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
  107. sprite.name = Path.GetFileName(pngFilePath);
  108. return sprite;
  109. }
  110. public static bool SetLocalizeTerm(Component obj, string term, bool forceApply = false)
  111. {
  112. return (Product.supportMultiLanguage || forceApply) && !(obj == null) && !string.IsNullOrEmpty(term) && Utility.SetLocalizeTerm(obj.GetComponent<Localize>(), term, forceApply);
  113. }
  114. public static bool SetLocalizeTerm(Localize localize, string term, bool forceApply = false)
  115. {
  116. if ((!Product.supportMultiLanguage && !forceApply) || localize == null || string.IsNullOrEmpty(term))
  117. {
  118. return false;
  119. }
  120. localize.SetTerm(term);
  121. return true;
  122. }
  123. public static void ResetNGUI(UIGrid grid)
  124. {
  125. grid.Reposition();
  126. }
  127. public static void ResetNGUI(UITable table)
  128. {
  129. table.Reposition();
  130. }
  131. public static void ResetNGUI(UIScrollBar scroll_bar)
  132. {
  133. scroll_bar.value = 0f;
  134. }
  135. public static void ResetNGUI(UIScrollView scroll_view)
  136. {
  137. scroll_view.currentMomentum = Vector3.zero;
  138. scroll_view.Press(true);
  139. scroll_view.Press(false);
  140. scroll_view.ResetPosition();
  141. }
  142. public static void ResizeUILabelFontSize(UILabel label, int limitWidth)
  143. {
  144. int width = label.width;
  145. label.width = 0;
  146. label.MakePixelPerfect();
  147. if (label.width <= limitWidth)
  148. {
  149. label.width = width;
  150. return;
  151. }
  152. while (limitWidth < label.width && 1 <= label.fontSize)
  153. {
  154. label.fontSize--;
  155. label.width = 0;
  156. label.MakePixelPerfect();
  157. }
  158. }
  159. public static float VolumeToDecibel(float volume)
  160. {
  161. return 20f * Mathf.Log10(Mathf.Clamp(volume, 0.0001f, 1f));
  162. }
  163. public static float DecibelToVolume(float db)
  164. {
  165. float num = Mathf.Pow(10f, Mathf.Clamp(db, -80f, 0f) / 20f);
  166. return (num > 0.0001f) ? num : 0f;
  167. }
  168. public static string ConvertMoneyText(int num)
  169. {
  170. return string.Format("{0:#,##0}", num);
  171. }
  172. public static string ConvertMoneyText(long num)
  173. {
  174. return string.Format("{0:#,##0}", num);
  175. }
  176. public static bool IsNumericOnlyText(string text)
  177. {
  178. bool result = true;
  179. foreach (char c in text)
  180. {
  181. if ((c < '0' || c > '9') && c != ' ' && c != '-')
  182. {
  183. result = false;
  184. break;
  185. }
  186. }
  187. return result;
  188. }
  189. public static string GetTermLastWord(string termName)
  190. {
  191. if (string.IsNullOrEmpty(termName))
  192. {
  193. return string.Empty;
  194. }
  195. if (termName.IndexOf('/') != -1)
  196. {
  197. string[] array = termName.Split(new char[]
  198. {
  199. '/'
  200. });
  201. return array[array.Length - 1];
  202. }
  203. return termName;
  204. }
  205. public static byte[] ZlibCompresss(byte[] data)
  206. {
  207. return DeflateStream.CompressBuffer(data);
  208. }
  209. public static byte[] ZlibUncompress(byte[] data)
  210. {
  211. return DeflateStream.UncompressBuffer(data);
  212. }
  213. public static Texture2D CreateQRCodeTexture(string text, int width = 512, int height = 512)
  214. {
  215. QRCodeWriter qrcodeWriter = new QRCodeWriter();
  216. BitMatrix bitMatrix = qrcodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, new Dictionary<EncodeHintType, object>
  217. {
  218. {
  219. EncodeHintType.CHARACTER_SET,
  220. "ISO-8859-1"
  221. },
  222. {
  223. EncodeHintType.ERROR_CORRECTION,
  224. ErrorCorrectionLevel.L
  225. }
  226. });
  227. int width2 = bitMatrix.Width;
  228. int height2 = bitMatrix.Height;
  229. Texture2D texture2D = new Texture2D(width2, height2, TextureFormat.ARGB32, false);
  230. for (int i = 0; i < height2; i++)
  231. {
  232. for (int j = 0; j < width2; j++)
  233. {
  234. texture2D.SetPixel(j, i, (!bitMatrix[j, i]) ? Color.white : Color.black);
  235. }
  236. }
  237. texture2D.Apply();
  238. return texture2D;
  239. }
  240. public static Texture2D CreateTexture2DFromRenderTexture(RenderTexture render_tex)
  241. {
  242. Texture2D texture2D = new Texture2D(render_tex.width, render_tex.height, TextureFormat.ARGB32, false);
  243. RenderTexture active = RenderTexture.active;
  244. RenderTexture.active = render_tex;
  245. texture2D.ReadPixels(new Rect(0f, 0f, (float)render_tex.width, (float)render_tex.height), 0, 0);
  246. texture2D.Apply();
  247. RenderTexture.active = active;
  248. return texture2D;
  249. }
  250. }
  251. }