EChartsExporter.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using XGame.Editor.Asset;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using XGame.Framework.Json;
  6. namespace XGame.Editor.Build.AssetBundles
  7. {
  8. internal static class EChartsExporter
  9. {
  10. public struct NodeCategory
  11. {
  12. public string name;
  13. public string keyword;
  14. }
  15. public struct AbNode
  16. {
  17. public string name;
  18. public int value;
  19. public int category;
  20. }
  21. public struct NodeLink
  22. {
  23. public int source;
  24. public int target;
  25. }
  26. public class AbNodes
  27. {
  28. public string type;
  29. public NodeCategory[] categories;
  30. public AbNode[] nodes;
  31. public NodeLink[] links;
  32. }
  33. [UnityEditor.MenuItem("Build/Assetbundle分组/导出配置(ECharts)")]
  34. public static void GenAbNodes()
  35. {
  36. var outputPath = $"{PathDefine.ExtAssetsPath}/AssetbundleNodes.json";
  37. var manifest = AssetBundleUtils.LoadManifest();
  38. if (manifest == null || manifest.bundleInfos == null)
  39. return;
  40. var bundleInfos = manifest.bundleInfos;
  41. var task = new AssetBundleGroupsTask(string.Empty, AssetBundleGroupsTask.AbGroupMode.Dependencies);
  42. if (!task.BundleGroup(bundleInfos, out var bundleGroupMap))
  43. {
  44. return;
  45. }
  46. var nodes = new List<AbNode>();
  47. var links = new List<NodeLink>();
  48. var bundleIndexMap = new Dictionary<uint, int>();
  49. var groupMax = 0;
  50. for (var i = 0; i < bundleInfos.Length; i++)
  51. {
  52. var bundleInfo = bundleInfos[i];
  53. var groupId = bundleGroupMap[bundleInfo.bundleId];
  54. if (groupId > groupMax)
  55. groupMax = groupId;
  56. var node = new AbNode()
  57. {
  58. name = bundleInfo.bundleId.ToString(),
  59. value = groupId,
  60. category = groupId,
  61. };
  62. nodes.Add(node);
  63. bundleIndexMap.Add(bundleInfo.bundleId, i);
  64. }
  65. var dependencies = new List<uint>();
  66. foreach (var bundleInfo in bundleInfos)
  67. {
  68. if (bundleInfo.DependenciesCount > 0)
  69. {
  70. var sourceIdx = bundleIndexMap[bundleInfo.bundleId];
  71. dependencies.AddRange(bundleInfo.dependencies);
  72. foreach (var depId in bundleInfo.dependencies)
  73. {
  74. var targetIdx = bundleIndexMap[depId];
  75. var tarBundle = bundleInfos[targetIdx];
  76. for (int i = dependencies.Count - 1; i >= 0; i--)
  77. {
  78. var compareId = dependencies[i];
  79. if (compareId == depId)
  80. continue;
  81. if (tarBundle.DependenciesCount > 0 && Array.IndexOf(tarBundle.dependencies, compareId) >= 0)
  82. {
  83. dependencies.RemoveAt(i);
  84. }
  85. }
  86. }
  87. foreach (var dep in dependencies)
  88. {
  89. var link = new NodeLink()
  90. {
  91. source = sourceIdx,
  92. target = bundleIndexMap[dep]
  93. };
  94. links.Add(link);
  95. }
  96. dependencies.Clear();
  97. }
  98. }
  99. //导出
  100. var categories = new NodeCategory[groupMax + 1];
  101. for (var i = 0; i < categories.Length; i++)
  102. {
  103. categories[i] = new NodeCategory()
  104. {
  105. name = i.ToString()
  106. };
  107. }
  108. var abNodes = new AbNodes()
  109. {
  110. type = "force",
  111. categories = categories,
  112. nodes = nodes.ToArray(),
  113. links = links.ToArray()
  114. };
  115. var text = XJson.ToJson(abNodes);
  116. File.WriteAllText(outputPath, text, System.Text.Encoding.UTF8);
  117. BuildLog.Log($"GenAssetbundleNodes Path:{outputPath}");
  118. }
  119. }
  120. }