Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public static class AssetFilesCounter
{
[MenuItem("Utilities/Count Asset Files")]
public static void CountAssetFiles()
{
EditorUtility.DisplayProgressBar("Counting Asset Files", "", 0);
try
{
// using our own counting here, AssetDatabase.GetAllAssetPaths() would include directories
int assetFilesCount = GetAllFilesExcludingExtensions(Application.dataPath, "*.*", "meta").Length;
Debug.Log("Asset Files: " + assetFilesCount);
}
finally
{
EditorUtility.ClearProgressBar();
}
}
public static string[] GetAllFilesExcludingExtensions(string path, string searchPattern, params string[] extensions)
{
if (!Directory.Exists(path))
{
throw new DirectoryNotFoundException("Directory " + path + " not found.");
}
// make sure extension has the right format
for (int i = 0; i < extensions.Length; i++)
{
if (!extensions[i].StartsWith("."))
{
extensions[i] = "." + extensions[i];
}
}
var allFiles = Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
List<string> validFiles = new List<string>();
foreach (var file in allFiles)
{
if (!PathHasExtension(file, extensions))
{
validFiles.Add(file);
}
}
return validFiles.ToArray();
}
public static bool PathHasExtension(string path, params string[] extensions)
{
for (int i = 0; i < extensions.Length; i++)
{
if (path.EndsWith(extensions[i]))
{
return true;
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment