AddressableAssetFoldExt.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using XGame.Framework.Asset;
  2. using XGame.Framework.Asset.Addressable.Data;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace XGame.Editor.Asset.Addressable
  6. {
  7. public static class AddressableAssetFoldExt
  8. {
  9. /// <summary>
  10. /// 按文件夹结构加入一个文件
  11. /// </summary>
  12. /// <param name="asssetFold"></param>
  13. /// <param name="assetInfo"></param>
  14. /// <param name="onApply"></param>
  15. internal static void AddFileWithFold(this AddressableAssetFold asssetFold, AssetInfo assetInfo, System.Func<ApplyEvnetContext, bool> onApply)
  16. {
  17. string path = assetInfo.GetAssetPath();
  18. char separator = '/';
  19. var names = path.Split(separator);
  20. //去掉Assets文件夹和最后的文件名
  21. if (names.Length <= 2)
  22. {
  23. Debug.LogError($"AddFileWithFold Error: Asset is in the wrong path. {assetInfo}");
  24. return;
  25. }
  26. var parent = asssetFold;
  27. StringBuilder builder = new StringBuilder();
  28. builder.Append(names[0]);
  29. for (int i = 1; i < names.Length - 1; i++)
  30. {
  31. builder.Append(separator);
  32. builder.Append(names[i]);
  33. var foldPath = builder.ToString();
  34. var fold = parent.GetChild(foldPath);
  35. if (fold == null)
  36. {
  37. fold = new AddressableAssetFold(foldPath);
  38. parent.AddChild(fold);
  39. }
  40. parent = (AddressableAssetFold)fold;
  41. }
  42. if (parent.Contains(path))
  43. {
  44. Debug.LogError($"AddFileWithFold Repeat. {assetInfo}");
  45. return;
  46. }
  47. var file = new AddressableAssetFile(path, assetInfo)
  48. {
  49. IsFoldout = true
  50. };
  51. file.OnApply += onApply;
  52. parent.AddChild(file);
  53. //Debug.Log($"AddFileWithFold Path: {path}");
  54. }
  55. /// <summary>
  56. /// 加入一个文件
  57. /// </summary>
  58. /// <param name="asssetFold"></param>
  59. /// <param name="assetInfo"></param>
  60. /// <param name="onApply"></param>
  61. internal static void AddFile(this AddressableAssetFold asssetFold, AssetInfo assetInfo, System.Func<ApplyEvnetContext, bool> onApply, FileSign fileSign)
  62. {
  63. string path = assetInfo.GetAssetPath();
  64. if (string.IsNullOrEmpty(path))
  65. {
  66. Debug.LogWarning($"AddFile {assetInfo}. But can't find assetPath. ");
  67. path = assetInfo.addressableName;
  68. }
  69. var file = new AddressableAssetFile(path, assetInfo)
  70. {
  71. IsFoldout = true,
  72. Sign = fileSign
  73. };
  74. file.OnApply += onApply;
  75. asssetFold.AddChild(file);
  76. //Debug.Log($"AddFile Path: {path}");
  77. }
  78. /// <summary>
  79. /// 加入失效的文件
  80. /// </summary>
  81. /// <param name="asssetFold"></param>
  82. /// <param name="assetInfo"></param>
  83. /// <param name="onRemove"></param>
  84. public static void AddInvalidFile(this AddressableAssetFold asssetFold, AssetInfo assetInfo,
  85. System.Action<string> onRemove)
  86. {
  87. var path = assetInfo.GetAssetPath();
  88. if (string.IsNullOrEmpty(path))
  89. {
  90. //Debug.LogWarning($"{assetInfo} can't find assetPath. ");
  91. path = assetInfo.addressableName;
  92. }
  93. var file = new AddressableAssetFile(path, assetInfo)
  94. {
  95. Sign = FileSign.Invalid
  96. };
  97. if (onRemove != null)
  98. {
  99. file.OnRemove += onRemove;
  100. }
  101. asssetFold.AddChild(file);
  102. //Debug.Log($"AddInvalidFile. {assetInfo}");
  103. }
  104. }
  105. }