FileUtils.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using XGame.Framework.Define;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace XGame.Framework.Utils
  5. {
  6. public static class FileUtils
  7. {
  8. //public static string PlatformName
  9. //{
  10. // get
  11. // {
  12. // switch (Application.platform)
  13. // {
  14. // case RuntimePlatform.OSXEditor:
  15. // case RuntimePlatform.OSXPlayer:
  16. // return "Mac";
  17. // case RuntimePlatform.IPhonePlayer:
  18. // return "iOS";
  19. // case RuntimePlatform.Android:
  20. // return "Android";
  21. // //case RuntimePlatform.WindowsEditor:
  22. // //case RuntimePlatform.WindowsPlayer:
  23. // default:
  24. // return "Windows";
  25. // }
  26. // }
  27. //}
  28. /// <summary>
  29. /// 获取游戏包里的文件路径
  30. /// </summary>
  31. /// <param name="fileName"></param>
  32. /// <param name="includeUpdate"></param>
  33. /// <returns></returns>
  34. public static string GetFilePath(string fileName, bool includeUpdate = true)
  35. {
  36. //先取热更新的资源
  37. //var path = Path.Combine(UpdatePath, fileName);
  38. if (TryGetFilePath(fileName, includeUpdate, out var path))
  39. {
  40. return path;
  41. }
  42. //找不到再从内置资源里取
  43. return $"{PathDefine.WorkPath}/{fileName}";
  44. }
  45. /// <summary>
  46. /// 获取游戏包里的文件URL
  47. /// </summary>
  48. /// <param name="fileName">文件名</param>
  49. /// <param name="includeUpdate"></param>
  50. /// <returns></returns>
  51. public static string GetFileUrl(string fileName, bool includeUpdate = true)
  52. {
  53. //先取热更新的资源
  54. //string path = Path.Combine(UpdatePath, fileName);
  55. if (TryGetFilePath(fileName, includeUpdate, out var path))
  56. {
  57. return "file://" + path;
  58. }
  59. //找不到再从内置资源里取
  60. if (Application.platform == RuntimePlatform.Android)
  61. return $"jar:file://{PathDefine.WorkPath}/{fileName}";
  62. return $"file://{PathDefine.WorkPath}/{fileName}";
  63. }
  64. private static bool TryGetFilePath(string fileName, bool includeUpdate, out string path)
  65. {
  66. if (includeUpdate)
  67. {
  68. //先取热更新的资源
  69. path = $"{PathDefine.UpdatePath}/{fileName}";
  70. if (System.IO.File.Exists(path))
  71. {
  72. return true;
  73. }
  74. }
  75. else
  76. {
  77. path = string.Empty;
  78. }
  79. #if UNITY_EDITOR
  80. //编辑器下多做一步平台判断
  81. path = $"{PathDefine.WorkPath}/{fileName}";
  82. if (System.IO.File.Exists(path))
  83. {
  84. return true;
  85. }
  86. var platform = PlatformType.Windows;
  87. #if UNITY_ANDROID
  88. platform = PlatformType.Android;
  89. #elif UNITY_IOS
  90. platform = PlatformType.iOS;
  91. #elif UNITY_WEIXINMINIGAME
  92. platform = PlatformType.Weixin;
  93. #elif UNITY_WEBGL
  94. platform = PlatformType.WebGL;
  95. #endif
  96. path = $"{PathDefine.WorkPath}/{platform}/{fileName}";
  97. if (System.IO.File.Exists(path))
  98. {
  99. return true;
  100. }
  101. #endif
  102. return false;
  103. }
  104. public static string GetAssetBundlePath(string bundleName)
  105. {
  106. return GetFilePath(Path.Combine("AssetBundles", bundleName));
  107. }
  108. }
  109. }