SysWindowGui.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using Leopotam.Ecs;
  7. using UnityEditor;
  8. using UnityEditor.SceneManagement;
  9. using UnityEditorInternal;
  10. using UnityEngine;
  11. using UnityEngine.SceneManagement;
  12. using Object = UnityEngine.Object;
  13. namespace Asset_Cleaner {
  14. class FileResultTag { }
  15. enum TargetTypeEnum {
  16. File = 0,
  17. Directory = 1,
  18. Scene = 2,
  19. ObjectInScene = 3,
  20. ObjectInStage = 4
  21. }
  22. class SysWindowGui : IEcsRunSystem, IEcsInitSystem {
  23. EcsFilter<Result, SearchResultGui, InSceneResult> SceneResultRows = null;
  24. EcsFilter<SceneResult, SceneDetails> ScenePaths = null;
  25. EcsFilter<SearchArg>.Exclude<InSceneResult> SearchArgMain = null;
  26. EcsFilter<Result, SearchResultGui, FileResultTag> FileResultRows = null;
  27. public void Init() {
  28. BacklinkStoreDirty(true);
  29. VisualSettingDirty(true);
  30. }
  31. public void Run() {
  32. var windowData = Globals<WindowData>.Value;
  33. _toolbarSelection = GUILayout.Toolbar(_toolbarSelection, _toolbarStrings, GUILayout.ExpandWidth(false));
  34. var conf = Globals<Config>.Value;
  35. switch (_toolbarSelection) {
  36. case 0: {
  37. ShowTabMain(conf, windowData);
  38. break;
  39. }
  40. case 1: {
  41. ShowTabSettings(conf);
  42. break;
  43. }
  44. }
  45. }
  46. string[] _toolbarStrings = {"Main", "Settings"};
  47. const int _progressBarShowFromLevel = 10;
  48. int _toolbarSelection = 0;
  49. int _settingIgnoredPathsHash1;
  50. bool BacklinkStoreDirty(bool set) {
  51. var res = Hash() != _settingIgnoredPathsHash1;
  52. if (set) _settingIgnoredPathsHash1 = Hash();
  53. return res;
  54. int Hash() {
  55. var conf = Globals<Config>.Value;
  56. return DirtyUtils.HashCode(conf.IgnorePathContainsCombined,
  57. conf.IgnoreMaterial,
  58. conf.IgnoreScriptable);
  59. }
  60. }
  61. int _settingCodeHash1;
  62. bool VisualSettingDirty(bool set) {
  63. var res = Hash() != _settingCodeHash1;
  64. if (set) _settingCodeHash1 = Hash();
  65. return res;
  66. int Hash() {
  67. var conf = Globals<Config>.Value;
  68. return DirtyUtils.HashCode(
  69. conf.MarkRed,
  70. conf.ShowInfoBox,
  71. conf.RebuildCacheOnDemand);
  72. }
  73. }
  74. void ShowTabSettings(Config conf) {
  75. using (new EditorGUILayout.VerticalScope()) {
  76. EditorGUILayout.Space();
  77. var enabled = GUI.enabled;
  78. GUI.enabled = true;
  79. using (new EditorGUILayout.VerticalScope()) {
  80. conf.MarkRed = GUILayout.Toggle(conf.MarkRed, "Display counters and red overlay in Project View");
  81. conf.ShowInfoBox = GUILayout.Toggle(conf.ShowInfoBox, "Help suggestions");
  82. conf.RebuildCacheOnDemand = GUILayout.Toggle(conf.RebuildCacheOnDemand, "Rebuild cache on demand (when scripts are updated often)");
  83. EditorGUILayout.Space();
  84. conf.IgnoreMaterial = GUILayout.Toggle(conf.IgnoreMaterial, "Ignore Materials");
  85. conf.IgnoreScriptable = GUILayout.Toggle(conf.IgnoreScriptable, "Ignore ScriptableObjects");
  86. }
  87. EditorGUILayout.Space();
  88. GUI.enabled = enabled;
  89. EditorGUILayout.LabelField("Ignore Path(s) contains:");
  90. conf.IgnorePathContainsCombined = GUILayout.TextArea(conf.IgnorePathContainsCombined);
  91. using (new EditorGUILayout.HorizontalScope(GUILayout.ExpandWidth(false))) {
  92. EditorGUILayout.Space();
  93. var previous = GUI.enabled;
  94. GUI.enabled = BacklinkStoreDirty(false) || VisualSettingDirty(false);
  95. if (GUILayout.Button("Apply")) {
  96. conf.IgnorePathContains = conf.IgnorePathContainsCombined.Split(';')
  97. .Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
  98. Apply();
  99. }
  100. GUI.enabled = previous;
  101. var selectedGuids = Selection.assetGUIDs;
  102. var assetPaths = new List<string>();
  103. if (selectedGuids.Length > 0) {
  104. foreach (var guid in selectedGuids) {
  105. var realAssetPath = AssetDatabase.GUIDToAssetPath(guid);
  106. var obj = AssetDatabase.LoadAssetAtPath<Object>(realAssetPath);
  107. var assetPath = realAssetPath.Replace("Assets/", string.Empty);
  108. if (obj is DefaultAsset &&
  109. !conf.IgnorePathContains.Any(p => (StringComparer.Ordinal.Equals(p, assetPath)))) {
  110. assetPaths.Add(assetPath);
  111. }
  112. }
  113. }
  114. GUI.enabled = (assetPaths.Count > 0);
  115. var foldersList = string.Join(", ", assetPaths);
  116. if (GUILayout.Button("Add Selected Path")) {
  117. var choice = EditorUtility.DisplayDialog(
  118. title: "Asset Cleaner",
  119. message:
  120. $"Do you really want to add these folder(s) to ignored list: \"{foldersList}\"?",
  121. ok: "Ignore",
  122. cancel: "Cancel");
  123. if (choice) {
  124. conf.IgnorePathContainsCombined += $"{foldersList};";
  125. conf.IgnorePathContains = conf.IgnorePathContainsCombined.Split(';')
  126. .Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
  127. Apply();
  128. }
  129. }
  130. GUI.enabled = true;
  131. if (GUILayout.Button("Reset")) {
  132. var choice = EditorUtility.DisplayDialog(
  133. title: "Asset Cleaner",
  134. message:
  135. $"Do you really want to reset to the factory settings?",
  136. ok: "Reset",
  137. cancel: "Cancel");
  138. if (choice) {
  139. var serializable = AufSerializableData.Default();
  140. AufSerializableData.OnDeserialize(in serializable, ref conf);
  141. Apply();
  142. }
  143. }
  144. GUI.enabled = previous;
  145. }
  146. EditorGUILayout.Space();
  147. EditorGUILayout.LabelField(conf.InitializationTime);
  148. var buf = GUI.enabled;
  149. GUI.enabled = Selection.objects.Length > 0;
  150. if (GUILayout.Button("Reserialize selected assets", GUILayout.ExpandWidth(false))) {
  151. var paths = Selection.objects.Select(AssetDatabase.GetAssetPath);
  152. AssetDatabase.ForceReserializeAssets(paths);
  153. EditorApplication.ExecuteMenuItem("File/Save Project");
  154. AssetDatabase.Refresh();
  155. }
  156. GUI.enabled = buf;
  157. EditorGUILayout.Space();
  158. }
  159. void Apply() {
  160. var rebuild = BacklinkStoreDirty(true);
  161. VisualSettingDirty(true);
  162. PersistenceUtils.Save(in conf);
  163. AufCtx.World.NewEntityWith(out RequestRepaintEvt _);
  164. if (rebuild)
  165. Globals<BacklinkStore>.Value.UpdateUnusedAssets();
  166. InternalEditorUtility.RepaintAllViews();
  167. }
  168. }
  169. void ShowTabMain(Config conf, WindowData windowData) {
  170. var store = Globals<BacklinkStore>.Value;
  171. EditorGUIUtility.labelWidth = windowData.Window.position.width * .7f;
  172. int Hash() => DirtyUtils.HashCode(conf.Locked);
  173. var active = SearchArgMain.Get1[0];
  174. if (conf.Locked && (windowData.FindFrom == FindModeEnum.File &&
  175. (active == null || active.Main == null || !AssetDatabase.Contains(active.Main)))) {
  176. conf.Locked = false;
  177. AufCtx.World.NewEntityWith(out RequestRepaintEvt _);
  178. }
  179. var style = windowData.Style;
  180. var hash = Hash();
  181. if (hash != Hash()) {
  182. PersistenceUtils.Save(in conf);
  183. AufCtx.World.NewEntityWith(out RequestRepaintEvt _);
  184. }
  185. // if (Globals<WindowData>.Get() == null) return;
  186. EditorGUILayout.Space();
  187. SearchArg arg = default;
  188. foreach (var i in SearchArgMain) {
  189. arg = SearchArgMain.Get1[i];
  190. if (arg != null && arg.Main != null) {
  191. break;
  192. }
  193. }
  194. if (arg == default)
  195. return;
  196. var targetTypeEnum = GetTargetType(windowData, arg?.Main);
  197. BacklinkStore.UnusedQty unusedQty = new BacklinkStore.UnusedQty(0, 0, 0);
  198. using (new EditorGUILayout.HorizontalScope()) {
  199. var enabledBuf = GUI.enabled;
  200. var selectedGuids = Selection.assetGUIDs;
  201. var undoRedoState = Globals<UndoRedoState>.Value;
  202. GUI.enabled = selectedGuids != null && !conf.Locked && undoRedoState.UndoEnabled;
  203. if (GUILayout.Button(style.ArrowL, style.ArrowBtn)) {
  204. AufCtx.World.NewEntityWith(out UndoEvt _);
  205. }
  206. GUI.enabled = selectedGuids != null && !conf.Locked && undoRedoState.RedoEnabled;
  207. if (GUILayout.Button(style.ArrowR, style.ArrowBtn)) {
  208. AufCtx.World.NewEntityWith(out RedoEvt _);
  209. }
  210. GUI.enabled = enabledBuf;
  211. if (conf.Locked) {
  212. if (GUILayout.Button(style.Lock, style.LockBtn)) {
  213. AufCtx.World.NewEntityWith(out SelectionChanged selectionChanged);
  214. conf.Locked = false;
  215. if (Selection.activeObject != arg.Target) {
  216. selectionChanged.From = FindModeEnum.Scene;
  217. selectionChanged.Scene = SceneManager.GetActiveScene();
  218. selectionChanged.Target = Selection.activeObject;
  219. }
  220. else if (Selection.assetGUIDs is string[] guids) {
  221. // todo show info box multiple selection is unsupported
  222. if (guids.Length > 0) {
  223. var path = AssetDatabase.GUIDToAssetPath(guids[0]);
  224. selectionChanged.Target = AssetDatabase.LoadAssetAtPath<Object>(path);
  225. switch (Selection.selectionChanged.Target) {
  226. case DefaultAsset _:
  227. selectionChanged.From = FindModeEnum.File;
  228. break;
  229. case GameObject go when go.scene.isLoaded:
  230. selectionChanged.From = FindModeEnum.Scene;
  231. selectionChanged.Scene = SceneManager.GetActiveScene();
  232. break;
  233. default:
  234. selectionChanged.From = FindModeEnum.File;
  235. break;
  236. }
  237. }
  238. else if (Selection.activeObject is GameObject go && go.scene.isLoaded) {
  239. selectionChanged.From = FindModeEnum.Scene;
  240. selectionChanged.Target = Selection.activeObject;
  241. selectionChanged.Scene = SceneManager.GetActiveScene();
  242. }
  243. }
  244. }
  245. }
  246. else {
  247. var enabled = GUI.enabled;
  248. GUI.enabled = selectedGuids != null && selectedGuids.Length == 1;
  249. if (GUILayout.Button(style.Unlock, style.UnlockBtn)) {
  250. conf.Locked = true;
  251. }
  252. GUI.enabled = enabled;
  253. }
  254. unusedQty = ShowObjectName(store, windowData, targetTypeEnum, arg, selectedGuids);
  255. }
  256. bool isMultiSelect = Selection.assetGUIDs != null && Selection.assetGUIDs.Length > 1;
  257. if (conf.ShowInfoBox) {
  258. if (isMultiSelect && (unusedQty.UnusedFilesQty + unusedQty.UnusedScenesQty > 0)) {
  259. var msgUnusedFiles = (unusedQty.UnusedFilesQty > 0)
  260. ? $"unused files ({unusedQty.UnusedFilesQty}),"
  261. : "";
  262. var msgUnusedScenes = (unusedQty.UnusedScenesQty > 0)
  263. ? $"unused scenes ({unusedQty.UnusedScenesQty}),"
  264. : "";
  265. var msgMultiSelect = $"This multi-selection contains: " +
  266. msgUnusedFiles + msgUnusedScenes +
  267. $"\nYou could delete them pressing corresponding button to the right.";
  268. EditorGUILayout.HelpBox(msgMultiSelect, MessageType.Info);
  269. }
  270. else if (TryGetHelpInfo(arg, out var msg, out var msgType)) {
  271. EditorGUILayout.HelpBox(msg, msgType);
  272. }
  273. }
  274. if (targetTypeEnum != TargetTypeEnum.Directory && !isMultiSelect) {
  275. var windowData2 = Globals<WindowData>.Value;
  276. EditorGUILayout.BeginVertical();
  277. {
  278. windowData2.ScrollPos = EditorGUILayout.BeginScrollView(windowData2.ScrollPos);
  279. {
  280. RenderRows(windowData2); //TODO?
  281. EditorGUILayout.Space();
  282. }
  283. EditorGUILayout.EndScrollView();
  284. }
  285. EditorGUILayout.EndVertical();
  286. EditorGUILayout.Space();
  287. }
  288. }
  289. static bool TryGetHelpInfo(SearchArg arg, out string msg, out MessageType msgType) {
  290. msgType = MessageType.Info;
  291. if (arg == null) {
  292. msg = default;
  293. return false;
  294. }
  295. var path = arg.FilePath;
  296. if (string.IsNullOrEmpty(path)) {
  297. msg = default;
  298. return false;
  299. }
  300. if (SearchUtils.IgnoredPaths(path, out var subPath)) {
  301. msg = $"Paths containing '{subPath}' are ignored. You could add or remove it in Settings tab";
  302. return true;
  303. }
  304. if (SearchUtils.IgnoredNonAssets(path) && !path.Eq("Assets")) {
  305. msg = $"Asset is outside of Assets folder";
  306. return true;
  307. }
  308. if (IgnoreTypes.Check(path, out var type)) {
  309. if (AssetDatabase.IsValidFolder(path)) {
  310. var scenes = arg.UnusedScenesFiltered?.Count;
  311. var files = arg.UnusedAssetsFiltered?.Count;
  312. if (scenes == 0 && files == 0) {
  313. msg = default;
  314. return false;
  315. }
  316. var b = new StringBuilder();
  317. b.Append("This directory contains: ");
  318. var any = false;
  319. if (files > 0) {
  320. any = true;
  321. b.Append($"unused files ({files})");
  322. }
  323. if (scenes > 0) {
  324. if (any)
  325. b.Append(", ");
  326. b.Append($"unused scenes ({scenes})");
  327. }
  328. b.Append(
  329. ".\nYou could delete them pressing corresponding button to the right.\nIf you don't want it to be inspected, please add it to Ignore list in the Settings tab");
  330. msg = b.ToString();
  331. return true;
  332. }
  333. msg = $"Assets of type '{type.Name}' are ignored. Please contact support if you need to change it";
  334. return true;
  335. }
  336. // if (Filters.ScenePaths.GetEntitiesCount() == 0 && Filters.FileResultRows.GetEntitiesCount() == 0) {
  337. if (SearchUtils.IsUnused(path)) {
  338. type = AssetDatabase.GetMainAssetTypeAtPath(path);
  339. var name = type.IsAssignableFromInverse(typeof(SceneAsset)) ? "scene" : "file";
  340. msg =
  341. $"This {name} has no explicit serialized usages and potentially could be removed. If you don't want it to be inspected, please add the containing folder to ignore list";
  342. return true;
  343. }
  344. msgType = default;
  345. msg = default;
  346. return false;
  347. }
  348. static TargetTypeEnum GetTargetType(WindowData windowData1, Object obj) {
  349. if (obj == null) return TargetTypeEnum.File;
  350. var targetTypeEnum = TargetTypeEnum.Directory;
  351. var path = AssetDatabase.GetAssetPath(obj);
  352. switch (windowData1.FindFrom) {
  353. case FindModeEnum.File when obj is DefaultAsset:
  354. targetTypeEnum = TargetTypeEnum.Directory;
  355. break;
  356. case FindModeEnum.File when path.LastIndexOf(".unity", StringComparison.Ordinal) != -1:
  357. targetTypeEnum = TargetTypeEnum.Scene;
  358. break;
  359. case FindModeEnum.File:
  360. targetTypeEnum = TargetTypeEnum.File;
  361. break;
  362. case FindModeEnum.Scene:
  363. targetTypeEnum = TargetTypeEnum.ObjectInScene;
  364. break;
  365. case FindModeEnum.Stage:
  366. targetTypeEnum = TargetTypeEnum.ObjectInStage;
  367. break;
  368. }
  369. return targetTypeEnum;
  370. }
  371. GUIContent _contentBuf = new GUIContent();
  372. GUIContent _buf2 = new GUIContent();
  373. BacklinkStore.UnusedQty ShowObjectName(BacklinkStore store, WindowData windowData, TargetTypeEnum targetTypeEnum, SearchArg arg, string[] selectedGuids) {
  374. float TextWidth() {
  375. _buf2.text = _contentBuf.text;
  376. return 20f + GUI.skin.button.CalcSize(_buf2).x;
  377. }
  378. if (arg == null || arg.Main == null) return new BacklinkStore.UnusedQty();
  379. bool isMultiSelect = selectedGuids != null && selectedGuids.Length > 1;
  380. if (_contentBuf == null) {
  381. _contentBuf = new GUIContent {tooltip = $"Click to ping"};
  382. }
  383. _contentBuf.image = isMultiSelect
  384. ? windowData.Style.MultiSelect.image
  385. : AssetPreview.GetMiniThumbnail(arg.Target);
  386. _contentBuf.text = string.Empty;
  387. _contentBuf.tooltip = string.Empty;
  388. if (!isMultiSelect) {
  389. switch (targetTypeEnum) {
  390. case TargetTypeEnum.Directory:
  391. if (!SearchArgMain.IsEmpty()) {
  392. _contentBuf.text = $"{arg.Main.name} (Folder)";
  393. if (GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn,
  394. GUILayout.MinWidth(TextWidth()))) {
  395. EditorGUIUtility.PingObject(arg.Main);
  396. }
  397. if (AskDeleteUnusedFiles(arg, arg.UnusedAssetsFiltered, windowData))
  398. return new BacklinkStore.UnusedQty();
  399. if (AskDeleteUnusedScenes(arg, arg.UnusedScenesFiltered, windowData))
  400. return new BacklinkStore.UnusedQty();
  401. }
  402. break;
  403. case TargetTypeEnum.File:
  404. _contentBuf.text = $"{arg.Main.name} (File Asset)";
  405. if (GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn,
  406. GUILayout.MinWidth(TextWidth()))) {
  407. EditorGUIUtility.PingObject(arg.Main);
  408. }
  409. bool hasUnusedFile = SearchUtils.IsUnused(arg.FilePath);
  410. var previous = GUI.enabled;
  411. GUI.enabled = hasUnusedFile;
  412. if (GUILayout.Button(windowData.Style.RemoveFile,
  413. windowData.Style.RemoveUnusedBtn)) {
  414. var choice = EditorUtility.DisplayDialog(
  415. title: "Asset Cleaner",
  416. message:
  417. $"Do you really want to remove file: \"{arg.Main.name}\"?",
  418. ok: "Remove",
  419. cancel: "Cancel");
  420. if (choice) {
  421. EditorApplication.ExecuteMenuItem("File/Save Project");
  422. DeleteWithMeta(arg.FilePath);
  423. AssetDatabase.Refresh();
  424. SearchUtils.Upd(arg);
  425. }
  426. }
  427. GUI.enabled = previous;
  428. break;
  429. case TargetTypeEnum.Scene:
  430. _contentBuf.text = $"{arg.Main.name} (Scene)";
  431. if (GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn,
  432. GUILayout.MinWidth(TextWidth()))) {
  433. EditorGUIUtility.PingObject(arg.Main);
  434. }
  435. bool hasUnusedScene = SearchUtils.IsUnused(arg.FilePath);
  436. previous = GUI.enabled;
  437. GUI.enabled = hasUnusedScene;
  438. if (GUILayout.Button(windowData.Style.RemoveScene,
  439. windowData.Style.RemoveUnusedBtn)) {
  440. var choice = EditorUtility.DisplayDialog(
  441. title: "Asset Cleaner",
  442. message:
  443. $"Do you really want to remove scene: {arg.Main.name}?",
  444. ok: "Remove",
  445. cancel: "Cancel");
  446. if (choice) {
  447. EditorApplication.ExecuteMenuItem("File/Save Project");
  448. DeleteWithMeta(arg.FilePath);
  449. AssetDatabase.Refresh();
  450. SearchUtils.Upd(arg);
  451. }
  452. }
  453. GUI.enabled = previous;
  454. break;
  455. case TargetTypeEnum.ObjectInScene:
  456. _contentBuf.text = $"{arg.Main.name} (Object in Scene)";
  457. if (GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn,
  458. GUILayout.MinWidth(TextWidth()))) {
  459. EditorGUIUtility.PingObject(arg.Main);
  460. }
  461. break;
  462. case TargetTypeEnum.ObjectInStage:
  463. _contentBuf.image = AssetPreview.GetMiniThumbnail(arg.Target);
  464. _contentBuf.text = $"{arg.Main.name} (Object in Staging)";
  465. if (GUILayout.Button(_contentBuf,
  466. windowData.Style.RemoveUnusedBtn)) {
  467. EditorGUIUtility.PingObject(arg.Main);
  468. }
  469. break;
  470. default:
  471. if (GUILayout.Button($"{arg.Main.name} (Unknown Object Type)",
  472. windowData.Style.RemoveUnusedBtn)) {
  473. EditorGUIUtility.PingObject(arg.Main);
  474. }
  475. break;
  476. }
  477. }
  478. else {
  479. var unusedAssets = new List<string>();
  480. var unusedScenes = new List<string>();
  481. foreach (var guid in selectedGuids) {
  482. var path = AssetDatabase.GUIDToAssetPath(guid);
  483. if (store.UnusedFiles.TryGetValue(path, out _))
  484. unusedAssets.Add(path);
  485. else if (store.UnusedScenes.TryGetValue(path, out _))
  486. unusedScenes.Add(path);
  487. if (store.FoldersWithQty.TryGetValue(path, out _)) {
  488. SearchArg searchArg = new SearchArg() {
  489. FilePath = path,
  490. Target = AssetDatabase.LoadAssetAtPath<DefaultAsset>(path),
  491. Main = AssetDatabase.LoadAssetAtPath<DefaultAsset>(path)
  492. };
  493. SearchUtils.Upd(searchArg);
  494. foreach (var unusedAssetPath in searchArg.UnusedAssetsFiltered)
  495. if (store.UnusedFiles.TryGetValue(unusedAssetPath, out _))
  496. unusedAssets.Add(unusedAssetPath);
  497. foreach (var unusedScenePath in searchArg.UnusedScenesFiltered)
  498. if (store.UnusedScenes.TryGetValue(unusedScenePath, out _))
  499. unusedScenes.Add(unusedScenePath);
  500. }
  501. }
  502. unusedAssets = unusedAssets.Distinct().ToList();
  503. unusedScenes = unusedScenes.Distinct().ToList();
  504. var assetSize = unusedAssets.Sum(CommonUtils.Size);
  505. var sceneSize = unusedScenes.Sum(CommonUtils.Size);
  506. _contentBuf.text =
  507. $"Assets: {unusedAssets.Count} ({CommonUtils.BytesToString(assetSize)}), Scenes: {unusedScenes.Count} ({CommonUtils.BytesToString(sceneSize)})";
  508. ;
  509. GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn, GUILayout.MinWidth(TextWidth()));
  510. if (AskDeleteUnusedFiles(arg, unusedAssets, windowData))
  511. return new BacklinkStore.UnusedQty();
  512. if (AskDeleteUnusedScenes(arg, unusedScenes, windowData))
  513. return new BacklinkStore.UnusedQty();
  514. return new BacklinkStore.UnusedQty(unusedAssets.Count, unusedScenes.Count, assetSize + sceneSize);
  515. }
  516. return new BacklinkStore.UnusedQty();
  517. }
  518. static bool AskDeleteUnusedFiles(SearchArg arg, List<string> unusedAssets, WindowData windowData) {
  519. if (arg == null || unusedAssets == null) return false;
  520. var hasUnusedAssets = unusedAssets.Count > 0;
  521. var previous = GUI.enabled;
  522. GUI.enabled = hasUnusedAssets;
  523. var guiContentRemoveAssets = windowData.Style.RemoveFile;
  524. if (GUILayout.Button(guiContentRemoveAssets,
  525. windowData.Style.RemoveUnusedBtn)) {
  526. var choice = EditorUtility.DisplayDialog(
  527. title: "Asset Cleaner",
  528. message:
  529. $"Do you really want to remove {unusedAssets.Count} asset(s)?",
  530. ok: "Remove",
  531. cancel: "Cancel");
  532. if (choice) {
  533. EditorApplication.ExecuteMenuItem("File/Save Project");
  534. var i = 0f;
  535. var total = (float) unusedAssets.Count;
  536. foreach (var f in unusedAssets) {
  537. var path = Application.dataPath.Replace("Assets", f);
  538. DeleteWithMeta(path);
  539. var percent = i * 100 / total;
  540. if (total >= _progressBarShowFromLevel) {
  541. if (Math.Abs(percent % 5f) < 0.01f) {
  542. if (EditorUtility.DisplayCancelableProgressBar(
  543. "Please wait...",
  544. "Deleting assets...", percent))
  545. throw new Exception("Deleting aborted");
  546. }
  547. i++;
  548. }
  549. }
  550. if (total >= _progressBarShowFromLevel) {
  551. EditorUtility.ClearProgressBar();
  552. }
  553. AssetDatabase.Refresh();
  554. SearchUtils.Upd(arg);
  555. }
  556. GUI.enabled = previous;
  557. return true;
  558. }
  559. GUI.enabled = previous;
  560. return false;
  561. }
  562. static void DeleteWithMeta(string path) {
  563. FileUtil.DeleteFileOrDirectory(path);
  564. var metaPath = AssetDatabase.GetTextMetaFilePathFromAssetPath(path);
  565. if (!string.IsNullOrEmpty(metaPath))
  566. FileUtil.DeleteFileOrDirectory(metaPath);
  567. }
  568. static bool AskDeleteUnusedScenes(SearchArg arg, List<string> unusedScenes, WindowData windowData) {
  569. if (arg == null || unusedScenes == null) return false;
  570. var hasUnusedScenes = unusedScenes.Count > 0;
  571. var previous = GUI.enabled;
  572. GUI.enabled = hasUnusedScenes;
  573. var guiContentRemoveScenes = windowData.Style.RemoveScene;
  574. if (GUILayout.Button(guiContentRemoveScenes,
  575. windowData.Style.RemoveUnusedBtn)) {
  576. var choice = EditorUtility.DisplayDialog(
  577. title: "Asset Cleaner",
  578. message:
  579. $"Do you really want to remove {unusedScenes.Count} scene(s)?",
  580. ok: "Remove",
  581. cancel: "Cancel");
  582. if (choice) {
  583. EditorApplication.ExecuteMenuItem("File/Save Project");
  584. var i = 0f;
  585. var total = (float) unusedScenes.Count;
  586. foreach (var scene in unusedScenes) {
  587. var path = Application.dataPath.Replace("Assets", scene);
  588. DeleteWithMeta(path);
  589. if (total >= _progressBarShowFromLevel) {
  590. var percent = i * 100 / total;
  591. if (Math.Abs(percent % 5f) < 0.01f) {
  592. if (EditorUtility.DisplayCancelableProgressBar(
  593. "Please wait...",
  594. "Deleting scenes...", percent))
  595. throw new Exception("Deleting aborted");
  596. }
  597. i++;
  598. }
  599. }
  600. if (total >= _progressBarShowFromLevel) {
  601. EditorUtility.ClearProgressBar();
  602. }
  603. AssetDatabase.Refresh();
  604. SearchUtils.Upd(arg);
  605. }
  606. GUI.enabled = previous;
  607. return true;
  608. }
  609. GUI.enabled = previous;
  610. return false;
  611. }
  612. void RenderRows(WindowData windowData) {
  613. // todo show spinner until scene is loaded,
  614. if (FileResultRows.GetEntitiesCount() > 0) {
  615. windowData.ExpandFiles =
  616. EditorGUILayout.Foldout(windowData.ExpandFiles,
  617. $"Usages in Project: {FileResultRows.GetEntitiesCount()}");
  618. }
  619. if (SearchArgMain.IsEmpty())
  620. return;
  621. if (windowData.ExpandFiles && windowData.FindFrom == FindModeEnum.File)
  622. foreach (var i in FileResultRows.Out(out var get1, out var get2, out _, out _))
  623. DrawRowFile(get1[i], get2[i], windowData);
  624. var sceneMessage = $"Usages in Scenes: {ScenePaths.GetEntitiesCount()}";
  625. if (ScenePaths.GetEntitiesCount() > 0) {
  626. windowData.ExpandScenes =
  627. EditorGUILayout.Foldout(windowData.ExpandScenes, sceneMessage);
  628. }
  629. if (!windowData.ExpandScenes) return;
  630. if (windowData.ExpandScenes && windowData.FindFrom == FindModeEnum.Scene) {
  631. foreach (var (grp, indices) in SceneResultRows.Out(out _, out var get2, out _, out _)
  632. .GroupBy1(ResultComp.Instance)) {
  633. using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) {
  634. var count = 0;
  635. foreach (var i in indices) {
  636. if (count++ == 0)
  637. if (GUILayout.Button(get2[i].Label, windowData.Style.RowMainAssetBtn)) {
  638. if (windowData.Click.IsDoubleClick(grp.RootGo)) {
  639. // _selectionChangedByArrows = false;
  640. Selection.activeObject = grp.RootGo;
  641. }
  642. else
  643. EditorGUIUtility.PingObject(grp.RootGo);
  644. windowData.Click = new PrevClick(grp.RootGo);
  645. }
  646. DrawRowScene(get2[i]);
  647. }
  648. }
  649. }
  650. }
  651. using (new GUILayout.HorizontalScope()) {
  652. GUILayout.Space(windowData.Style.SceneIndent1);
  653. using (new EditorGUILayout.VerticalScope()) {
  654. foreach (var i1 in ScenePaths.Out(out var get1, out var get2, out _)) {
  655. windowData.SceneFoldout.text = get1[i1].PathNicified;
  656. var details = get2[i1];
  657. details.SearchRequested = details.Scene.isLoaded;
  658. details.SearchRequested = EditorGUILayout.Foldout(details.SearchRequested,
  659. windowData.SceneFoldout, EditorStyles.foldout);
  660. if (details.SearchRequested && details.Scene.isLoaded && !details.SearchDone) {
  661. var mainArg = SearchArgMain.GetSingle();
  662. mainArg.Scene = SceneManager.GetSceneByPath(details.Scene.path);
  663. SearchUtils.InScene(mainArg, details.Scene);
  664. details.SearchDone = true;
  665. }
  666. if (!details.SearchRequested) {
  667. if (!details.Scene.isLoaded) continue;
  668. if (!details.WasOpened) {
  669. // to clean up on selection change
  670. AufCtx.World.NewEntityWith(out SceneToClose comp);
  671. comp.Scene = details.Scene;
  672. comp.ForceClose = true;
  673. }
  674. foreach (var row in SceneResultRows.Out(out _, out _, out var get3, out var entities)) {
  675. if (!get3[row].ScenePath.Eq(details.Path))
  676. continue;
  677. entities[row].Destroy();
  678. }
  679. details.SearchDone = false;
  680. }
  681. else {
  682. if (!details.Scene.isLoaded) {
  683. details.Scene = EditorSceneManager.OpenScene(details.Path, OpenSceneMode.Additive);
  684. // to clean up on selection change
  685. AufCtx.World.NewEntityWith(out SceneToClose comp);
  686. comp.Scene = details.Scene;
  687. comp.SelectionId = Globals<PersistentUndoRedoState>.Value.Id;
  688. #if UNITY_2019_1_OR_NEWER
  689. EditorSceneManager.SetSceneCullingMask(details.Scene, 0);
  690. #endif
  691. details.SearchRequested = true;
  692. // todo Scope component
  693. #if later
  694. if (details.Scene.isLoaded)
  695. EditorSceneManager.CloseScene(details.Scene, false);
  696. #endif
  697. }
  698. else if (SceneResultRows.IsEmpty())
  699. EditorGUILayout.LabelField("No in-scene dependencies found.");
  700. else
  701. using (new GUILayout.HorizontalScope()) {
  702. GUILayout.Space(windowData.Style.SceneIndent2);
  703. using (new EditorGUILayout.VerticalScope())
  704. foreach (var (grp, indices) in SceneResultRows
  705. .Out(out var g1, out var g2, out var g3, out _)
  706. .GroupBy1(ResultComp.Instance)) {
  707. var any = false;
  708. foreach (var i3 in indices) {
  709. if (!g3[i3].ScenePath.Eq(details.Path))
  710. continue;
  711. any = true;
  712. break;
  713. }
  714. if (!any)
  715. continue;
  716. using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) {
  717. var count = 0;
  718. foreach (var i2 in indices) {
  719. if (!g3[i2].ScenePath.Eq(details.Path))
  720. continue;
  721. if (count++ == 0) {
  722. Result comp = g1[i2];
  723. if (GUILayout.Button(g2[i2].Label,
  724. windowData.Style.RowMainAssetBtn)) {
  725. if (windowData.Click.IsDoubleClick(grp.RootGo)) {
  726. // _selectionChangedByArrows = false;
  727. Selection.activeObject = comp.RootGo;
  728. }
  729. else
  730. EditorGUIUtility.PingObject(comp.RootGo);
  731. windowData.Click = new PrevClick(comp.RootGo);
  732. }
  733. }
  734. DrawRowScene(g2[i2]);
  735. }
  736. }
  737. }
  738. }
  739. }
  740. }
  741. }
  742. }
  743. }
  744. class ResultComp : IEqualityComparer<Result> {
  745. public static ResultComp Instance { get; } = new ResultComp();
  746. public bool Equals(Result x, Result y) => GetHashCode(x) == GetHashCode(y);
  747. public int GetHashCode(Result obj) => obj.RootGo.GetInstanceID();
  748. }
  749. static void DrawRowScene(SearchResultGui gui) {
  750. EditorGUI.BeginChangeCheck();
  751. // if (data.TargetGo || data.TargetComponent)
  752. foreach (var prop in gui.Properties) {
  753. {
  754. var locked = prop.Property.objectReferenceValue is MonoScript;
  755. var f = GUI.enabled;
  756. if (locked) GUI.enabled = false;
  757. EditorGUILayout.PropertyField(prop.Property, prop.Content, false);
  758. if (locked) GUI.enabled = f;
  759. }
  760. }
  761. if (EditorGUI.EndChangeCheck())
  762. gui.SerializedObject.ApplyModifiedProperties();
  763. }
  764. static void DrawRowFile(Result data, SearchResultGui gui, WindowData windowData) {
  765. using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox)) {
  766. var buf = GUI.color;
  767. var pingGo = data.MainFile == null ? data.RootGo : data.MainFile;
  768. if (GUILayout.Button(gui.Label, windowData.Style.RowMainAssetBtn)) {
  769. if (windowData.Click.IsDoubleClick(pingGo)) {
  770. // _selectionChangedByArrows = false;
  771. Selection.activeObject = pingGo;
  772. }
  773. else {
  774. EditorGUIUtility.PingObject(pingGo);
  775. }
  776. windowData.Click = new PrevClick(pingGo);
  777. }
  778. GUI.color = buf;
  779. EditorGUI.BeginChangeCheck();
  780. if (data.File) {
  781. foreach (var prop in gui.Properties) {
  782. using (new EditorGUILayout.HorizontalScope()) {
  783. var locked = prop.Property.objectReferenceValue is MonoScript;
  784. var f = GUI.enabled;
  785. if (locked) GUI.enabled = false;
  786. EditorGUILayout.PropertyField(prop.Property, prop.Content, false);
  787. if (locked) GUI.enabled = f;
  788. }
  789. }
  790. }
  791. if (EditorGUI.EndChangeCheck()) {
  792. gui.SerializedObject.ApplyModifiedProperties();
  793. // dependency.SerializedObject.Update();
  794. }
  795. }
  796. }
  797. }
  798. }