ResourcesHelper.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ETModel
  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. string[] ss = info.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
  34. return ss;
  35. }
  36. public static void CollectDependencies(List<string> parents, string assetBundleName, Dictionary<string, int> info)
  37. {
  38. parents.Add(assetBundleName);
  39. string[] deps = GetDependencies(assetBundleName);
  40. foreach (string parent in parents)
  41. {
  42. if (!info.ContainsKey(parent))
  43. {
  44. info[parent] = 0;
  45. }
  46. info[parent] += deps.Length;
  47. }
  48. foreach (string dep in deps)
  49. {
  50. if (parents.Contains(dep))
  51. {
  52. throw new Exception($"包有循环依赖,请重新标记: {assetBundleName} {dep}");
  53. }
  54. CollectDependencies(parents, dep, info);
  55. }
  56. parents.RemoveAt(parents.Count - 1);
  57. }
  58. }
  59. }