ResourcesHelper.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 UnityEngine.Object Load(string path)
  14. {
  15. return Resources.Load(path);
  16. }
  17. public static string[] GetDependencies(string assetBundleName)
  18. {
  19. string[] dependencies = new string[0];
  20. if (!Define.IsAsync)
  21. {
  22. #if UNITY_EDITOR
  23. dependencies = AssetDatabase.GetAssetBundleDependencies(assetBundleName, true);
  24. #endif
  25. }
  26. else
  27. {
  28. dependencies = ResourcesComponent.AssetBundleManifestObject.GetAllDependencies(assetBundleName);
  29. }
  30. return dependencies;
  31. }
  32. public static string[] GetSortedDependencies(string assetBundleName)
  33. {
  34. Dictionary<string, int> info = new Dictionary<string, int>();
  35. List<string> parents = new List<string>();
  36. CollectDependencies(parents, assetBundleName, info);
  37. string[] ss = info.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
  38. return ss;
  39. }
  40. public static void CollectDependencies(List<string> parents, string assetBundleName, Dictionary<string, int> info)
  41. {
  42. parents.Add(assetBundleName);
  43. string[] deps = GetDependencies(assetBundleName);
  44. foreach (string parent in parents)
  45. {
  46. if (!info.ContainsKey(parent))
  47. {
  48. info[parent] = 0;
  49. }
  50. info[parent] += deps.Length;
  51. }
  52. foreach (string dep in deps)
  53. {
  54. if (parents.Contains(dep))
  55. {
  56. throw new Exception($"包有循环依赖,请重新标记: {assetBundleName} {dep}");
  57. }
  58. CollectDependencies(parents, dep, info);
  59. }
  60. parents.RemoveAt(parents.Count - 1);
  61. }
  62. }
  63. }