A Unity runtime inspection plugin for fun
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

155 行
5.1KB

  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. namespace Dissonance.Inspector
  8. {
  9. public static class AssetInspector
  10. {
  11. private static AssetBundle[] allBundles;
  12. public static bool Init()
  13. {
  14. allBundles = AssetBundle.GetAllLoadedAssetBundles().ToArray();
  15. string[] allBundleNames = new string[allBundles.Length];
  16. for (int i = 0; i < allBundles.Length; i++)
  17. {
  18. allBundleNames[i] = allBundles[i].name;
  19. }
  20. Debug.Log($"Dissonance: Found AssetBundle {string.Join(", ", allBundleNames)}");
  21. return allBundles != null;
  22. }
  23. public static void Log<T>() where T : UnityEngine.Object
  24. {
  25. Instance[] instances = Inspect<T>();
  26. string[] filelines = new string[instances.Length];
  27. for (int i = 0; i < instances.Length; i++)
  28. {
  29. filelines[i] = instances[i].ToString() + ",";
  30. }
  31. Debug.Log("AssetInspector Log dump\n[" + string.Join(",\n", filelines) + "]");
  32. }
  33. public static void Log<T>(AssetBundle assetBundle) where T : UnityEngine.Object
  34. {
  35. Instance[] instances = Inspect<T>(assetBundle);
  36. string[] filelines = new string[instances.Length];
  37. for (int i = 0; i < instances.Length; i++)
  38. {
  39. filelines[i] = instances[i].ToString() + ",";
  40. }
  41. Debug.Log("AssetInspector Log dump\n[" + string.Join(",\n", filelines) + "]");
  42. }
  43. public static void LogNames<T>() where T : UnityEngine.Object
  44. {
  45. Debug.Log(string.Join("\n", InspectNames<T>()));
  46. }
  47. public static void LogNames<T>(AssetBundle assetBundle) where T : UnityEngine.Object
  48. {
  49. Debug.Log(string.Join("\n", InspectNames<T>(assetBundle)));
  50. }
  51. public static void LogAll()
  52. {
  53. Log<UnityEngine.Object>();
  54. }
  55. public static void LogNamesAll()
  56. {
  57. LogNames<UnityEngine.Object>();
  58. }
  59. public static void Dump<T>(string filename) where T : UnityEngine.Object
  60. {
  61. Instance[] instances = Inspect<T>();
  62. string[] filelines = new string[instances.Length];
  63. for (int i = 0; i < instances.Length; i++)
  64. {
  65. filelines[i] = instances[i].ToString() + ",";
  66. }
  67. File.WriteAllLines(filename, filelines);
  68. }
  69. public static void Dump<T>(string filename, AssetBundle assetBundle) where T : UnityEngine.Object
  70. {
  71. Instance[] instances = Inspect<T>(assetBundle);
  72. string[] filelines = new string[instances.Length];
  73. for (int i = 0; i < instances.Length; i++)
  74. {
  75. filelines[i] = instances[i].ToString() + ",";
  76. }
  77. File.WriteAllLines(filename, filelines);
  78. }
  79. public static void DumpNames<T>(string filename) where T : UnityEngine.Object
  80. {
  81. File.WriteAllLines(filename, InspectNames<T>());
  82. }
  83. public static void DumpNames<T>(string filename, AssetBundle assetBundle) where T : UnityEngine.Object
  84. {
  85. File.WriteAllLines(filename, InspectNames<T>(assetBundle));
  86. }
  87. public static void DumpNamesAll(string filename)
  88. {
  89. DumpNames<UnityEngine.Object>(filename);
  90. }
  91. public static void DumpNamesAll(string filename, AssetBundle assetBundle)
  92. {
  93. DumpNames<UnityEngine.Object>(filename, assetBundle);
  94. }
  95. public static Instance[] Inspect<T>() where T : UnityEngine.Object
  96. {
  97. if (allBundles == null) Init();
  98. List<Instance> allInstances = new List<Instance>();
  99. foreach (var b in allBundles)
  100. {
  101. allInstances.AddRange(Inspect<T>(b));
  102. }
  103. return allInstances.ToArray();
  104. }
  105. public static Instance[] Inspect<T>(AssetBundle assetBundle) where T : UnityEngine.Object
  106. {
  107. List<Instance> allInstances = new List<Instance>();
  108. UnityEngine.Object[] assets = assetBundle.LoadAllAssets<T>();
  109. for (int i = 0; i < assets.Length; i++)
  110. {
  111. allInstances.Add(new Instance(assets[i]));
  112. }
  113. return allInstances.ToArray();
  114. }
  115. public static string[] InspectNames<T>() where T : UnityEngine.Object
  116. {
  117. if (allBundles == null) Init();
  118. List<string> allNames = new List<string>();
  119. foreach (var b in allBundles)
  120. {
  121. allNames.AddRange(InspectNames<T>(b));
  122. }
  123. return allNames.ToArray();
  124. }
  125. public static string[] InspectNames<T>(AssetBundle assetBundle) where T : UnityEngine.Object
  126. {
  127. List<string> allNames = new List<string>();
  128. UnityEngine.Object[] assets = assetBundle.LoadAllAssets<T>();
  129. for (int i = 0; i < assets.Length; i++)
  130. {
  131. allNames.Add(assets[i].name);
  132. }
  133. return allNames.ToArray();
  134. }
  135. }
  136. }