ResourcesHelper.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using UnityEngine;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #endif
  9. namespace Model
  10. {
  11. public static class ResourcesHelper
  12. {
  13. public static string[] GetDependencies(string assetBundleName)
  14. {
  15. string[] dependencies = new string[0];
  16. if (!Define.IsAsync)
  17. {
  18. #if UNITY_EDITOR
  19. dependencies = AssetDatabase.GetAssetBundleDependencies(assetBundleName, true);
  20. #endif
  21. }
  22. else
  23. {
  24. dependencies = ResourcesComponent.AssetBundleManifestObject.GetAllDependencies(assetBundleName);
  25. }
  26. return dependencies;
  27. }
  28. public static string[] GetSortedDependencies(string assetBundleName)
  29. {
  30. Dictionary<string, int> info = new Dictionary<string, int>();
  31. List<string> parents = new List<string>();
  32. CollectDependencies(parents, assetBundleName, info);
  33. info.Remove(assetBundleName);
  34. string[] ss = info.OrderByDescending(x => x.Value).Select(x => x.Key).ToArray();
  35. return ss;
  36. }
  37. public static void CollectDependencies(List<string> parents, string assetBundleName, Dictionary<string, int> info)
  38. {
  39. parents.Add(assetBundleName);
  40. string[] deps = GetDependencies(assetBundleName);
  41. foreach (string parent in parents)
  42. {
  43. if (!info.ContainsKey(parent))
  44. {
  45. info[parent] = 0;
  46. }
  47. info[parent] += deps.Length;
  48. }
  49. foreach (string dep in deps)
  50. {
  51. if (parents.Contains(dep))
  52. {
  53. throw new Exception($"包有循环依赖,请重新标记: {assetBundleName} {dep}");
  54. }
  55. CollectDependencies(parents, dep, info);
  56. }
  57. parents.RemoveAt(parents.Count - 1);
  58. }
  59. }
  60. }