123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using OpenCVForUnity.CoreModule;
- using OpenCVForUnity.ImgcodecsModule;
- using OpenCVForUnity.ImgprocModule;
- using OpenCVForUnity.UnityUtils;
- using UnityEditor;
- using System.Text;
- using System.Web;
- using System;
- using System.IO;
- using System.Linq;
- public class GameUtility
- {
-
-
-
- public static Transform FindDeepChild(GameObject _target, string _childName)
- {
- Transform resultTrs = null;
- resultTrs = _target.transform.Find(_childName);
- if (resultTrs == null)
- {
- foreach (Transform trs in _target.transform)
- {
- resultTrs = GameUtility.FindDeepChild(trs.gameObject, _childName);
- if (resultTrs != null)
- return resultTrs;
- }
- }
- return resultTrs;
- }
-
-
-
- public static T FindDeepChild<T>(GameObject _target, string _childName) where T : Component
- {
- Transform resultTrs = GameUtility.FindDeepChild(_target, _childName);
- if (resultTrs != null)
- return resultTrs.gameObject.GetComponent<T>();
- return (T)((object)null);
- }
-
-
-
-
- public static void AddChildToTarget(Transform target, Transform child)
- {
-
- child.SetParent(target, false);
- child.localScale = Vector3.one;
- child.localPosition = Vector3.zero;
- child.localEulerAngles = Vector3.zero;
- ChangeChildLayer(child, target.gameObject.layer);
- }
-
-
-
-
- public static void ChangeChildLayer(Transform t, int layer)
- {
- t.gameObject.layer = layer;
- for (int i = 0; i < t.childCount; ++i)
- {
- Transform child = t.GetChild(i);
- child.gameObject.layer = layer;
- ChangeChildLayer(child, layer);
- }
- }
-
- public static void DeleteAllGo(Transform go)
- {
- List<GameObject> DeleteList = new List<GameObject>();
- for (int i = 0; i < go.childCount; i++)
- {
- DeleteList.Add(go.GetChild(i).gameObject);
- }
- for (int i = 0; i < DeleteList.Count; i++)
- {
- GameObject.DestroyImmediate(DeleteList[i],true);
- }
- }
-
- public static void DeleteSingleGo(Transform go, int index)
- {
- for (int i = 0; i < go.childCount; i++)
- {
- if (i == index)
- GameObject.Destroy(go.GetChild(i).gameObject);
- }
- }
- private static Texture2D _tex;
-
-
-
-
-
-
- public static Mat zoomMatByOpenCV(string path, double zoom)
- {
-
- Mat mat = filePathToMat(path);
-
-
-
- Size size = new Size(mat.cols() * zoom, mat.rows() * zoom);
- Mat zoomMat = new Mat(size, CvType.CV_8UC4);
- Imgproc.resize(mat, zoomMat, size);
-
- Mat imgRGBMat = new Mat(zoomMat.cols(), zoomMat.rows(), CvType.CV_8UC4);
- Imgproc.cvtColor(zoomMat, imgRGBMat, Imgproc.COLOR_BGR2RGB);
- zoomMat.release();
- mat.release();
- return imgRGBMat;
-
- }
- public event Action RefreshEvent;
-
-
-
-
-
-
- public static Mat zoomByteByOpenCV(string path, double zoom,bool isZD=false,bool issuolve=false)
- {
- MatOfByte matbytes = filePathToMatByte(path);
- Mat mat = Imgcodecs.imdecode(matbytes, -1);
- if (isZD)
- {
- zoom = 1;
- float sint = 2048f;
- if(issuolve)
- {
- sint = 256f;
- }
- if (mat.cols() > sint)
- {
- zoom = (float)(sint / mat.cols());
- }
- if (mat.rows() * zoom > sint)
- {
- zoom = (float)(sint / (float)(mat.rows() * zoom));
- }
- }
-
-
-
- Size size = new Size(mat.cols() * zoom, mat.rows() * zoom);
- Mat zoomMat = new Mat(size, CvType.CV_8UC4);
- Imgproc.resize(mat, zoomMat, size);
-
- Mat imgRGBMat = new Mat(zoomMat.cols(), zoomMat.rows(), CvType.CV_8UC4);
- Imgproc.cvtColor(zoomMat, imgRGBMat, Imgproc.COLOR_BGR2RGB);
-
- zoomMat.release();
- mat.release();
- matbytes.release();
- return imgRGBMat;
-
- }
-
-
-
-
-
-
- public static Mat zoomByteByOpenCV(byte[] bytedata, double zoom)
- {
- MatOfByte matbytes = new MatOfByte(bytedata);
- if (matbytes.nativeObj == IntPtr.Zero)
- {
- return null;
- }
- Mat mat = Imgcodecs.imdecode(matbytes, -1);
-
-
-
-
- Size size = new Size(mat.cols() * zoom, mat.rows() * zoom);
- Mat zoomMat = new Mat(size, CvType.CV_8UC4);
- Imgproc.resize(mat, zoomMat, size);
-
- Mat imgRGBMat = new Mat(zoomMat.cols(), zoomMat.rows(), CvType.CV_8UC4);
- Imgproc.cvtColor(zoomMat, imgRGBMat, Imgproc.COLOR_BGR2RGB);
- zoomMat.release();
- mat.release();
- matbytes.release();
-
- return imgRGBMat;
-
- }
-
-
-
-
-
-
- public static Mat zoomMatOfByteByOpenCV(MatOfByte matbytes, double zoom)
- {
- Mat mat = Imgcodecs.imdecode(matbytes, -1);
- zoom = 1;
- float sint = 256f;
- if (mat.cols() > sint)
- {
- zoom = (float)(sint / mat.cols());
- }
- if (mat.rows() * zoom > sint)
- {
- zoom = (float)(sint / (float)(mat.rows() * zoom));
- }
-
-
-
- Size size = new Size(mat.cols() * zoom, mat.rows() * zoom);
- Mat zoomMat = new Mat(size, CvType.CV_8UC4);
- Imgproc.resize(mat, zoomMat, size);
-
- Mat imgRGBMat = new Mat(zoomMat.cols(), zoomMat.rows(), CvType.CV_8UC4);
- Imgproc.cvtColor(zoomMat, imgRGBMat, Imgproc.COLOR_BGR2RGB);
- zoomMat.release();
- mat.release();
- matbytes.release();
- return imgRGBMat;
- }
-
-
-
-
-
- public static Mat filePathToMat(string path)
- {
-
-
- return Imgcodecs.imread(path);
- }
- public static MatOfByte filePathToMatByte(string path)
- {
- MatOfByte matOfByte = new MatOfByte(LoadSpriteByIOByte(path));
-
- return matOfByte;
- }
-
-
-
-
-
-
- public static Texture2D TextureByMat(Mat imgRGBMat)
- {
-
-
- _tex = new Texture2D(imgRGBMat.cols(), imgRGBMat.rows(), TextureFormat.RGB24, false);
- Utils.fastMatToTexture2D(imgRGBMat, _tex);
- return _tex;
- }
-
-
-
-
-
-
-
- public static Texture2D Resize(Texture2D source, int newWidth, int newHeight)
- {
- source.filterMode = FilterMode.Point;
- RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight);
- rt.filterMode = FilterMode.Point;
- RenderTexture.active = rt;
- Graphics.Blit(source, rt);
- var nTex = new Texture2D(newWidth, newHeight);
- nTex.ReadPixels(new UnityEngine.Rect(0, 0, newWidth, newHeight), 0, 0);
- nTex.Apply();
- RenderTexture.active = null;
- return nTex;
- }
-
-
-
-
-
- public Texture2D LoadSpriteByIO(string fileurl)
- {
- double startTime = (double)Time.time;
-
- FileStream fileStream = new FileStream(fileurl, FileMode.Open, FileAccess.Read);
- fileStream.Seek(0, SeekOrigin.Begin);
-
- byte[] bytes = new byte[fileStream.Length];
-
- fileStream.Read(bytes, 0, (int)fileStream.Length);
-
- fileStream.Close();
- fileStream.Dispose();
- fileStream = null;
-
- int width = 300;
- int height = 372;
- Texture2D texture2D = new Texture2D(width, height);
- texture2D.LoadImage(bytes);
-
- return texture2D;
- }
-
-
-
-
-
- public static byte[] GetTextureToByte(Texture2D temp)
- {
-
- byte[] photoByte = temp.EncodeToPNG();
- return photoByte;
- }
-
-
-
-
-
- public static byte[] LoadSpriteByIOByte(string fileurl)
- {
-
- FileStream fileStream = new FileStream(fileurl, FileMode.Open, FileAccess.Read);
- fileStream.Seek(0, SeekOrigin.Begin);
-
- byte[] bytes = new byte[fileStream.Length];
-
- fileStream.Read(bytes, 0, (int)fileStream.Length);
-
- fileStream.Close();
- fileStream.Dispose();
- fileStream = null;
- return bytes;
- }
-
-
-
-
- private static void OrderSortAsFolderName(ref DirectoryInfo[] dirs)
- {
- Array.Sort(dirs, delegate (DirectoryInfo x, DirectoryInfo y) { return x.Name.CompareTo(y.Name); });
- }
-
-
-
-
- private static void ReverseSortAsFolderName(ref DirectoryInfo[] dirs)
- {
- Array.Sort(dirs, delegate (DirectoryInfo x, DirectoryInfo y) { return y.Name.CompareTo(x.Name); });
- }
-
-
-
-
- private static void OrderSortAsFolderCreationTime(ref DirectoryInfo[] dirs)
- {
- Array.Sort(dirs, delegate (DirectoryInfo x, DirectoryInfo y) { return x.CreationTime.CompareTo(y.CreationTime); });
- }
-
-
-
-
- private static void ReverseSortAsFolderCreationTime(ref DirectoryInfo[] dirs)
- {
- Array.Sort(dirs, delegate (DirectoryInfo x, DirectoryInfo y) { return y.CreationTime.CompareTo(x.CreationTime); });
- }
-
-
-
-
- private static void OrderSortAsFolderLastWriteTime(ref DirectoryInfo[] dirs)
- {
- Array.Sort(dirs, delegate (DirectoryInfo x, DirectoryInfo y) { return x.CreationTime.CompareTo(y.LastWriteTime); });
- }
-
-
-
-
- private static void ReverseSortAsFolderLastWriteTime(ref DirectoryInfo[] dirs)
- {
- Array.Sort(dirs, delegate (DirectoryInfo x, DirectoryInfo y) { return y.CreationTime.CompareTo(x.LastWriteTime); });
- }
- static double unit = 1024;
-
-
-
-
-
-
-
-
-
-
-
- public static string GetLength(long lengthOfDocument)
- {
- if (lengthOfDocument < unit)
- return string.Format(lengthOfDocument.ToString("f2") + 'B');
- else if (lengthOfDocument > unit && lengthOfDocument <= Math.Pow(unit, 2))
- return string.Format((lengthOfDocument / unit).ToString("f2") + "KB");
- else if (lengthOfDocument > Math.Pow(unit, 2) && lengthOfDocument <= Math.Pow(unit, 3))
- return string.Format((lengthOfDocument / unit / unit).ToString("f2") + "MB");
- else
- return string.Format((lengthOfDocument / unit / unit / unit).ToString("f2") + "GB");
- }
-
-
-
-
-
-
-
-
-
- public static string GetNumFormat(long lengthOfDocument)
- {
- if (lengthOfDocument < 1000)
- return string.Format(lengthOfDocument.ToString());
- else if (lengthOfDocument >= 1000 && lengthOfDocument < Math.Pow(1000, 2))
- {
- return string.Format((lengthOfDocument / 1000).ToString() + "K");
- }
-
- else if (lengthOfDocument >= Math.Pow(1000, 2) && lengthOfDocument < Math.Pow(1000, 3))
- return string.Format((lengthOfDocument / 1000 / 1000).ToString() + "M");
- else
- return string.Format((lengthOfDocument / 1000 / 1000 / 1000).ToString() + "B");
- }
- public static string GetVideoTime(long time)
- {
- string timeStr;
- if (time < 60)
- {
- timeStr = string.Format("00:{0:00}", time % 60);
- }
- else if (time < 3600)
- {
- timeStr = string.Format("{0}:", time / 60) + string.Format("{0}", time % 60);
- }
- else
- {
- timeStr = string.Format("{0}:", time / 3600) + string.Format("{0}:", (time % 3600) / 60) ;
- }
- return timeStr;
- }
- public static string formatLongToTimeStr(long l)
- {
-
- String str = "";
- int hour = 0;
- int minute = 0;
- int second = 0;
- second = (int)l / 1000;
- if (second > 60)
- {
- minute = second / 60;
- second = second % 60;
- }
- if (minute > 60)
- {
- hour = minute / 60;
- minute = minute % 60;
- }
- return (hour+ ":" + minute+ ":"
- + second );
- }
-
- public static string StampToDateTime(string timeStamp)
- {
- DateTime startTime = System.TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- DateTime dt = startTime.AddSeconds(double.Parse(timeStamp));
- return dt.ToString("yyyy/MM/dd");
- }
- }
|