Utility.cs 5.1 KB

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