AssetBankScanner.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Threading;
  6. using LLM.Editor.Helper;
  7. using UnityEditor;
  8. using UnityEditorInternal;
  9. using UnityEngine;
  10. namespace AssetBank.Editor
  11. {
  12. public class AssetBankScanner : EditorWindow
  13. {
  14. private List<string> folderToIgnore = new List<string>()
  15. {
  16. "AssetBank",
  17. "Library"
  18. };
  19. private List<string> fileExtensionToIgnore = new List<string>()
  20. {
  21. ".meta",
  22. ".dylib",
  23. ".bytes"
  24. };
  25. private List<string> typesToIgnore = new List<string>()
  26. {
  27. typeof(DefaultAsset).ToString()
  28. };
  29. private int currentTab = 0;
  30. private string[] tabs = new[] { "General" , "Settings"};
  31. private readonly string folderstoignore = "AssetBankScanner_FolderToIgnore";
  32. private readonly string extensiontoignore = "AssetBankScanner_FileExtensionToIgnore";
  33. private readonly string typesToignore = "AssetBankScanner_TypesToIgnore";
  34. private readonly string CACHE_FOLDER = "AssetBank";
  35. private string cachePath;
  36. private List<ReorderableList> prefLists = new();
  37. [MenuItem("Window/Asset Database Scanner")]
  38. public static void ShowWindow()
  39. {
  40. var window = GetWindow<AssetBankScanner>();
  41. window.titleContent = new GUIContent("Asset Database Scanner");
  42. window.Show();
  43. }
  44. private void OnEnable()
  45. {
  46. cachePath = Path.Join("Library", CACHE_FOLDER);
  47. LoadSettings();
  48. SetupLists();
  49. }
  50. private void SetupLists()
  51. {
  52. prefLists.Add(CreateReorderableList("Folders to ignore", folderToIgnore));
  53. prefLists.Add(CreateReorderableList("Files to ignore", fileExtensionToIgnore));
  54. prefLists.Add(CreateReorderableList("Types to ignore", typesToIgnore));
  55. }
  56. private void LoadSettings()
  57. {
  58. if (!EditorPrefs.HasKey(folderstoignore))
  59. {
  60. EditorPrefs.SetString(folderstoignore, String.Join(";",folderToIgnore));
  61. }
  62. if (!EditorPrefs.HasKey(extensiontoignore))
  63. {
  64. EditorPrefs.SetString(extensiontoignore, String.Join(";", fileExtensionToIgnore));
  65. }
  66. if (!EditorPrefs.HasKey(typesToignore))
  67. {
  68. EditorPrefs.SetString(typesToignore, String.Join(";",typesToIgnore));
  69. }
  70. folderToIgnore = EditorPrefs.GetString(folderstoignore, "").Split(';')
  71. .Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
  72. fileExtensionToIgnore = EditorPrefs.GetString(extensiontoignore, "").Split(';')
  73. .Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
  74. typesToIgnore = EditorPrefs.GetString(typesToignore, "").Split(';').Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
  75. }
  76. private void OnGUI()
  77. {
  78. EditorGUILayout.LabelField("Asset Bank Scanner", EditorStyles.boldLabel);
  79. GUILayout.Space(10);
  80. currentTab = GUILayout.Toolbar(currentTab, tabs);
  81. GUILayout.Space(10);
  82. switch (currentTab)
  83. {
  84. case 0: DrawScanner(); break;
  85. case 1: DrawSettings(); break;
  86. }
  87. }
  88. private void DrawSettings()
  89. {
  90. GUILayout.Label("Settings", EditorStyles.boldLabel);
  91. foreach (var prefList in prefLists)
  92. {
  93. prefList.DoLayoutList();
  94. EditorGUILayout.Space();
  95. }
  96. if (GUILayout.Button("Clear cache", EditorStyles.miniButton))
  97. {
  98. Directory.Delete(cachePath, true);
  99. }
  100. return;
  101. # region "Clear options"
  102. if (GUILayout.Button("Clear preferences", EditorStyles.miniButton))
  103. {
  104. prefLists.Clear();
  105. EditorPrefs.DeleteKey(folderstoignore);
  106. EditorPrefs.DeleteKey(extensiontoignore);
  107. EditorPrefs.DeleteKey(typesToignore);
  108. }
  109. #endregion
  110. }
  111. private ReorderableList CreateReorderableList(string title, List<string> targetList)
  112. {
  113. var list = new ReorderableList(targetList, typeof(string), true, true, true, true)
  114. {
  115. drawHeaderCallback = rect =>
  116. {
  117. EditorGUI.LabelField(rect, title);
  118. },
  119. drawElementCallback = (rect, index, isActive, isFocused) =>
  120. {
  121. targetList[index] = EditorGUI.TextField(
  122. new Rect(rect.x, rect.y + 2, rect.width, EditorGUIUtility.singleLineHeight),
  123. targetList[index]
  124. );
  125. },
  126. onAddCallback = l =>
  127. {
  128. targetList.Add("");
  129. },
  130. onRemoveCallback = l =>
  131. {
  132. targetList.RemoveAt(l.index);
  133. }
  134. };
  135. return list;
  136. }
  137. private void DrawScanner()
  138. {
  139. if (GUILayout.Button("Scan project", GUILayout.Height(25)))
  140. {
  141. ScanProject();
  142. }
  143. }
  144. private void ScanProject()
  145. {
  146. CreateMeta();
  147. SerializeProjectSettings();
  148. }
  149. private void SerializeProjectSettings()
  150. {
  151. }
  152. private void CreateMeta()
  153. {
  154. float index = 0;
  155. var assetPaths = AssetDatabase.GetAllAssetPaths();
  156. int assetCount = assetPaths.Length;
  157. foreach (var assetPath in assetPaths)
  158. {
  159. var dataPath = Path.Join(cachePath, assetPath) + ".json";
  160. var metaPath = Path.Join(cachePath, assetPath) + ".meta" + ".json";
  161. var guid = AssetDatabase.AssetPathToGUID(assetPath);
  162. var progress = index / assetCount;
  163. index++;
  164. if (string.IsNullOrEmpty(guid))
  165. {
  166. Debug.LogWarning("Asset is invalid: " + assetPath);
  167. continue;
  168. }
  169. var extension = Path.GetExtension(assetPath);
  170. if (fileExtensionToIgnore.Contains(extension))
  171. {
  172. Debug.LogWarning("Asset file is not supported: " + assetPath);
  173. continue;
  174. }
  175. FileInfo info = new FileInfo(metaPath);
  176. if (info.Directory != null)
  177. {
  178. if (folderToIgnore.Contains(info.Directory.FullName))
  179. {
  180. Debug.Log("Ignoring directory: " + assetPath);
  181. continue;
  182. }
  183. info.Directory.Create();
  184. }
  185. AssetMetaData assetMetaData = new AssetMetaData
  186. {
  187. path = assetPath,
  188. guid = guid
  189. };
  190. if (EditorUtility.DisplayCancelableProgressBar("Updating", "Updating meta for file" + assetPath,
  191. progress))
  192. {
  193. return;
  194. }
  195. Thread.Sleep(1);
  196. var metaData = assetMetaData.ToJson();
  197. File.WriteAllText(metaPath, metaData);
  198. SerializeFile(assetPath, dataPath);
  199. }
  200. EditorUtility.ClearProgressBar();
  201. }
  202. private void SerializeFile(string path, string dataPath)
  203. {
  204. Type type = AssetDatabase.GetMainAssetTypeAtPath(path);
  205. // Looking at supporting types only
  206. if (type != typeof(GameObject) && type != typeof(Texture) && type != typeof(Material) &&
  207. type != typeof(Shader) && type != typeof(Font) && type != typeof(PlayerSettings) &&
  208. type != typeof(Animation) && type != typeof(Material) &&
  209. type != typeof(PrefabAssetType) && type != typeof(TextAsset) && type != typeof(AudioClip) &&
  210. type != typeof(SceneAsset) && type != typeof(MonoScript) &&
  211. type != typeof(ScriptableObject))
  212. {
  213. return;
  214. }
  215. AssetData assetData = new AssetData();
  216. // TODO: Serialization for asset types
  217. var jsonifiedAsset = assetData.ToJson();
  218. File.WriteAllText(dataPath, jsonifiedAsset);
  219. }
  220. }
  221. }