AssetBundleUtils.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using XGame.Editor.Asset;
  2. using XGame.Framework.Asset;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. using XGame.Framework.i18n;
  8. namespace XGame.Editor.Build.AssetBundles
  9. {
  10. internal static class AssetBundleUtils
  11. {
  12. /// <summary>
  13. /// 检测一个资源路径是否有效
  14. /// 用于判断指定Asset是否可以打包AssetBundle
  15. /// </summary>
  16. /// <param name="assetPath">使用Unity的相对路径</param>
  17. /// <returns></returns>
  18. public static bool IsValidPath(string assetPath)
  19. {
  20. if (string.IsNullOrEmpty(assetPath))
  21. {
  22. return false;
  23. }
  24. //if (!File.Exists(assetPath))
  25. //{
  26. // Debug.LogWarning($"File can't find. Path:{assetPath}");
  27. // return false;
  28. //}
  29. //assetPath = FileUtil.ToRelativePath(assetPath);
  30. if (FileUtil.IsFileIgnore(assetPath))
  31. {
  32. return false;
  33. }
  34. if (assetPath.StartsWith(PathDefine.ResAddressableRelative))
  35. {
  36. //在Addressable资源目录下
  37. return true;
  38. }
  39. if (AddressableHelper.IsI18nAssetPath(assetPath))
  40. {
  41. //在国际化资源目录下
  42. return true;
  43. }
  44. return false;
  45. }
  46. /// <summary>
  47. /// 检查Assetbundle内是否存在同名资源
  48. /// </summary>
  49. /// <param name="assetBundleBuilds"></param>
  50. /// <returns></returns>
  51. public static bool CheckAssetsNameRepeat(Dictionary<long, AssetBundleData> assetBundleBuilds)
  52. {
  53. bool result = true;
  54. Dictionary<string, string> assetPathMap = new Dictionary<string, string>();
  55. foreach (var bundleBuild in assetBundleBuilds)
  56. {
  57. assetPathMap.Clear();
  58. foreach (var assetPath in bundleBuild.Value.assetNames)
  59. {
  60. var fileName = Path.GetFileName(assetPath);
  61. if (assetPathMap.ContainsKey(fileName))
  62. {
  63. result = false;
  64. Debug.LogError($"AssetBundle存在重名资源。Bundle:{bundleBuild.Value.originBundleName} 重名资源1:{assetPathMap[fileName]} 重名资源2:{assetPath}");
  65. }
  66. else
  67. {
  68. assetPathMap.Add(fileName, assetPath);
  69. }
  70. }
  71. }
  72. return result;
  73. }
  74. ///// <summary>
  75. ///// 返回ResStatic目录下资源的bundleName
  76. ///// 只有打了AssetBundle的资源才有效
  77. ///// </summary>
  78. ///// <param name="assetPath"></param>
  79. ///// <returns></returns>
  80. //public static string GetRawBundleName(string assetPath)
  81. //{
  82. // var directoryRelative = FileUtil.ToRelativePath(Path.GetDirectoryName(assetPath)?.Replace("\\", "/"));
  83. // string bundleName;
  84. // var rawRelative = PathDefine.ResStaticRelative;
  85. // if (directoryRelative.Equals(rawRelative))
  86. // {
  87. // bundleName = PathDefine.ResStaticName.ToLower();
  88. // }
  89. // else if (directoryRelative.Contains(rawRelative))
  90. // {
  91. // bundleName = directoryRelative.Replace(PathDefine.ResStaticRelative + "/", "").Replace("/", "_").ToLower();
  92. // }
  93. // else
  94. // {
  95. // Debug.LogError($"文件路径错误,该文件不在{rawRelative}");
  96. // bundleName = string.Empty;
  97. // }
  98. // bundleName = $"{PathDefine.rawBundlePrefix}{bundleName}";
  99. // return bundleName;
  100. //}
  101. /// <summary>
  102. /// 返回指定资源的bundleName,只对未设置Packer的资源有效
  103. /// </summary>
  104. /// <param name="assetPath"></param>
  105. /// <returns></returns>
  106. public static string GetBundleName(string assetPath)
  107. {
  108. //var assetPath = FileUtil.ToRelativePath(filePath);
  109. var directoryRelative = FileUtil.ToRelativePath(Path.GetDirectoryName(assetPath).Replace("\\", "/"));
  110. string bundleName;
  111. var languageType = AddressableHelper.GetLanguageTypeByAssetPath(assetPath);
  112. if (languageType != LanguageType.NONE)
  113. { //国际化资源
  114. if (directoryRelative.EndsWith(PathDefine.ResAddressableName))
  115. {
  116. bundleName = $"{languageType}_{PathDefine.ResAddressableName}".ToLower();
  117. bundleName = $"{LanguageUtils.ToName(languageType)}/{bundleName}";
  118. }
  119. else
  120. {
  121. var offest = directoryRelative.IndexOf(PathDefine.ResAddressableName, StringComparison.Ordinal) + PathDefine.ResAddressableName.Length + 1;
  122. bundleName = directoryRelative.Substring(offest).Replace("/", "_");
  123. bundleName = $"{languageType}_{bundleName}".ToLower();
  124. bundleName = $"{LanguageUtils.ToName(languageType)}/{bundleName}";
  125. }
  126. }
  127. else if (directoryRelative.Equals(PathDefine.ResAddressableRelative))
  128. {//主目录
  129. bundleName = PathDefine.ResAddressableName.ToLower();
  130. }
  131. else
  132. {
  133. bundleName = directoryRelative.Replace(PathDefine.ResAddressableRelative + "/", "").Replace("/", "_").ToLower();
  134. }
  135. return bundleName;
  136. }
  137. /// <summary>
  138. /// 加载 KCAssetBundleManifest.asset
  139. /// </summary>
  140. /// <returns></returns>
  141. public static AssetBundleInfosSo LoadManifest()
  142. {
  143. return UnityEditor.AssetDatabase.LoadAssetAtPath<AssetBundleInfosSo>(PathDefine.BundleManifestPath);
  144. }
  145. }
  146. }