using System; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace KairoEngine.Core { public static class Utilities { public static string RandomString(int length = 8) { var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var stringChars = new char[length]; var random = new System.Random(); for (int i = 0; i < stringChars.Length; i++) { stringChars[i] = chars[random.Next(chars.Length)]; } var finalString = new string(stringChars); return finalString; } public static string TimeToString(float time) { string t = ""; if(time < 1) { int miliseconds = (int)(time * 1000); return $"{miliseconds} ms"; } int minutes = (int)time / 60; int hours = (int)minutes / 60; minutes = (int)minutes % 60; int seconds = (int)time % 60; if(hours > 1) t += hours.ToString() + " hours "; else if(hours == 1) t += hours.ToString() + " hour "; if(minutes > 1) t += minutes.ToString() + " minutes "; else if(minutes == 1) t += minutes.ToString() + " minute "; if(seconds == 1) t += seconds.ToString() + " second"; else t += seconds.ToString() + " seconds"; return t; } public static float Round(float value, int digits = 2) { float mult = Mathf.Pow(10.0f, (float)digits); return Mathf.Round(value * mult) / mult; } public static Stream Serialize(System.Object source) { IFormatter formater = new BinaryFormatter(); Stream stream = new MemoryStream(); formater.Serialize(stream, source); return stream; } public static T Deserialze(Stream stream) { IFormatter formatter = new BinaryFormatter(); stream.Position = 0; return (T)formatter.Deserialize(stream); } public static T Clone(System.Object source) { return Deserialze(Serialize(source)); } public static string FindFileByName(string fileName) { string[] res = System.IO.Directory.GetFiles(Application.dataPath + "/../", fileName, System.IO.SearchOption.AllDirectories); if (res.Length == 0) { Debug.LogError("error message ...."); return fileName; } string path = res[0].Replace("\\", "/"); return path; } } }