123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Ionic.Zlib;
- using UnityEngine;
- using ZXing;
- using ZXing.Common;
- using ZXing.QrCode;
- using ZXing.QrCode.Internal;
- namespace wf
- {
- public static class Utility
- {
- public static GameObject CreatePrefab(GameObject parent, string path, bool trans_reset = true)
- {
- GameObject gameObject = UnityEngine.Object.Instantiate(Resources.Load(path)) as GameObject;
- if (parent != null)
- {
- gameObject.transform.SetParent(parent.transform, false);
- }
- else
- {
- gameObject.transform.SetParent(null, false);
- }
- if (trans_reset)
- {
- gameObject.transform.localPosition = Vector3.zero;
- gameObject.transform.localScale = Vector3.one;
- }
- return gameObject;
- }
- public static Sprite CreateTextureSprite(string texturFileName)
- {
- if (!string.IsNullOrEmpty(texturFileName))
- {
- texturFileName = Path.ChangeExtension(texturFileName, ".tex");
- }
- if (!GameUty.FileSystem.IsExistentFile(texturFileName))
- {
- return null;
- }
- Texture2D texture2D = ImportCM.CreateTexture(texturFileName);
- Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
- sprite.name = texturFileName;
- return sprite;
- }
- public static Sprite CreateTextureSpriteFromImageFile(string pngFilePath)
- {
- if (string.IsNullOrEmpty(pngFilePath))
- {
- return null;
- }
- if (!File.Exists(pngFilePath))
- {
- return null;
- }
- Texture2D texture2D = null;
- try
- {
- byte[] array = File.ReadAllBytes(pngFilePath);
- if (array == null || array.Length == 0)
- {
- return null;
- }
- texture2D = new Texture2D(32, 32);
- texture2D.LoadImage(array);
- }
- catch (Exception ex)
- {
- Debug.LogError(ex.Message + "\n\n" + ex.StackTrace);
- return null;
- }
- if (texture2D == null || texture2D.width == 0 || texture2D.height == 0)
- {
- return null;
- }
- Sprite sprite = Sprite.Create(texture2D, new Rect(0f, 0f, (float)texture2D.width, (float)texture2D.height), default(Vector2));
- sprite.name = Path.GetFileName(pngFilePath);
- return sprite;
- }
- public static void ResetNGUI(UIGrid grid)
- {
- grid.Reposition();
- }
- public static void ResetNGUI(UITable table)
- {
- table.Reposition();
- }
- public static void ResetNGUI(UIScrollBar scroll_bar)
- {
- scroll_bar.value = 0f;
- }
- public static void ResetNGUI(UIScrollView scroll_view)
- {
- scroll_view.currentMomentum = Vector3.zero;
- scroll_view.Press(true);
- scroll_view.Press(false);
- scroll_view.ResetPosition();
- }
- public static void ResizeUILabelFontSize(UILabel label, int limitWidth)
- {
- int width = label.width;
- label.width = 0;
- label.MakePixelPerfect();
- if (label.width <= limitWidth)
- {
- label.width = width;
- return;
- }
- while (limitWidth < label.width && 1 <= label.fontSize)
- {
- label.fontSize--;
- label.width = 0;
- label.MakePixelPerfect();
- }
- }
- public static float VolumeToDecibel(float volume)
- {
- return 20f * Mathf.Log10(Mathf.Clamp(volume, 0.0001f, 1f));
- }
- public static float DecibelToVolume(float db)
- {
- float num = Mathf.Pow(10f, Mathf.Clamp(db, -80f, 0f) / 20f);
- return (num > 0.0001f) ? num : 0f;
- }
- public static string ConvertMoneyText(int num)
- {
- return string.Format("{0:#,##0}", num);
- }
- public static string ConvertMoneyText(long num)
- {
- return string.Format("{0:#,##0}", num);
- }
- public static bool IsNumericOnlyText(string text)
- {
- bool result = true;
- foreach (char c in text)
- {
- if ((c < '0' || c > '9') && c != ' ' && c != '-')
- {
- result = false;
- break;
- }
- }
- return result;
- }
- public static string GetTermLastWord(string termName)
- {
- if (string.IsNullOrEmpty(termName))
- {
- return string.Empty;
- }
- if (termName.IndexOf('/') != -1)
- {
- string[] array = termName.Split(new char[]
- {
- '/'
- });
- return array[array.Length - 1];
- }
- return termName;
- }
- public static byte[] ZlibCompresss(byte[] data)
- {
- return DeflateStream.CompressBuffer(data);
- }
- public static byte[] ZlibUncompress(byte[] data)
- {
- return DeflateStream.UncompressBuffer(data);
- }
- public static Texture2D CreateQRCodeTexture(string text, int width = 512, int height = 512)
- {
- QRCodeWriter qrcodeWriter = new QRCodeWriter();
- BitMatrix bitMatrix = qrcodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height, new Dictionary<EncodeHintType, object>
- {
- {
- EncodeHintType.CHARACTER_SET,
- "ISO-8859-1"
- },
- {
- EncodeHintType.ERROR_CORRECTION,
- ErrorCorrectionLevel.L
- }
- });
- int width2 = bitMatrix.Width;
- int height2 = bitMatrix.Height;
- Texture2D texture2D = new Texture2D(width2, height2, TextureFormat.ARGB32, false);
- for (int i = 0; i < height2; i++)
- {
- for (int j = 0; j < width2; j++)
- {
- texture2D.SetPixel(j, i, (!bitMatrix[j, i]) ? Color.white : Color.black);
- }
- }
- texture2D.Apply();
- return texture2D;
- }
- public static Texture2D CreateTexture2DFromRenderTexture(RenderTexture render_tex)
- {
- Texture2D texture2D = new Texture2D(render_tex.width, render_tex.height, TextureFormat.ARGB32, false);
- RenderTexture active = RenderTexture.active;
- RenderTexture.active = render_tex;
- texture2D.ReadPixels(new Rect(0f, 0f, (float)render_tex.width, (float)render_tex.height), 0, 0);
- texture2D.Apply();
- RenderTexture.active = active;
- return texture2D;
- }
- }
- }
|