예제 #1
0
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        FindUnusedAssets window = (FindUnusedAssets)EditorWindow.GetWindow(typeof(FindUnusedAssets));

        window.Show();
    }
    public static void FindAssets()
    {
        FindUnusedAssets window = GetWindow <FindUnusedAssets>();

        window.Show();
        canceled = false;

        if (EditorUtility.DisplayCancelableProgressBar("Searching...", "Searching for asset references", 0))
        {
            canceled = true;
            EditorUtility.ClearProgressBar();
            return;
        }

        var assetPaths = AssetDatabase.GetAllAssetPaths().Where(x => x.StartsWith("Assets/" + subfolder) && !AssetDatabase.IsValidFolder(x));

        assetPaths = assetPaths.Where(x => !x.Contains("/Resources/") &&
                                      !x.Contains("/Editor/") && !x.Contains("/Plugins/") && !x.Contains("StreamingAssets"));
        assetPaths = assetPaths.Where(x => !Regex.IsMatch(x, $"\\.({string.Join("|", excludedExtensions)})$"));

        window.unusedAssets = new List <string>(); // Empty old results

        List <string> extensionsToSearchInWithMeta = new List <string>(extensionsToSearchIn);

        extensionsToSearchInWithMeta.AddRange(extensionsToSearchIn.Select(x => x + ".meta"));

        projectPath     = Application.dataPath.Substring(0, Application.dataPath.Length - 7).Replace("/", "\\");
        otherFilesPaths = Directory.EnumerateFiles(projectPath + "\\Assets", "*", SearchOption.AllDirectories).ToList();
        otherFilesPaths.AddRange(Directory.EnumerateFiles(projectPath + "\\ProjectSettings", "*", SearchOption.AllDirectories).ToList());
        otherFilesPaths = otherFilesPaths.Where(x => Regex.IsMatch(x, $"\\.({string.Join("|", extensionsToSearchInWithMeta)})$")).ToList();

        int total   = assetPaths.Count();
        int current = 0;

        foreach (var assetPath in assetPaths)
        {
            current++;
            progress = current / (float)total;

            if (canceled || EditorUtility.DisplayCancelableProgressBar("Searching...", "Looking for asset references", progress))
            {
                EditorUtility.ClearProgressBar();
                return;
            }

            var guid = AssetDatabase.AssetPathToGUID(assetPath);
            if (!FindAnyAssetUsage(guid))
            {
                window.unusedAssets.Add(assetPath);
                File.AppendAllText("Usages.txt", assetPath + " Type[" + AssetDatabase.GetMainAssetTypeAtPath(assetPath) + "]\n");
            }
        }

        EditorUtility.ClearProgressBar();
    }
    public static void OpenUsageWindow()
    {
        FindUnusedAssets window = GetWindow <FindUnusedAssets>();

        window.Show();
    }