Beispiel #1
0
        static void ShowRowQuantity(Rect rect, string path, BacklinkStore backlinkStore)
        {
            if (!AssetDatabase.IsValidFolder(path))
            {
                return;
            }

            backlinkStore.FoldersWithQty.TryGetValue(path, out var folderWithQty);

            var  cntFiles  = folderWithQty?.UnusedFilesQty ?? 0;
            var  cntScenes = folderWithQty?.UnusedScenesQty ?? 0;
            long size      = folderWithQty?.UnusedSize ?? 0;

            if (cntFiles == 0 && cntScenes == 0)
            {
                return;
            }
            var countStr = cntFiles + cntScenes > 0 ? $"{cntFiles} | {cntScenes} ({CommonUtils.BytesToString(size)})" : "";

            GUI.Label(rect, countStr, _style.ProjectViewCounterLabel);
        }
        BacklinkStore.UnusedQty ShowObjectName(BacklinkStore store, WindowData windowData, TargetTypeEnum targetTypeEnum, SearchArg arg, string[] selectedGuids)
        {
            float TextWidth()
            {
                _buf2.text = _contentBuf.text;
                return(20f + GUI.skin.button.CalcSize(_buf2).x);
            }

            if (arg == null || arg.Main == null)
            {
                return(new BacklinkStore.UnusedQty());
            }

            bool isMultiSelect = selectedGuids != null && selectedGuids.Length > 1;

            if (_contentBuf == null)
            {
                _contentBuf = new GUIContent {
                    tooltip = $"Click to ping"
                };
            }

            _contentBuf.image = isMultiSelect
                ? windowData.Style.MultiSelect.image
                : AssetPreview.GetMiniThumbnail(arg.Target);
            _contentBuf.text    = string.Empty;
            _contentBuf.tooltip = string.Empty;

            if (!isMultiSelect)
            {
                switch (targetTypeEnum)
                {
                case TargetTypeEnum.Directory:
                    if (!SearchArgMain.IsEmpty())
                    {
                        _contentBuf.text = $"{arg.Main.name} (Folder)";
                        if (GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn,
                                             GUILayout.MinWidth(TextWidth())))
                        {
                            EditorGUIUtility.PingObject(arg.Main);
                        }

                        if (AskDeleteUnusedFiles(arg, arg.UnusedAssetsFiltered, windowData))
                        {
                            return(new BacklinkStore.UnusedQty());
                        }

                        if (AskDeleteUnusedScenes(arg, arg.UnusedScenesFiltered, windowData))
                        {
                            return(new BacklinkStore.UnusedQty());
                        }
                    }

                    break;

                case TargetTypeEnum.File:
                    _contentBuf.text = $"{arg.Main.name} (File Asset)";
                    if (GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn,
                                         GUILayout.MinWidth(TextWidth())))
                    {
                        EditorGUIUtility.PingObject(arg.Main);
                    }

                    bool hasUnusedFile = SearchUtils.IsUnused(arg.FilePath);
                    var  previous      = GUI.enabled;
                    GUI.enabled = hasUnusedFile;

                    if (GUILayout.Button(windowData.Style.RemoveFile,
                                         windowData.Style.RemoveUnusedBtn))
                    {
                        var choice = EditorUtility.DisplayDialog(
                            title: "Asset Cleaner",
                            message:
                            $"Do you really want to remove file: \"{arg.Main.name}\"?",
                            ok: "Remove",
                            cancel: "Cancel");
                        if (choice)
                        {
                            EditorApplication.ExecuteMenuItem("File/Save Project");
                            DeleteWithMeta(arg.FilePath);
                            AssetDatabase.Refresh();
                            SearchUtils.Upd(arg);
                        }
                    }

                    GUI.enabled = previous;

                    break;

                case TargetTypeEnum.Scene:
                    _contentBuf.text = $"{arg.Main.name} (Scene)";
                    if (GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn,
                                         GUILayout.MinWidth(TextWidth())))
                    {
                        EditorGUIUtility.PingObject(arg.Main);
                    }

                    bool hasUnusedScene = SearchUtils.IsUnused(arg.FilePath);
                    previous    = GUI.enabled;
                    GUI.enabled = hasUnusedScene;

                    if (GUILayout.Button(windowData.Style.RemoveScene,
                                         windowData.Style.RemoveUnusedBtn))
                    {
                        var choice = EditorUtility.DisplayDialog(
                            title: "Asset Cleaner",
                            message:
                            $"Do you really want to remove scene: {arg.Main.name}?",
                            ok: "Remove",
                            cancel: "Cancel");
                        if (choice)
                        {
                            EditorApplication.ExecuteMenuItem("File/Save Project");
                            DeleteWithMeta(arg.FilePath);
                            AssetDatabase.Refresh();
                            SearchUtils.Upd(arg);
                        }
                    }

                    GUI.enabled = previous;
                    break;

                case TargetTypeEnum.ObjectInScene:
                    _contentBuf.text = $"{arg.Main.name} (Object in Scene)";

                    if (GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn,
                                         GUILayout.MinWidth(TextWidth())))
                    {
                        EditorGUIUtility.PingObject(arg.Main);
                    }

                    break;

                case TargetTypeEnum.ObjectInStage:
                    _contentBuf.image = AssetPreview.GetMiniThumbnail(arg.Target);
                    _contentBuf.text  = $"{arg.Main.name} (Object in Staging)";

                    if (GUILayout.Button(_contentBuf,
                                         windowData.Style.RemoveUnusedBtn))
                    {
                        EditorGUIUtility.PingObject(arg.Main);
                    }

                    break;

                default:
                    if (GUILayout.Button($"{arg.Main.name} (Unknown Object Type)",
                                         windowData.Style.RemoveUnusedBtn))
                    {
                        EditorGUIUtility.PingObject(arg.Main);
                    }

                    break;
                }
            }
            else
            {
                var unusedAssets = new List <string>();
                var unusedScenes = new List <string>();

                foreach (var guid in selectedGuids)
                {
                    var path = AssetDatabase.GUIDToAssetPath(guid);

                    if (store.UnusedFiles.TryGetValue(path, out _))
                    {
                        unusedAssets.Add(path);
                    }

                    else if (store.UnusedScenes.TryGetValue(path, out _))
                    {
                        unusedScenes.Add(path);
                    }

                    if (store.FoldersWithQty.TryGetValue(path, out _))
                    {
                        SearchArg searchArg = new SearchArg()
                        {
                            FilePath = path,
                            Target   = AssetDatabase.LoadAssetAtPath <DefaultAsset>(path),
                            Main     = AssetDatabase.LoadAssetAtPath <DefaultAsset>(path)
                        };
                        SearchUtils.Upd(searchArg);

                        foreach (var unusedAssetPath in searchArg.UnusedAssetsFiltered)
                        {
                            if (store.UnusedFiles.TryGetValue(unusedAssetPath, out _))
                            {
                                unusedAssets.Add(unusedAssetPath);
                            }
                        }

                        foreach (var unusedScenePath in searchArg.UnusedScenesFiltered)
                        {
                            if (store.UnusedScenes.TryGetValue(unusedScenePath, out _))
                            {
                                unusedScenes.Add(unusedScenePath);
                            }
                        }
                    }
                }

                unusedAssets = unusedAssets.Distinct().ToList();
                unusedScenes = unusedScenes.Distinct().ToList();

                var assetSize = unusedAssets.Sum(CommonUtils.Size);
                var sceneSize = unusedScenes.Sum(CommonUtils.Size);

                _contentBuf.text =
                    $"Assets: {unusedAssets.Count} ({CommonUtils.BytesToString(assetSize)}), Scenes: {unusedScenes.Count} ({CommonUtils.BytesToString(sceneSize)})";
                ;

                GUILayout.Button(_contentBuf, windowData.Style.CurrentBtn, GUILayout.MinWidth(TextWidth()));

                if (AskDeleteUnusedFiles(arg, unusedAssets, windowData))
                {
                    return(new BacklinkStore.UnusedQty());
                }

                if (AskDeleteUnusedScenes(arg, unusedScenes, windowData))
                {
                    return(new BacklinkStore.UnusedQty());
                }

                return(new BacklinkStore.UnusedQty(unusedAssets.Count, unusedScenes.Count, assetSize + sceneSize));
            }

            return(new BacklinkStore.UnusedQty());
        }