Utility.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.IO;
  2. using UnityEngine;
  3. namespace VEngine
  4. {
  5. public static class Utility
  6. {
  7. public static string buildPath = "Bundles";
  8. //unity原生build出来的AB资源文件夹名称
  9. public static string BuildlesUnityFolder = "BuildlesUnity";
  10. public const string unsupportedPlatform = "Unsupported";
  11. private static readonly double[] byteUnits =
  12. {
  13. 1073741824.0, 1048576.0, 1024.0, 1
  14. };
  15. private static readonly string[] byteUnitsNames =
  16. {
  17. "GB", "MB", "KB", "B"
  18. };
  19. public static string GetPlatformName()
  20. {
  21. if (Application.platform == RuntimePlatform.Android) return "Android";
  22. if (Application.platform == RuntimePlatform.WindowsPlayer) return "Windows";
  23. if (Application.platform == RuntimePlatform.IPhonePlayer) return "iOS";
  24. return Application.platform == RuntimePlatform.WebGLPlayer ? "WebGL" : unsupportedPlatform;
  25. }
  26. public static string FormatBytes(long bytes)
  27. {
  28. var size = "0 B";
  29. if (bytes == 0) return size;
  30. for (var index = 0; index < byteUnits.Length; index++)
  31. {
  32. var unit = byteUnits[index];
  33. if (bytes >= unit)
  34. {
  35. size = $"{bytes / unit:##.##} {byteUnitsNames[index]}";
  36. break;
  37. }
  38. }
  39. return size;
  40. }
  41. public static uint ComputeCRC32(Stream stream)
  42. {
  43. var crc32 = new CRC32();
  44. return crc32.Compute(stream);
  45. }
  46. public static uint ComputeCRC32(string filename)
  47. {
  48. if (!File.Exists(filename)) return 0;
  49. using (var stream = File.OpenRead(filename))
  50. {
  51. return ComputeCRC32(stream);
  52. }
  53. }
  54. }
  55. }