Utilities.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Runtime.Serialization;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using UnityEngine;
  8. namespace KairoEngine.Core
  9. {
  10. public static class Utilities
  11. {
  12. public static string RandomString(int length = 8)
  13. {
  14. var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  15. var stringChars = new char[length];
  16. var random = new System.Random();
  17. for (int i = 0; i < stringChars.Length; i++)
  18. {
  19. stringChars[i] = chars[random.Next(chars.Length)];
  20. }
  21. var finalString = new string(stringChars);
  22. return finalString;
  23. }
  24. public static string TimeToString(float time)
  25. {
  26. string t = "";
  27. int minutes = (int)time / 60;
  28. int hours = (int)minutes / 60;
  29. minutes = (int)minutes % 60;
  30. int seconds = (int)time % 60;
  31. if(hours > 1) t += hours.ToString() + " hours ";
  32. else if(hours == 1) t += hours.ToString() + " hour ";
  33. if(minutes > 1) t += minutes.ToString() + " minutes ";
  34. else if(minutes == 1) t += minutes.ToString() + " minute ";
  35. if(seconds == 1) t += seconds.ToString() + " second";
  36. else t += seconds.ToString() + " seconds";
  37. return t;
  38. }
  39. public static float Round(float value, int digits = 2)
  40. {
  41. float mult = Mathf.Pow(10.0f, (float)digits);
  42. return Mathf.Round(value * mult) / mult;
  43. }
  44. public static Stream Serialize(System.Object source)
  45. {
  46. IFormatter formater = new BinaryFormatter();
  47. Stream stream = new MemoryStream();
  48. formater.Serialize(stream, source);
  49. return stream;
  50. }
  51. public static T Deserialze<T>(Stream stream)
  52. {
  53. IFormatter formatter = new BinaryFormatter();
  54. stream.Position = 0;
  55. return (T)formatter.Deserialize(stream);
  56. }
  57. public static T Clone<T>(System.Object source)
  58. {
  59. return Deserialze<T>(Serialize(source));
  60. }
  61. }
  62. }